Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(perf): avoid using klona for less options #520

Merged
merged 2 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.21.5",
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