Skip to content

Commit 8a63159

Browse files
fix(perf): avoid using klona for less options (#520)
1 parent 15a0b60 commit 8a63159

File tree

3 files changed

+14
-17
lines changed

3 files changed

+14
-17
lines changed

package.json

-3
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@
4444
"less": "^3.5.0 || ^4.0.0",
4545
"webpack": "^5.0.0"
4646
},
47-
"dependencies": {
48-
"klona": "^2.0.6"
49-
},
5047
"devDependencies": {
5148
"@babel/cli": "^7.21.5",
5249
"@babel/core": "^7.22.1",

src/utils.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import path from "path";
22

3-
import { klona } from "klona/full";
4-
53
/* eslint-disable class-methods-use-this */
64
const trailingSlash = /[/\\]$/;
75

@@ -152,19 +150,18 @@ function createWebpackLessPlugin(loaderContext, implementation) {
152150
}
153151

154152
/**
155-
* Get the less options from the loader context and normalizes its values
153+
* Get the `less` options from the loader context and normalizes its values
156154
*
157155
* @param {object} loaderContext
158156
* @param {object} loaderOptions
159157
* @param {object} implementation
160158
* @returns {Object}
161159
*/
162160
function getLessOptions(loaderContext, loaderOptions, implementation) {
163-
const options = klona(
161+
const options =
164162
typeof loaderOptions.lessOptions === "function"
165163
? loaderOptions.lessOptions(loaderContext) || {}
166-
: loaderOptions.lessOptions || {}
167-
);
164+
: loaderOptions.lessOptions || {};
168165

169166
const lessOptions = {
170167
plugins: [],
@@ -174,18 +171,17 @@ function getLessOptions(loaderContext, loaderOptions, implementation) {
174171
...options,
175172
};
176173

174+
const plugins = lessOptions.plugins.slice();
177175
const shouldUseWebpackImporter =
178176
typeof loaderOptions.webpackImporter === "boolean"
179177
? loaderOptions.webpackImporter
180178
: true;
181179

182180
if (shouldUseWebpackImporter) {
183-
lessOptions.plugins.unshift(
184-
createWebpackLessPlugin(loaderContext, implementation)
185-
);
181+
plugins.unshift(createWebpackLessPlugin(loaderContext, implementation));
186182
}
187183

188-
lessOptions.plugins.unshift({
184+
plugins.unshift({
189185
install(lessProcessor, pluginManager) {
190186
// eslint-disable-next-line no-param-reassign
191187
pluginManager.webpackLoaderContext = loaderContext;
@@ -194,6 +190,8 @@ function getLessOptions(loaderContext, loaderOptions, implementation) {
194190
},
195191
});
196192

193+
lessOptions.plugins = plugins;
194+
197195
return lessOptions;
198196
}
199197

test/loader.test.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,17 @@ describe("loader", () => {
7070
pluginInstalled = true;
7171
},
7272
};
73-
73+
const sourceMap = { outputSourceFiles: false };
74+
const plugins = [testPlugin];
7475
const testId = "./basic.less";
7576
const compiler = await getCompiler(testId, {
76-
lessOptions: {
77-
plugins: [testPlugin],
78-
},
77+
sourceMap: true,
78+
lessOptions: { plugins, sourceMap },
7979
});
8080
const stats = await compile(compiler);
8181

82+
expect(plugins).toHaveLength(1);
83+
expect(sourceMap).toEqual({ outputSourceFiles: false });
8284
expect(pluginInstalled).toBe(true);
8385
expect(getWarnings(stats)).toMatchSnapshot("warnings");
8486
expect(getErrors(stats)).toMatchSnapshot("errors");

0 commit comments

Comments
 (0)