source: trip-planner-front/node_modules/less-loader/dist/utils.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 7.2 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.getLessOptions = getLessOptions;
7exports.isUnsupportedUrl = isUnsupportedUrl;
8exports.normalizeSourceMap = normalizeSourceMap;
9exports.getLessImplementation = getLessImplementation;
10
11var _path = _interopRequireDefault(require("path"));
12
13var _full = require("klona/full");
14
15function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16
17/* eslint-disable class-methods-use-this */
18const trailingSlash = /[/\\]$/; // This somewhat changed in Less 3.x. Now the file name comes without the
19// automatically added extension whereas the extension is passed in as `options.ext`.
20// So, if the file name matches this regexp, we simply ignore the proposed extension.
21
22const IS_SPECIAL_MODULE_IMPORT = /^~[^/]+$/; // `[drive_letter]:\` + `\\[server]\[sharename]\`
23
24const IS_NATIVE_WIN32_PATH = /^[a-z]:[/\\]|^\\\\/i; // Examples:
25// - ~package
26// - ~package/
27// - ~@org
28// - ~@org/
29// - ~@org/package
30// - ~@org/package/
31
32const IS_MODULE_IMPORT = /^~([^/]+|[^/]+\/|@[^/]+[/][^/]+|@[^/]+\/?|@[^/]+[/][^/]+\/)$/;
33const MODULE_REQUEST_REGEX = /^[^?]*~/;
34/**
35 * Creates a Less plugin that uses webpack's resolving engine that is provided by the loaderContext.
36 *
37 * @param {LoaderContext} loaderContext
38 * @param {object} implementation
39 * @returns {LessPlugin}
40 */
41
42function createWebpackLessPlugin(loaderContext, implementation) {
43 const resolve = loaderContext.getResolve({
44 dependencyType: "less",
45 conditionNames: ["less", "style"],
46 mainFields: ["less", "style", "main", "..."],
47 mainFiles: ["index", "..."],
48 extensions: [".less", ".css"],
49 preferRelative: true
50 });
51
52 class WebpackFileManager extends implementation.FileManager {
53 supports(filename) {
54 if (filename[0] === "/" || IS_NATIVE_WIN32_PATH.test(filename)) {
55 return true;
56 }
57
58 if (this.isPathAbsolute(filename)) {
59 return false;
60 }
61
62 return true;
63 } // Sync resolving is used at least by the `data-uri` function.
64 // This file manager doesn't know how to do it, so let's delegate it
65 // to the default file manager of Less.
66 // We could probably use loaderContext.resolveSync, but it's deprecated,
67 // see https://webpack.js.org/api/loaders/#this-resolvesync
68
69
70 supportsSync() {
71 return false;
72 }
73
74 async resolveFilename(filename, currentDirectory) {
75 // Less is giving us trailing slashes, but the context should have no trailing slash
76 const context = currentDirectory.replace(trailingSlash, "");
77 let request = filename; // A `~` makes the url an module
78
79 if (MODULE_REQUEST_REGEX.test(filename)) {
80 request = request.replace(MODULE_REQUEST_REGEX, "");
81 }
82
83 if (IS_MODULE_IMPORT.test(filename)) {
84 request = request[request.length - 1] === "/" ? request : `${request}/`;
85 }
86
87 return this.resolveRequests(context, [...new Set([request, filename])]);
88 }
89
90 async resolveRequests(context, possibleRequests) {
91 if (possibleRequests.length === 0) {
92 return Promise.reject();
93 }
94
95 let result;
96
97 try {
98 result = await resolve(context, possibleRequests[0]);
99 } catch (error) {
100 const [, ...tailPossibleRequests] = possibleRequests;
101
102 if (tailPossibleRequests.length === 0) {
103 throw error;
104 }
105
106 result = await this.resolveRequests(context, tailPossibleRequests);
107 }
108
109 return result;
110 }
111
112 async loadFile(filename, ...args) {
113 let result;
114
115 try {
116 if (IS_SPECIAL_MODULE_IMPORT.test(filename)) {
117 const error = new Error();
118 error.type = "Next";
119 throw error;
120 }
121
122 result = await super.loadFile(filename, ...args);
123 } catch (error) {
124 if (error.type !== "File" && error.type !== "Next") {
125 return Promise.reject(error);
126 }
127
128 try {
129 result = await this.resolveFilename(filename, ...args);
130 } catch (webpackResolveError) {
131 error.message = `Less resolver error:\n${error.message}\n\n` + `Webpack resolver error details:\n${webpackResolveError.details}\n\n` + `Webpack resolver error missing:\n${webpackResolveError.missing}\n\n`;
132 return Promise.reject(error);
133 }
134
135 loaderContext.addDependency(result);
136 return super.loadFile(result, ...args);
137 }
138
139 loaderContext.addDependency(_path.default.normalize(result.filename));
140 return result;
141 }
142
143 }
144
145 return {
146 install(lessInstance, pluginManager) {
147 pluginManager.addFileManager(new WebpackFileManager());
148 },
149
150 minVersion: [3, 0, 0]
151 };
152}
153/**
154 * Get the less options from the loader context and normalizes its values
155 *
156 * @param {object} loaderContext
157 * @param {object} loaderOptions
158 * @param {object} implementation
159 * @returns {Object}
160 */
161
162
163function getLessOptions(loaderContext, loaderOptions, implementation) {
164 const options = (0, _full.klona)(typeof loaderOptions.lessOptions === "function" ? loaderOptions.lessOptions(loaderContext) || {} : loaderOptions.lessOptions || {});
165 const lessOptions = {
166 plugins: [],
167 relativeUrls: true,
168 // We need to set the filename because otherwise our WebpackFileManager will receive an undefined path for the entry
169 filename: loaderContext.resourcePath,
170 ...options
171 };
172 const shouldUseWebpackImporter = typeof loaderOptions.webpackImporter === "boolean" ? loaderOptions.webpackImporter : true;
173
174 if (shouldUseWebpackImporter) {
175 lessOptions.plugins.unshift(createWebpackLessPlugin(loaderContext, implementation));
176 }
177
178 lessOptions.plugins.unshift({
179 install(lessProcessor, pluginManager) {
180 // eslint-disable-next-line no-param-reassign
181 pluginManager.webpackLoaderContext = loaderContext;
182 lessOptions.pluginManager = pluginManager;
183 }
184
185 });
186 return lessOptions;
187}
188
189function isUnsupportedUrl(url) {
190 // Is Windows path
191 if (IS_NATIVE_WIN32_PATH.test(url)) {
192 return false;
193 } // Scheme: https://tools.ietf.org/html/rfc3986#section-3.1
194 // Absolute URL: https://tools.ietf.org/html/rfc3986#section-4.3
195
196
197 return /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(url);
198}
199
200function normalizeSourceMap(map) {
201 const newMap = map; // map.file is an optional property that provides the output filename.
202 // Since we don't know the final filename in the webpack build chain yet, it makes no sense to have it.
203 // eslint-disable-next-line no-param-reassign
204
205 delete newMap.file; // eslint-disable-next-line no-param-reassign
206
207 newMap.sourceRoot = ""; // `less` returns POSIX paths, that's why we need to transform them back to native paths.
208 // eslint-disable-next-line no-param-reassign
209
210 newMap.sources = newMap.sources.map(source => _path.default.normalize(source));
211 return newMap;
212}
213
214function getLessImplementation(loaderContext, implementation) {
215 let resolvedImplementation = implementation;
216
217 if (!implementation || typeof implementation === "string") {
218 const lessImplPkg = implementation || "less";
219
220 try {
221 // eslint-disable-next-line import/no-dynamic-require, global-require
222 resolvedImplementation = require(lessImplPkg);
223 } catch (error) {
224 loaderContext.emitError(error); // eslint-disable-next-line consistent-return
225
226 return;
227 }
228 } // eslint-disable-next-line consistent-return
229
230
231 return resolvedImplementation;
232}
Note: See TracBrowser for help on using the repository browser.