Skip to content

Commit

Permalink
fix(perf): avoid using klona for less options (#520)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Jun 8, 2023
1 parent 15a0b60 commit 8a63159
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@
"less": "^3.5.0 || ^4.0.0",
"webpack": "^5.0.0"
},
"dependencies": {
"klona": "^2.0.6"
},
"devDependencies": {
"@babel/cli": "^7.21.5",
"@babel/core": "^7.22.1",
Expand Down
18 changes: 8 additions & 10 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import path from "path";

import { klona } from "klona/full";

/* eslint-disable class-methods-use-this */
const trailingSlash = /[/\\]$/;

Expand Down Expand Up @@ -152,19 +150,18 @@ function createWebpackLessPlugin(loaderContext, implementation) {
}

/**
* Get the less options from the loader context and normalizes its values
* Get the `less` options from the loader context and normalizes its values
*
* @param {object} loaderContext
* @param {object} loaderOptions
* @param {object} implementation
* @returns {Object}
*/
function getLessOptions(loaderContext, loaderOptions, implementation) {
const options = klona(
const options =
typeof loaderOptions.lessOptions === "function"
? loaderOptions.lessOptions(loaderContext) || {}
: loaderOptions.lessOptions || {}
);
: loaderOptions.lessOptions || {};

const lessOptions = {
plugins: [],
Expand All @@ -174,18 +171,17 @@ function getLessOptions(loaderContext, loaderOptions, implementation) {
...options,
};

const plugins = lessOptions.plugins.slice();
const shouldUseWebpackImporter =
typeof loaderOptions.webpackImporter === "boolean"
? loaderOptions.webpackImporter
: true;

if (shouldUseWebpackImporter) {
lessOptions.plugins.unshift(
createWebpackLessPlugin(loaderContext, implementation)
);
plugins.unshift(createWebpackLessPlugin(loaderContext, implementation));
}

lessOptions.plugins.unshift({
plugins.unshift({
install(lessProcessor, pluginManager) {
// eslint-disable-next-line no-param-reassign
pluginManager.webpackLoaderContext = loaderContext;
Expand All @@ -194,6 +190,8 @@ function getLessOptions(loaderContext, loaderOptions, implementation) {
},
});

lessOptions.plugins = plugins;

return lessOptions;
}

Expand Down
10 changes: 6 additions & 4 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,17 @@ describe("loader", () => {
pluginInstalled = true;
},
};

const sourceMap = { outputSourceFiles: false };
const plugins = [testPlugin];
const testId = "./basic.less";
const compiler = await getCompiler(testId, {
lessOptions: {
plugins: [testPlugin],
},
sourceMap: true,
lessOptions: { plugins, sourceMap },
});
const stats = await compile(compiler);

expect(plugins).toHaveLength(1);
expect(sourceMap).toEqual({ outputSourceFiles: false });
expect(pluginInstalled).toBe(true);
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
Expand Down

0 comments on commit 8a63159

Please sign in to comment.