less-loader 12.3.3 → 13.1.0
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/README.md +135 -59
- package/dist/{index.js → cjs/index.js} +50 -15
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/utils.js +388 -0
- package/dist/esm/index.js +134 -0
- package/dist/esm/options.json +67 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/utils.js +379 -0
- package/package.json +51 -35
- package/types/index.d.ts +21 -0
- package/types/utils.d.ts +112 -0
- package/dist/cjs.js +0 -3
- package/dist/utils.js +0 -216
- /package/dist/{options.json → cjs/options.json} +0 -0
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import url from "node:url";
|
|
3
|
+
|
|
4
|
+
/** @typedef {import("webpack").LoaderContext<LoaderOptions>} LoaderContext */
|
|
5
|
+
/** @typedef {import("less")} Less */
|
|
6
|
+
|
|
7
|
+
/** @typedef {Parameters<Less["render"]>[1]} LessOptions */
|
|
8
|
+
/** @typedef {NonNullable<LessOptions["plugins"]>[number]} LessPlugin */
|
|
9
|
+
/** @typedef {Less["FileManager"]} LessFileManager */
|
|
10
|
+
/** @typedef {InstanceType<Less["PluginManager"]>} PluginManager */
|
|
11
|
+
/** @typedef {InstanceType<LessFileManager>} LessFileManagerInstance */
|
|
12
|
+
/** @typedef {Parameters<InstanceType<LessFileManager>["loadFile"]>[2]} LoadFileOptions */
|
|
13
|
+
/** @typedef {Parameters<InstanceType<LessFileManager>["loadFile"]>[3]} Environment */
|
|
14
|
+
/** @typedef {Awaited<ReturnType<LessFileManagerInstance["loadFile"]>>} LoadFileResult */
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @typedef {object} LoaderOptions
|
|
18
|
+
* @property {LessOptions | ((loaderContext: LoaderContext) => LessOptions)=} lessOptions less options
|
|
19
|
+
* @property {string | ((source: string, loaderContext: LoaderContext) => string | Promise<string>)=} additionalData additional data
|
|
20
|
+
* @property {boolean=} sourceMap true when need to generate source map, otherwise false
|
|
21
|
+
* @property {boolean | "only"=} webpackImporter true when need to use webpack importer, otherwise false
|
|
22
|
+
* @property {string | Less=} implementation implementation
|
|
23
|
+
* @property {boolean=} lessLogAsWarnOrErr true when need to log less warnings and errors as webpack warnings and errors
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @typedef {object} SourceMap
|
|
28
|
+
* @property {string=} file file
|
|
29
|
+
* @property {string=} sourceRoot source root
|
|
30
|
+
* @property {string[]} sources sources
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @typedef {Error & { type?: string, filename?: string, line?: number, column?: number, extract?: string[] }} LessError
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
const trailingSlash = /[/\\]$/;
|
|
38
|
+
|
|
39
|
+
// This somewhat changed in Less 3.x. Now the file name comes without the
|
|
40
|
+
// automatically added extension whereas the extension is passed in as `options.ext`.
|
|
41
|
+
// So, if the file name matches this regexp, we simply ignore the proposed extension.
|
|
42
|
+
const IS_SPECIAL_MODULE_IMPORT = /^~[^/]+$/;
|
|
43
|
+
|
|
44
|
+
// `[drive_letter]:\` + `\\[server]\[share_name]\`
|
|
45
|
+
const IS_NATIVE_WIN32_PATH = /^[a-z]:[/\\]|^\\\\/i;
|
|
46
|
+
|
|
47
|
+
// Examples:
|
|
48
|
+
// - ~package
|
|
49
|
+
// - ~package/
|
|
50
|
+
// - ~@org
|
|
51
|
+
// - ~@org/
|
|
52
|
+
// - ~@org/package
|
|
53
|
+
// - ~@org/package/
|
|
54
|
+
const IS_MODULE_IMPORT = /^~([^/]+|[^/]+\/|@[^/]+[/][^/]+|@[^/]+\/?|@[^/]+[/][^/]+\/)$/;
|
|
55
|
+
const MODULE_REQUEST_REGEX = /^[^?]*~/;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Creates a Less plugin that uses webpack's resolving engine that is provided by the loaderContext.
|
|
59
|
+
* @param {LoaderContext} loaderContext loader context
|
|
60
|
+
* @param {Less} implementation implementation
|
|
61
|
+
* @param {Promise<void>[]} pendingDependencyTasks pending dependency tasks
|
|
62
|
+
* @returns {LessPlugin} less plugin
|
|
63
|
+
*/
|
|
64
|
+
function createWebpackLessPlugin(loaderContext, implementation, pendingDependencyTasks) {
|
|
65
|
+
const lessOptions = /** @type {LoaderOptions} */
|
|
66
|
+
loaderContext.getOptions();
|
|
67
|
+
const resolve = loaderContext.getResolve({
|
|
68
|
+
dependencyType: "less",
|
|
69
|
+
conditionNames: ["less", "style", "..."],
|
|
70
|
+
mainFields: ["less", "style", "main", "..."],
|
|
71
|
+
mainFiles: ["index", "..."],
|
|
72
|
+
extensions: [".less", ".css"],
|
|
73
|
+
preferRelative: true
|
|
74
|
+
});
|
|
75
|
+
class WebpackFileManager extends implementation.FileManager {
|
|
76
|
+
/**
|
|
77
|
+
* @param {string} filename filename
|
|
78
|
+
* @returns {boolean} true when filename is supported, otherwise false
|
|
79
|
+
*/
|
|
80
|
+
supports(filename) {
|
|
81
|
+
if (filename[0] === "/" || IS_NATIVE_WIN32_PATH.test(filename)) {
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
if (this.isPathAbsolute(filename)) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Sync loading is used by `data-uri()` and any custom Less function
|
|
91
|
+
// (including those installed via `@plugin`). Webpack doesn't expose a
|
|
92
|
+
// sync resolver, so we fulfil the sync read by delegating to Less's
|
|
93
|
+
// default file manager (which can only handle native filesystem paths)
|
|
94
|
+
// and, in parallel, kick off an async webpack resolve so the loaded
|
|
95
|
+
// file is tracked as a webpack file dependency. Without this, webpack's
|
|
96
|
+
// persistent cache won't invalidate when a sync-loaded file changes.
|
|
97
|
+
// See https://github.com/webpack/less-loader/issues/492.
|
|
98
|
+
/**
|
|
99
|
+
* @returns {boolean} true when support sync, otherwise false
|
|
100
|
+
*/
|
|
101
|
+
supportsSync() {
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* @param {string} filename filename
|
|
107
|
+
* @param {string} currentDirectory current directory
|
|
108
|
+
* @param {LoadFileOptions} options options
|
|
109
|
+
* @param {Environment} environment environment
|
|
110
|
+
* @returns {LoadFileResult} loaded file
|
|
111
|
+
*/
|
|
112
|
+
loadFileSync(filename, currentDirectory, options, environment) {
|
|
113
|
+
// The default Less `loadFileSync` internally dispatches to
|
|
114
|
+
// `this.loadFile` with `options.syncImport = true`. Because we
|
|
115
|
+
// override `loadFile` (async), dynamic dispatch would land back in
|
|
116
|
+
// our async version and break the sync contract. Invoke the parent
|
|
117
|
+
// `loadFile` directly with the sync flag instead.
|
|
118
|
+
// @ts-expect-error bad types in less
|
|
119
|
+
const result = /** @type {LoadFileResult} */
|
|
120
|
+
super.loadFile(filename, currentDirectory, {
|
|
121
|
+
...options,
|
|
122
|
+
syncImport: true
|
|
123
|
+
}, environment);
|
|
124
|
+
if (result && result.filename) {
|
|
125
|
+
loaderContext.addDependency(path.normalize(path.isAbsolute(result.filename) ? result.filename : path.resolve(currentDirectory || ".", result.filename)));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Also try to resolve via webpack so aliases / custom resolvers can
|
|
129
|
+
// contribute dependencies. The resolved content is discarded - we
|
|
130
|
+
// only need the file path to track as a dependency.
|
|
131
|
+
pendingDependencyTasks.push(this.resolveFilename(filename, currentDirectory).then(resolved => {
|
|
132
|
+
const absoluteFilename = path.isAbsolute(resolved) ? resolved : path.resolve(".", resolved);
|
|
133
|
+
loaderContext.addDependency(path.normalize(absoluteFilename));
|
|
134
|
+
}).catch(() => {
|
|
135
|
+
// Webpack may legitimately fail to resolve paths that Less's
|
|
136
|
+
// default sync manager handled (e.g. node-style relative
|
|
137
|
+
// lookups). The sync result above is what Less consumes, so
|
|
138
|
+
// ignore the async failure.
|
|
139
|
+
}));
|
|
140
|
+
return result;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* @param {string} filename filename
|
|
145
|
+
* @param {string} currentDirectory current directory
|
|
146
|
+
* @returns {Promise<string>} resolved filename
|
|
147
|
+
*/
|
|
148
|
+
async resolveFilename(filename, currentDirectory) {
|
|
149
|
+
// Less is giving us trailing slashes, but the context should have no trailing slash
|
|
150
|
+
const context = currentDirectory.replace(trailingSlash, "");
|
|
151
|
+
let request = filename;
|
|
152
|
+
|
|
153
|
+
// A `~` makes the url an module
|
|
154
|
+
if (MODULE_REQUEST_REGEX.test(filename)) {
|
|
155
|
+
request = request.replace(MODULE_REQUEST_REGEX, "");
|
|
156
|
+
}
|
|
157
|
+
if (IS_MODULE_IMPORT.test(filename)) {
|
|
158
|
+
request = request[request.length - 1] === "/" ? request : `${request}/`;
|
|
159
|
+
}
|
|
160
|
+
return this.resolveRequests(context, [...new Set([request, filename])]);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* @param {string} context context
|
|
165
|
+
* @param {string[]} possibleRequests possible requests
|
|
166
|
+
* @returns {Promise<string>} resolved requests
|
|
167
|
+
*/
|
|
168
|
+
async resolveRequests(context, possibleRequests) {
|
|
169
|
+
if (possibleRequests.length === 0) {
|
|
170
|
+
throw new Error("No possible requests to resolve");
|
|
171
|
+
}
|
|
172
|
+
let result;
|
|
173
|
+
try {
|
|
174
|
+
result = await resolve(context, possibleRequests[0]);
|
|
175
|
+
} catch (error) {
|
|
176
|
+
const [, ...tailPossibleRequests] = possibleRequests;
|
|
177
|
+
if (tailPossibleRequests.length === 0) {
|
|
178
|
+
throw error;
|
|
179
|
+
}
|
|
180
|
+
result = await this.resolveRequests(context, tailPossibleRequests);
|
|
181
|
+
}
|
|
182
|
+
return result;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* @param {string} filename filename
|
|
187
|
+
* @param {string} currentDirectory current directory
|
|
188
|
+
* @param {LoadFileOptions} options options
|
|
189
|
+
* @param {Environment} environment environment
|
|
190
|
+
* @returns {Promise<LoadFileResult>} loaded file
|
|
191
|
+
*/
|
|
192
|
+
async loadFile(filename, currentDirectory, options, environment) {
|
|
193
|
+
let result;
|
|
194
|
+
try {
|
|
195
|
+
if (IS_SPECIAL_MODULE_IMPORT.test(filename) || lessOptions.webpackImporter === "only") {
|
|
196
|
+
const error = /** @type {LessError} */new Error("Next");
|
|
197
|
+
error.type = "Next";
|
|
198
|
+
throw error;
|
|
199
|
+
}
|
|
200
|
+
result = await super.loadFile(filename, currentDirectory, options, environment);
|
|
201
|
+
} catch (error) {
|
|
202
|
+
const lessError = /** @type {LessError} */error;
|
|
203
|
+
if (lessError.type !== "File" && lessError.type !== "Next") {
|
|
204
|
+
throw error;
|
|
205
|
+
}
|
|
206
|
+
try {
|
|
207
|
+
result = await this.resolveFilename(filename, currentDirectory);
|
|
208
|
+
} catch (err) {
|
|
209
|
+
lessError.message = `Less resolver error:\n${lessError.message}\n\n` + `Webpack resolver error details:\n${ /** @type {{ details: string }} */err.details}\n\n` + `Webpack resolver error missing:\n${ /** @type {{ missing: string }} */err.missing}\n\n`;
|
|
210
|
+
throw error;
|
|
211
|
+
}
|
|
212
|
+
loaderContext.addDependency(result);
|
|
213
|
+
return super.loadFile(result, currentDirectory, options, environment);
|
|
214
|
+
}
|
|
215
|
+
const absoluteFilename = path.isAbsolute(result.filename) ? result.filename : path.resolve(".", result.filename);
|
|
216
|
+
loaderContext.addDependency(path.normalize(absoluteFilename));
|
|
217
|
+
return result;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return {
|
|
221
|
+
/**
|
|
222
|
+
* @param {Less} less less
|
|
223
|
+
* @param {PluginManager} pluginManager plugin manager
|
|
224
|
+
*/
|
|
225
|
+
install(less, pluginManager) {
|
|
226
|
+
pluginManager.addFileManager(new WebpackFileManager());
|
|
227
|
+
},
|
|
228
|
+
minVersion: [3, 0, 0]
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Get the `less` options from the loader context and normalizes its values
|
|
234
|
+
* @param {LoaderContext} loaderContext loader context
|
|
235
|
+
* @param {LoaderOptions} loaderOptions loader options
|
|
236
|
+
* @param {Less} implementation implementation
|
|
237
|
+
* @returns {{ lessOptions: LessOptions, pendingDependencyTasks: Promise<void>[] }} implementation and pending tasks
|
|
238
|
+
*/
|
|
239
|
+
function getLessOptions(loaderContext, loaderOptions, implementation) {
|
|
240
|
+
const options = typeof loaderOptions.lessOptions === "function" ? loaderOptions.lessOptions(loaderContext) || {} : loaderOptions.lessOptions || {};
|
|
241
|
+
|
|
242
|
+
/** @type {LessOptions} */
|
|
243
|
+
const lessOptions = {
|
|
244
|
+
// @ts-expect-error bad types
|
|
245
|
+
relativeUrls: true,
|
|
246
|
+
// We need to set the filename because otherwise our WebpackFileManager will receive an undefined path for the entry;
|
|
247
|
+
// a `data:` resource has no path of its own, so it reads as a file in the loader's context
|
|
248
|
+
filename: loaderContext.resourcePath.startsWith("data:") ? path.join(loaderContext.context, "stdin.less") : loaderContext.resourcePath,
|
|
249
|
+
...options
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
// Collects async dependency-resolution promises kicked off from
|
|
253
|
+
// synchronous Less file loads (e.g. `data-uri()`, `@plugin`). The loader
|
|
254
|
+
// awaits these before completing so webpack's dependency snapshot is
|
|
255
|
+
// accurate.
|
|
256
|
+
/** @type {Promise<void>[]} */
|
|
257
|
+
const pendingDependencyTasks = [];
|
|
258
|
+
|
|
259
|
+
/** @type {LessPlugin[]} */
|
|
260
|
+
const plugins = [...(lessOptions.plugins || [])];
|
|
261
|
+
const shouldUseWebpackImporter = typeof loaderOptions.webpackImporter === "boolean" || loaderOptions.webpackImporter === "only" ? loaderOptions.webpackImporter : true;
|
|
262
|
+
if (shouldUseWebpackImporter) {
|
|
263
|
+
plugins.unshift(createWebpackLessPlugin(loaderContext, implementation, pendingDependencyTasks));
|
|
264
|
+
}
|
|
265
|
+
plugins.unshift({
|
|
266
|
+
/**
|
|
267
|
+
* @param {Less} less less
|
|
268
|
+
* @param {PluginManager} pluginManager plugin manager
|
|
269
|
+
*/
|
|
270
|
+
install(less, pluginManager) {
|
|
271
|
+
// @ts-expect-error to provide loader context into plugin
|
|
272
|
+
pluginManager.webpackLoaderContext = loaderContext;
|
|
273
|
+
// @ts-expect-error to reset it after execution
|
|
274
|
+
lessOptions.pluginManager = pluginManager;
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
lessOptions.plugins = plugins;
|
|
278
|
+
return {
|
|
279
|
+
lessOptions,
|
|
280
|
+
pendingDependencyTasks
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* @param {string} url url
|
|
286
|
+
* @returns {boolean} true when url is unsupported, otherwise false
|
|
287
|
+
*/
|
|
288
|
+
function isUnsupportedUrl(url) {
|
|
289
|
+
// Is Windows path
|
|
290
|
+
if (IS_NATIVE_WIN32_PATH.test(url)) {
|
|
291
|
+
return false;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// Scheme: https://tools.ietf.org/html/rfc3986#section-3.1
|
|
295
|
+
// Absolute URL: https://tools.ietf.org/html/rfc3986#section-4.3
|
|
296
|
+
return /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(url);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* @param {SourceMap} map map
|
|
301
|
+
* @returns {SourceMap} normalized source map
|
|
302
|
+
*/
|
|
303
|
+
function normalizeSourceMap(map) {
|
|
304
|
+
const newMap = map;
|
|
305
|
+
|
|
306
|
+
// map.file is an optional property that provides the output filename.
|
|
307
|
+
// Since we don't know the final filename in the webpack build chain yet, it makes no sense to have it.
|
|
308
|
+
|
|
309
|
+
delete newMap.file;
|
|
310
|
+
newMap.sourceRoot = "";
|
|
311
|
+
|
|
312
|
+
// `less` returns POSIX paths, that's why we need to transform them back to native paths.
|
|
313
|
+
|
|
314
|
+
newMap.sources = newMap.sources.map(source => path.normalize(source));
|
|
315
|
+
return newMap;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* @param {string} specifier specifier
|
|
320
|
+
* @returns {string} resolved specifier
|
|
321
|
+
*/
|
|
322
|
+
function normalizeImportSpecifier(specifier) {
|
|
323
|
+
if (specifier.startsWith("file:")) {
|
|
324
|
+
return specifier;
|
|
325
|
+
}
|
|
326
|
+
if (path.isAbsolute(specifier)) {
|
|
327
|
+
return url.pathToFileURL(specifier).href;
|
|
328
|
+
}
|
|
329
|
+
return specifier;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* @param {LoaderContext} loaderContext loader context
|
|
334
|
+
* @param {string | Less | undefined} implementation implementation
|
|
335
|
+
* @returns {Promise<Less>} less implementation
|
|
336
|
+
*/
|
|
337
|
+
async function getLessImplementation(loaderContext, implementation) {
|
|
338
|
+
let resolvedImplementation = implementation;
|
|
339
|
+
if (!implementation || typeof implementation === "string") {
|
|
340
|
+
const lessImplPkg = implementation || "less";
|
|
341
|
+
const imported = await import(normalizeImportSpecifier(lessImplPkg));
|
|
342
|
+
resolvedImplementation = imported.default ?? imported;
|
|
343
|
+
}
|
|
344
|
+
return /** @type {Less} */resolvedImplementation;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* @param {LessError} error error
|
|
349
|
+
* @returns {string[]} file excerpt
|
|
350
|
+
*/
|
|
351
|
+
function getFileExcerptIfPossible(error) {
|
|
352
|
+
if (typeof error.extract === "undefined") {
|
|
353
|
+
return [];
|
|
354
|
+
}
|
|
355
|
+
const excerpt = error.extract.slice(0, 2);
|
|
356
|
+
const column = Math.max(/** @type {number} */error.column - 1, 0);
|
|
357
|
+
if (typeof excerpt[0] === "undefined") {
|
|
358
|
+
excerpt.shift();
|
|
359
|
+
}
|
|
360
|
+
excerpt.push(`${" ".repeat(column)}^`);
|
|
361
|
+
return excerpt;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* @param {LessError} error error
|
|
366
|
+
* @returns {Error} built error
|
|
367
|
+
*/
|
|
368
|
+
function errorFactory(error) {
|
|
369
|
+
const message = ["\n", ...getFileExcerptIfPossible(error), error.message.charAt(0).toUpperCase() + error.message.slice(1), error.filename ? ` Error in ${path.normalize(error.filename)} (line ${error.line}, column ${error.column})` : ""].join("\n");
|
|
370
|
+
const obj = /** @type {Error & { stack: string | null }} */
|
|
371
|
+
new Error(message, {
|
|
372
|
+
cause: error
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
// @ts-expect-error avoid extra stack for less
|
|
376
|
+
obj.stack = null;
|
|
377
|
+
return obj;
|
|
378
|
+
}
|
|
379
|
+
export { errorFactory, getLessImplementation, getLessOptions, isUnsupportedUrl, normalizeSourceMap };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "less-loader",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "13.1.0",
|
|
4
4
|
"description": "A Less loader for webpack. Compiles Less to CSS.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"webpack",
|
|
@@ -20,56 +20,72 @@
|
|
|
20
20
|
},
|
|
21
21
|
"license": "MIT",
|
|
22
22
|
"author": "Johannes Ewald @jhnns",
|
|
23
|
-
"
|
|
23
|
+
"type": "module",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./types/index.d.ts",
|
|
27
|
+
"import": "./dist/esm/index.js",
|
|
28
|
+
"require": "./dist/cjs/index.js",
|
|
29
|
+
"default": "./dist/esm/index.js"
|
|
30
|
+
},
|
|
31
|
+
"./package.json": "./package.json"
|
|
32
|
+
},
|
|
33
|
+
"main": "./dist/cjs/index.js",
|
|
34
|
+
"module": "./dist/esm/index.js",
|
|
35
|
+
"types": "./types/index.d.ts",
|
|
24
36
|
"files": [
|
|
25
|
-
"dist"
|
|
37
|
+
"dist",
|
|
38
|
+
"types"
|
|
26
39
|
],
|
|
27
40
|
"scripts": {
|
|
28
41
|
"start": "npm run build -- -w",
|
|
29
42
|
"clean": "del-cli dist",
|
|
30
43
|
"prebuild": "npm run clean",
|
|
31
|
-
"build": "
|
|
32
|
-
"
|
|
44
|
+
"build:esm": "babel src -d dist/esm --env-name esm --copy-files --no-copy-ignored && node -e \"require('fs').writeFileSync('dist/esm/package.json','{\\\"type\\\":\\\"module\\\"}\\n')\"",
|
|
45
|
+
"build:cjs": "babel src -d dist/cjs --env-name cjs --copy-files --no-copy-ignored && node -e \"const fs=require('fs');fs.writeFileSync('dist/cjs/package.json','{\\\"type\\\":\\\"commonjs\\\"}\\n');fs.appendFileSync('dist/cjs/index.js','module.exports = exports.default;\\nmodule.exports.default = exports.default;\\n')\"",
|
|
46
|
+
"build:types": "tsc && prettier \"types/**/*.ts\" --write",
|
|
47
|
+
"build": "npm-run-all -p \"build:*\"",
|
|
33
48
|
"security": "npm audit --production",
|
|
34
|
-
"lint
|
|
35
|
-
"lint:
|
|
49
|
+
"lint": "npm-run-all -l -p \"lint:**\" && npm run fmt:check",
|
|
50
|
+
"lint:code": "eslint --cache .",
|
|
36
51
|
"lint:spelling": "cspell --cache --no-must-find-files --quiet \"**/*.*\"",
|
|
37
|
-
"lint": "
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"fix": "npm
|
|
41
|
-
"
|
|
42
|
-
"test:watch": "npm run test:only -- --watch",
|
|
43
|
-
"test:coverage": "npm run test:only -- --collectCoverageFrom=\"src/**/*.js\" --coverage",
|
|
52
|
+
"lint:types": "tsc --pretty --noEmit",
|
|
53
|
+
"fmt": "npm run fmt:check -- --write",
|
|
54
|
+
"fmt:check": "prettier --list-different --cache --ignore-unknown .",
|
|
55
|
+
"fix": "npm run fix:code && npm run fmt",
|
|
56
|
+
"fix:code": "npm run lint:code -- --fix",
|
|
44
57
|
"pretest": "npm run lint",
|
|
45
|
-
"test": "npm run test:
|
|
58
|
+
"test": "npm run test:base",
|
|
59
|
+
"test:base": "node --import ./test/setup-snapshots.js --test --test-force-exit --test-reporter=dot --test-reporter-destination=stdout --experimental-test-module-mocks",
|
|
60
|
+
"test:coverage": "npm run test:base -- --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=coverage.lcov --test-coverage-include=\"src/**/*.js\"",
|
|
46
61
|
"prepare": "husky && npm run build",
|
|
47
|
-
"
|
|
62
|
+
"version": "changeset version",
|
|
63
|
+
"release": "npm run build && changeset publish"
|
|
64
|
+
},
|
|
65
|
+
"dependencies": {
|
|
66
|
+
"@types/less": "^3.0.8"
|
|
48
67
|
},
|
|
49
68
|
"devDependencies": {
|
|
50
69
|
"@babel/cli": "^7.24.7",
|
|
51
|
-
"@babel/core": "^7.
|
|
52
|
-
"@babel/preset-env": "^7.
|
|
53
|
-
"@
|
|
54
|
-
"@
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"del": "^7.
|
|
59
|
-
"del-cli": "^5.1.0",
|
|
70
|
+
"@babel/core": "^7.29.6",
|
|
71
|
+
"@babel/preset-env": "^7.29.5",
|
|
72
|
+
"@changesets/cli": "^2.30.0",
|
|
73
|
+
"@changesets/get-github-info": "^0.8.0",
|
|
74
|
+
"@types/node": "^22.13.4",
|
|
75
|
+
"cspell": "^10.0.0",
|
|
76
|
+
"del": "^8.0.1",
|
|
77
|
+
"del-cli": "^7.0.0",
|
|
60
78
|
"eslint": "^9.32.0",
|
|
61
|
-
"eslint-config-webpack": "^4.
|
|
79
|
+
"eslint-config-webpack": "^4.9.6",
|
|
62
80
|
"husky": "^9.1.3",
|
|
63
|
-
"
|
|
64
|
-
"less": "^4.6.3",
|
|
81
|
+
"less": "^4.6.5",
|
|
65
82
|
"less-plugin-glob": "^3.0.0",
|
|
66
|
-
"lint-staged": "^
|
|
67
|
-
"memfs": "^4.
|
|
83
|
+
"lint-staged": "^17.0.5",
|
|
84
|
+
"memfs": "^4.57.2",
|
|
68
85
|
"npm-run-all": "^4.1.5",
|
|
69
|
-
"prettier": "^3.3
|
|
70
|
-
"
|
|
71
|
-
"
|
|
72
|
-
"webpack": "^5.92.1"
|
|
86
|
+
"prettier": "^3.8.3",
|
|
87
|
+
"typescript": "^6.0.3",
|
|
88
|
+
"webpack": "^5.107.0"
|
|
73
89
|
},
|
|
74
90
|
"peerDependencies": {
|
|
75
91
|
"@rspack/core": "0.x || ^1.0.0 || ^2.0.0-0",
|
|
@@ -85,6 +101,6 @@
|
|
|
85
101
|
}
|
|
86
102
|
},
|
|
87
103
|
"engines": {
|
|
88
|
-
"node": ">=
|
|
104
|
+
"node": ">= 22.11.0"
|
|
89
105
|
}
|
|
90
106
|
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export default lessLoader;
|
|
2
|
+
export type LoaderContext = import("webpack").LoaderContext<LoaderOptions>;
|
|
3
|
+
export type Schema = import("schema-utils/declarations/validate").Schema;
|
|
4
|
+
export type LoaderOptions = import("./utils.js").LoaderOptions;
|
|
5
|
+
export type LessError = import("./utils.js").LessError;
|
|
6
|
+
export type SourceMap = import("./utils.js").SourceMap;
|
|
7
|
+
/** @typedef {import("webpack").LoaderContext<LoaderOptions>} LoaderContext */
|
|
8
|
+
/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
|
|
9
|
+
/** @typedef {import("./utils.js").LoaderOptions} LoaderOptions */
|
|
10
|
+
/** @typedef {import("./utils.js").LessError} LessError */
|
|
11
|
+
/** @typedef {import("./utils.js").SourceMap} SourceMap */
|
|
12
|
+
/**
|
|
13
|
+
* Webpack loader that compiles Less to CSS.
|
|
14
|
+
* @this {LoaderContext}
|
|
15
|
+
* @param {string} content content
|
|
16
|
+
* @returns {Promise<void>} loader result
|
|
17
|
+
*/
|
|
18
|
+
declare function lessLoader(
|
|
19
|
+
this: import("webpack").LoaderContext<import("./utils.js").LoaderOptions>,
|
|
20
|
+
content: string,
|
|
21
|
+
): Promise<void>;
|
package/types/utils.d.ts
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
export type LoaderContext = import("webpack").LoaderContext<LoaderOptions>;
|
|
2
|
+
export type Less = LessStatic;
|
|
3
|
+
export type LessOptions = Parameters<Less["render"]>[1];
|
|
4
|
+
export type LessPlugin = NonNullable<LessOptions["plugins"]>[number];
|
|
5
|
+
export type LessFileManager = Less["FileManager"];
|
|
6
|
+
export type PluginManager = InstanceType<Less["PluginManager"]>;
|
|
7
|
+
export type LessFileManagerInstance = InstanceType<LessFileManager>;
|
|
8
|
+
export type LoadFileOptions = Parameters<
|
|
9
|
+
InstanceType<LessFileManager>["loadFile"]
|
|
10
|
+
>[2];
|
|
11
|
+
export type Environment = Parameters<
|
|
12
|
+
InstanceType<LessFileManager>["loadFile"]
|
|
13
|
+
>[3];
|
|
14
|
+
export type LoadFileResult = Awaited<
|
|
15
|
+
ReturnType<LessFileManagerInstance["loadFile"]>
|
|
16
|
+
>;
|
|
17
|
+
export type LoaderOptions = {
|
|
18
|
+
/**
|
|
19
|
+
* less options
|
|
20
|
+
*/
|
|
21
|
+
lessOptions?:
|
|
22
|
+
| (LessOptions | ((loaderContext: LoaderContext) => LessOptions))
|
|
23
|
+
| undefined;
|
|
24
|
+
/**
|
|
25
|
+
* additional data
|
|
26
|
+
*/
|
|
27
|
+
additionalData?:
|
|
28
|
+
| (
|
|
29
|
+
| string
|
|
30
|
+
| ((
|
|
31
|
+
source: string,
|
|
32
|
+
loaderContext: LoaderContext,
|
|
33
|
+
) => string | Promise<string>)
|
|
34
|
+
)
|
|
35
|
+
| undefined;
|
|
36
|
+
/**
|
|
37
|
+
* true when need to generate source map, otherwise false
|
|
38
|
+
*/
|
|
39
|
+
sourceMap?: boolean | undefined;
|
|
40
|
+
/**
|
|
41
|
+
* true when need to use webpack importer, otherwise false
|
|
42
|
+
*/
|
|
43
|
+
webpackImporter?: (boolean | "only") | undefined;
|
|
44
|
+
/**
|
|
45
|
+
* implementation
|
|
46
|
+
*/
|
|
47
|
+
implementation?: (string | Less) | undefined;
|
|
48
|
+
/**
|
|
49
|
+
* true when need to log less warnings and errors as webpack warnings and errors
|
|
50
|
+
*/
|
|
51
|
+
lessLogAsWarnOrErr?: boolean | undefined;
|
|
52
|
+
};
|
|
53
|
+
export type SourceMap = {
|
|
54
|
+
/**
|
|
55
|
+
* file
|
|
56
|
+
*/
|
|
57
|
+
file?: string | undefined;
|
|
58
|
+
/**
|
|
59
|
+
* source root
|
|
60
|
+
*/
|
|
61
|
+
sourceRoot?: string | undefined;
|
|
62
|
+
/**
|
|
63
|
+
* sources
|
|
64
|
+
*/
|
|
65
|
+
sources: string[];
|
|
66
|
+
};
|
|
67
|
+
export type LessError = Error & {
|
|
68
|
+
type?: string;
|
|
69
|
+
filename?: string;
|
|
70
|
+
line?: number;
|
|
71
|
+
column?: number;
|
|
72
|
+
extract?: string[];
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* @param {LessError} error error
|
|
76
|
+
* @returns {Error} built error
|
|
77
|
+
*/
|
|
78
|
+
export function errorFactory(error: LessError): Error;
|
|
79
|
+
/**
|
|
80
|
+
* @param {LoaderContext} loaderContext loader context
|
|
81
|
+
* @param {string | Less | undefined} implementation implementation
|
|
82
|
+
* @returns {Promise<Less>} less implementation
|
|
83
|
+
*/
|
|
84
|
+
export function getLessImplementation(
|
|
85
|
+
loaderContext: LoaderContext,
|
|
86
|
+
implementation: string | Less | undefined,
|
|
87
|
+
): Promise<Less>;
|
|
88
|
+
/**
|
|
89
|
+
* Get the `less` options from the loader context and normalizes its values
|
|
90
|
+
* @param {LoaderContext} loaderContext loader context
|
|
91
|
+
* @param {LoaderOptions} loaderOptions loader options
|
|
92
|
+
* @param {Less} implementation implementation
|
|
93
|
+
* @returns {{ lessOptions: LessOptions, pendingDependencyTasks: Promise<void>[] }} implementation and pending tasks
|
|
94
|
+
*/
|
|
95
|
+
export function getLessOptions(
|
|
96
|
+
loaderContext: LoaderContext,
|
|
97
|
+
loaderOptions: LoaderOptions,
|
|
98
|
+
implementation: Less,
|
|
99
|
+
): {
|
|
100
|
+
lessOptions: LessOptions;
|
|
101
|
+
pendingDependencyTasks: Promise<void>[];
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* @param {string} url url
|
|
105
|
+
* @returns {boolean} true when url is unsupported, otherwise false
|
|
106
|
+
*/
|
|
107
|
+
export function isUnsupportedUrl(url: string): boolean;
|
|
108
|
+
/**
|
|
109
|
+
* @param {SourceMap} map map
|
|
110
|
+
* @returns {SourceMap} normalized source map
|
|
111
|
+
*/
|
|
112
|
+
export function normalizeSourceMap(map: SourceMap): SourceMap;
|
package/dist/cjs.js
DELETED