less-loader 11.1.1 → 11.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -4,69 +4,52 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  var _path = _interopRequireDefault(require("path"));
9
-
10
8
  var _options = _interopRequireDefault(require("./options.json"));
11
-
12
9
  var _utils = require("./utils");
13
-
14
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
-
16
11
  async function lessLoader(source) {
17
12
  const options = this.getOptions(_options.default);
18
13
  const callback = this.async();
19
14
  let implementation;
20
-
21
15
  try {
22
16
  implementation = (0, _utils.getLessImplementation)(this, options.implementation);
23
17
  } catch (error) {
24
18
  callback(error);
25
19
  return;
26
20
  }
27
-
28
21
  if (!implementation) {
29
22
  callback(new Error(`The Less implementation "${options.implementation}" not found`));
30
23
  return;
31
24
  }
32
-
33
25
  const lessOptions = (0, _utils.getLessOptions)(this, options, implementation);
34
26
  const useSourceMap = typeof options.sourceMap === "boolean" ? options.sourceMap : this.sourceMap;
35
-
36
27
  if (useSourceMap) {
37
28
  lessOptions.sourceMap = {
38
29
  outputSourceFiles: true
39
30
  };
40
31
  }
41
-
42
32
  let data = source;
43
-
44
33
  if (typeof options.additionalData !== "undefined") {
45
34
  data = typeof options.additionalData === "function" ? `${await options.additionalData(data, this)}` : `${options.additionalData}\n${data}`;
46
35
  }
47
-
48
36
  const logger = this.getLogger("less-loader");
49
37
  const loggerListener = {
50
38
  error(message) {
51
39
  logger.error(message);
52
40
  },
53
-
54
41
  warn(message) {
55
42
  logger.warn(message);
56
43
  },
57
-
58
44
  info(message) {
59
45
  logger.log(message);
60
46
  },
61
-
62
47
  debug(message) {
63
48
  logger.debug(message);
64
49
  }
65
-
66
50
  };
67
51
  implementation.logger.addListener(loggerListener);
68
52
  let result;
69
-
70
53
  try {
71
54
  result = await implementation.render(data, lessOptions);
72
55
  } catch (error) {
@@ -75,7 +58,6 @@ async function lessLoader(source) {
75
58
  // Ref: https://github.com/webpack-contrib/less-loader/issues/357
76
59
  this.addDependency(_path.default.normalize(error.filename));
77
60
  }
78
-
79
61
  callback((0, _utils.errorFactory)(error));
80
62
  return;
81
63
  } finally {
@@ -84,7 +66,6 @@ async function lessLoader(source) {
84
66
  delete lessOptions.pluginManager.webpackLoaderContext;
85
67
  delete lessOptions.pluginManager;
86
68
  }
87
-
88
69
  const {
89
70
  css,
90
71
  imports
@@ -92,25 +73,22 @@ async function lessLoader(source) {
92
73
  imports.forEach(item => {
93
74
  if ((0, _utils.isUnsupportedUrl)(item)) {
94
75
  return;
95
- } // `less` return forward slashes on windows when `webpack` resolver return an absolute windows path in `WebpackFileManager`
96
- // Ref: https://github.com/webpack-contrib/less-loader/issues/357
97
-
98
-
99
- const normalizedItem = _path.default.normalize(item); // Custom `importer` can return only `contents` so item will be relative
76
+ }
100
77
 
78
+ // `less` return forward slashes on windows when `webpack` resolver return an absolute windows path in `WebpackFileManager`
79
+ // Ref: https://github.com/webpack-contrib/less-loader/issues/357
80
+ const normalizedItem = _path.default.normalize(item);
101
81
 
82
+ // Custom `importer` can return only `contents` so item will be relative
102
83
  if (_path.default.isAbsolute(normalizedItem)) {
103
84
  this.addDependency(normalizedItem);
104
85
  }
105
86
  });
106
87
  let map = typeof result.map === "string" ? JSON.parse(result.map) : result.map;
107
-
108
88
  if (map && useSourceMap) {
109
89
  map = (0, _utils.normalizeSourceMap)(map, this.rootContext);
110
90
  }
111
-
112
91
  callback(null, css, map);
113
92
  }
114
-
115
93
  var _default = lessLoader;
116
94
  exports.default = _default;
package/dist/utils.js CHANGED
@@ -8,30 +8,30 @@ exports.getLessImplementation = getLessImplementation;
8
8
  exports.getLessOptions = getLessOptions;
9
9
  exports.isUnsupportedUrl = isUnsupportedUrl;
10
10
  exports.normalizeSourceMap = normalizeSourceMap;
11
-
12
11
  var _path = _interopRequireDefault(require("path"));
13
-
14
12
  var _full = require("klona/full");
15
-
16
13
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
-
18
14
  /* eslint-disable class-methods-use-this */
19
- const trailingSlash = /[/\\]$/; // This somewhat changed in Less 3.x. Now the file name comes without the
15
+ const trailingSlash = /[/\\]$/;
16
+
17
+ // This somewhat changed in Less 3.x. Now the file name comes without the
20
18
  // automatically added extension whereas the extension is passed in as `options.ext`.
21
19
  // So, if the file name matches this regexp, we simply ignore the proposed extension.
20
+ const IS_SPECIAL_MODULE_IMPORT = /^~[^/]+$/;
22
21
 
23
- const IS_SPECIAL_MODULE_IMPORT = /^~[^/]+$/; // `[drive_letter]:\` + `\\[server]\[share_name]\`
22
+ // `[drive_letter]:\` + `\\[server]\[share_name]\`
23
+ const IS_NATIVE_WIN32_PATH = /^[a-z]:[/\\]|^\\\\/i;
24
24
 
25
- const IS_NATIVE_WIN32_PATH = /^[a-z]:[/\\]|^\\\\/i; // Examples:
25
+ // Examples:
26
26
  // - ~package
27
27
  // - ~package/
28
28
  // - ~@org
29
29
  // - ~@org/
30
30
  // - ~@org/package
31
31
  // - ~@org/package/
32
-
33
32
  const IS_MODULE_IMPORT = /^~([^/]+|[^/]+\/|@[^/]+[/][^/]+|@[^/]+\/?|@[^/]+[/][^/]+\/)$/;
34
33
  const MODULE_REQUEST_REGEX = /^[^?]*~/;
34
+
35
35
  /**
36
36
  * Creates a Less plugin that uses webpack's resolving engine that is provided by the loaderContext.
37
37
  *
@@ -39,7 +39,6 @@ const MODULE_REQUEST_REGEX = /^[^?]*~/;
39
39
  * @param {object} implementation
40
40
  * @returns {LessPlugin}
41
41
  */
42
-
43
42
  function createWebpackLessPlugin(loaderContext, implementation) {
44
43
  const resolve = loaderContext.getResolve({
45
44
  dependencyType: "less",
@@ -49,108 +48,89 @@ function createWebpackLessPlugin(loaderContext, implementation) {
49
48
  extensions: [".less", ".css"],
50
49
  preferRelative: true
51
50
  });
52
-
53
51
  class WebpackFileManager extends implementation.FileManager {
54
52
  supports(filename) {
55
53
  if (filename[0] === "/" || IS_NATIVE_WIN32_PATH.test(filename)) {
56
54
  return true;
57
55
  }
58
-
59
56
  if (this.isPathAbsolute(filename)) {
60
57
  return false;
61
58
  }
62
-
63
59
  return true;
64
- } // Sync resolving is used at least by the `data-uri` function.
60
+ }
61
+
62
+ // Sync resolving is used at least by the `data-uri` function.
65
63
  // This file manager doesn't know how to do it, so let's delegate it
66
64
  // to the default file manager of Less.
67
65
  // We could probably use loaderContext.resolveSync, but it's deprecated,
68
66
  // see https://webpack.js.org/api/loaders/#this-resolvesync
69
-
70
-
71
67
  supportsSync() {
72
68
  return false;
73
69
  }
74
-
75
70
  async resolveFilename(filename, currentDirectory) {
76
71
  // Less is giving us trailing slashes, but the context should have no trailing slash
77
72
  const context = currentDirectory.replace(trailingSlash, "");
78
- let request = filename; // A `~` makes the url an module
73
+ let request = filename;
79
74
 
75
+ // A `~` makes the url an module
80
76
  if (MODULE_REQUEST_REGEX.test(filename)) {
81
77
  request = request.replace(MODULE_REQUEST_REGEX, "");
82
78
  }
83
-
84
79
  if (IS_MODULE_IMPORT.test(filename)) {
85
80
  request = request[request.length - 1] === "/" ? request : `${request}/`;
86
81
  }
87
-
88
82
  return this.resolveRequests(context, [...new Set([request, filename])]);
89
83
  }
90
-
91
84
  async resolveRequests(context, possibleRequests) {
92
85
  if (possibleRequests.length === 0) {
93
86
  return Promise.reject();
94
87
  }
95
-
96
88
  let result;
97
-
98
89
  try {
99
90
  result = await resolve(context, possibleRequests[0]);
100
91
  } catch (error) {
101
92
  const [, ...tailPossibleRequests] = possibleRequests;
102
-
103
93
  if (tailPossibleRequests.length === 0) {
104
94
  throw error;
105
95
  }
106
-
107
96
  result = await this.resolveRequests(context, tailPossibleRequests);
108
97
  }
109
-
110
98
  return result;
111
99
  }
112
-
113
100
  async loadFile(filename, ...args) {
114
101
  let result;
115
-
116
102
  try {
117
103
  if (IS_SPECIAL_MODULE_IMPORT.test(filename)) {
118
104
  const error = new Error();
119
105
  error.type = "Next";
120
106
  throw error;
121
107
  }
122
-
123
108
  result = await super.loadFile(filename, ...args);
124
109
  } catch (error) {
125
110
  if (error.type !== "File" && error.type !== "Next") {
126
111
  return Promise.reject(error);
127
112
  }
128
-
129
113
  try {
130
114
  result = await this.resolveFilename(filename, ...args);
131
115
  } catch (webpackResolveError) {
132
116
  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`;
133
117
  return Promise.reject(error);
134
118
  }
135
-
136
119
  loaderContext.addDependency(result);
137
120
  return super.loadFile(result, ...args);
138
121
  }
139
-
140
122
  loaderContext.addDependency(_path.default.normalize(result.filename));
141
123
  return result;
142
124
  }
143
-
144
125
  }
145
-
146
126
  return {
147
127
  install(lessInstance, pluginManager) {
148
128
  pluginManager.addFileManager(new WebpackFileManager());
149
129
  },
150
-
151
130
  minVersion: [3, 0, 0]
152
131
  };
153
132
  }
133
+
154
134
  /**
155
135
  * Get the less options from the loader context and normalizes its values
156
136
  *
@@ -159,8 +139,6 @@ function createWebpackLessPlugin(loaderContext, implementation) {
159
139
  * @param {object} implementation
160
140
  * @returns {Object}
161
141
  */
162
-
163
-
164
142
  function getLessOptions(loaderContext, loaderOptions, implementation) {
165
143
  const options = (0, _full.klona)(typeof loaderOptions.lessOptions === "function" ? loaderOptions.lessOptions(loaderContext) || {} : loaderOptions.lessOptions || {});
166
144
  const lessOptions = {
@@ -171,76 +149,68 @@ function getLessOptions(loaderContext, loaderOptions, implementation) {
171
149
  ...options
172
150
  };
173
151
  const shouldUseWebpackImporter = typeof loaderOptions.webpackImporter === "boolean" ? loaderOptions.webpackImporter : true;
174
-
175
152
  if (shouldUseWebpackImporter) {
176
153
  lessOptions.plugins.unshift(createWebpackLessPlugin(loaderContext, implementation));
177
154
  }
178
-
179
155
  lessOptions.plugins.unshift({
180
156
  install(lessProcessor, pluginManager) {
181
157
  // eslint-disable-next-line no-param-reassign
182
158
  pluginManager.webpackLoaderContext = loaderContext;
183
159
  lessOptions.pluginManager = pluginManager;
184
160
  }
185
-
186
161
  });
187
162
  return lessOptions;
188
163
  }
189
-
190
164
  function isUnsupportedUrl(url) {
191
165
  // Is Windows path
192
166
  if (IS_NATIVE_WIN32_PATH.test(url)) {
193
167
  return false;
194
- } // Scheme: https://tools.ietf.org/html/rfc3986#section-3.1
195
- // Absolute URL: https://tools.ietf.org/html/rfc3986#section-4.3
196
-
168
+ }
197
169
 
170
+ // Scheme: https://tools.ietf.org/html/rfc3986#section-3.1
171
+ // Absolute URL: https://tools.ietf.org/html/rfc3986#section-4.3
198
172
  return /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(url);
199
173
  }
200
-
201
174
  function normalizeSourceMap(map) {
202
- const newMap = map; // map.file is an optional property that provides the output filename.
175
+ const newMap = map;
176
+
177
+ // map.file is an optional property that provides the output filename.
203
178
  // Since we don't know the final filename in the webpack build chain yet, it makes no sense to have it.
204
179
  // eslint-disable-next-line no-param-reassign
180
+ delete newMap.file;
205
181
 
206
- delete newMap.file; // eslint-disable-next-line no-param-reassign
207
-
208
- newMap.sourceRoot = ""; // `less` returns POSIX paths, that's why we need to transform them back to native paths.
209
182
  // eslint-disable-next-line no-param-reassign
183
+ newMap.sourceRoot = "";
210
184
 
185
+ // `less` returns POSIX paths, that's why we need to transform them back to native paths.
186
+ // eslint-disable-next-line no-param-reassign
211
187
  newMap.sources = newMap.sources.map(source => _path.default.normalize(source));
212
188
  return newMap;
213
189
  }
214
-
215
190
  function getLessImplementation(loaderContext, implementation) {
216
191
  let resolvedImplementation = implementation;
217
-
218
192
  if (!implementation || typeof implementation === "string") {
219
- const lessImplPkg = implementation || "less"; // eslint-disable-next-line import/no-dynamic-require, global-require
193
+ const lessImplPkg = implementation || "less";
220
194
 
195
+ // eslint-disable-next-line import/no-dynamic-require, global-require
221
196
  resolvedImplementation = require(lessImplPkg);
222
- } // eslint-disable-next-line consistent-return
223
-
197
+ }
224
198
 
199
+ // eslint-disable-next-line consistent-return
225
200
  return resolvedImplementation;
226
201
  }
227
-
228
202
  function getFileExcerptIfPossible(error) {
229
203
  if (typeof error.extract === "undefined") {
230
204
  return [];
231
205
  }
232
-
233
206
  const excerpt = error.extract.slice(0, 2);
234
207
  const column = Math.max(error.column - 1, 0);
235
-
236
208
  if (typeof excerpt[0] === "undefined") {
237
209
  excerpt.shift();
238
210
  }
239
-
240
211
  excerpt.push(`${new Array(column).join(" ")}^`);
241
212
  return excerpt;
242
213
  }
243
-
244
214
  function errorFactory(error) {
245
215
  const message = ["\n", ...getFileExcerptIfPossible(error), error.message.charAt(0).toUpperCase() + error.message.slice(1), error.filename ? ` Error in ${_path.default.normalize(error.filename)} (line ${error.line}, column ${error.column})` : ""].join("\n");
246
216
  const obj = new Error(message, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "less-loader",
3
- "version": "11.1.1",
3
+ "version": "11.1.2",
4
4
  "description": "A Less loader for webpack. Compiles Less to CSS.",
5
5
  "license": "MIT",
6
6
  "repository": "webpack-contrib/less-loader",
@@ -45,8 +45,7 @@
45
45
  "webpack": "^5.0.0"
46
46
  },
47
47
  "dependencies": {
48
- "klona": "^2.0.6",
49
- "v": "^0.3.0"
48
+ "klona": "^2.0.6"
50
49
  },
51
50
  "devDependencies": {
52
51
  "@babel/cli": "^7.21.5",