From 8a6315985b63c1fbb6b31ada1824951a2d2fbaa8 Mon Sep 17 00:00:00 2001 From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> Date: Fri, 9 Jun 2023 01:04:56 +0300 Subject: [PATCH] fix(perf): avoid using `klona` for `less` options (#520) --- package.json | 3 --- src/utils.js | 18 ++++++++---------- test/loader.test.js | 10 ++++++---- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index 782a327..f1fbc93 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/utils.js b/src/utils.js index ed03364..60b32df 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,7 +1,5 @@ import path from "path"; -import { klona } from "klona/full"; - /* eslint-disable class-methods-use-this */ const trailingSlash = /[/\\]$/; @@ -152,7 +150,7 @@ 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 @@ -160,11 +158,10 @@ function createWebpackLessPlugin(loaderContext, 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: [], @@ -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; @@ -194,6 +190,8 @@ function getLessOptions(loaderContext, loaderOptions, implementation) { }, }); + lessOptions.plugins = plugins; + return lessOptions; } diff --git a/test/loader.test.js b/test/loader.test.js index 487cc54..9190e1e 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -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");