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