Skip to content

Commit e754c3f

Browse files
fix(perf): avoid using klona for postcss options (#658)
1 parent 69446c3 commit e754c3f

File tree

5 files changed

+70
-20
lines changed

5 files changed

+70
-20
lines changed

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
"dependencies": {
4848
"cosmiconfig": "^8.1.3",
4949
"jiti": "^1.18.2",
50-
"klona": "^2.0.6",
5150
"semver": "^7.3.8"
5251
},
5352
"devDependencies": {

src/index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,17 @@ export default async function loader(content, sourceMap, meta) {
7575
}
7676
}
7777

78-
const useSourceMap =
79-
typeof options.sourceMap !== "undefined"
80-
? options.sourceMap
81-
: this.sourceMap;
82-
8378
const { plugins, processOptions } = await getPostcssOptions(
8479
this,
8580
loadedConfig,
8681
options.postcssOptions
8782
);
8883

84+
const useSourceMap =
85+
typeof options.sourceMap !== "undefined"
86+
? options.sourceMap
87+
: this.sourceMap;
88+
8989
if (useSourceMap) {
9090
processOptions.map = {
9191
inline: false,

src/utils.js

+12-14
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import path from "path";
22
import url from "url";
33
import Module from "module";
44

5-
import { klona } from "klona/full";
65
import { cosmiconfig, defaultLoaders } from "cosmiconfig";
76

87
const parentModule = module;
@@ -185,11 +184,9 @@ async function loadConfig(loaderContext, config, postcssOptions) {
185184
options: postcssOptions || {},
186185
};
187186

188-
result.config = result.config(api);
187+
return { ...result, config: result.config(api) };
189188
}
190189

191-
result = klona(result);
192-
193190
return result;
194191
}
195192

@@ -330,7 +327,7 @@ async function getPostcssOptions(
330327
loaderContext.emitError(error);
331328
}
332329

333-
const processOptionsFromConfig = loadedConfig.config || {};
330+
const processOptionsFromConfig = { ...loadedConfig.config } || {};
334331

335332
if (processOptionsFromConfig.from) {
336333
processOptionsFromConfig.from = path.resolve(
@@ -346,10 +343,7 @@ async function getPostcssOptions(
346343
);
347344
}
348345

349-
// No need them for processOptions
350-
delete processOptionsFromConfig.plugins;
351-
352-
const processOptionsFromOptions = klona(normalizedPostcssOptions);
346+
const processOptionsFromOptions = { ...normalizedPostcssOptions };
353347

354348
if (processOptionsFromOptions.from) {
355349
processOptionsFromOptions.from = path.resolve(
@@ -365,16 +359,20 @@ async function getPostcssOptions(
365359
);
366360
}
367361

368-
// No need them for processOptions
369-
delete processOptionsFromOptions.config;
370-
delete processOptionsFromOptions.plugins;
362+
// No need `plugins` and `config` for processOptions
363+
const { plugins: __plugins, ...optionsFromConfig } = processOptionsFromConfig;
364+
const {
365+
config: _config,
366+
plugins: _plugins,
367+
...optionsFromOptions
368+
} = processOptionsFromOptions;
371369

372370
const processOptions = {
373371
from: file,
374372
to: file,
375373
map: false,
376-
...processOptionsFromConfig,
377-
...processOptionsFromOptions,
374+
...optionsFromConfig,
375+
...optionsFromOptions,
378376
};
379377

380378
if (typeof processOptions.parser === "string") {

test/__snapshots__/postcssOptions.test.js.snap

+14
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,20 @@ exports[`"postcssOptions" option should work "Function" value: errors 1`] = `[]`
213213

214214
exports[`"postcssOptions" option should work "Function" value: warnings 1`] = `[]`;
215215

216+
exports[`"postcssOptions" option should work and don't modify postcss options: css 1`] = `
217+
"a { color: black }
218+
219+
.foo {
220+
float: right;
221+
}
222+
223+
/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZyb20uY3NzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLElBQUksYUFBYTs7QUFFakI7RUFDRSxZQUFZO0FBQ2QiLCJmaWxlIjoidG8uY3NzIiwic291cmNlc0NvbnRlbnQiOlsiYSB7IGNvbG9yOiBibGFjayB9XG5cbi5mb28ge1xuICBmbG9hdDogcmlnaHQ7XG59XG4iXX0= */"
224+
`;
225+
226+
exports[`"postcssOptions" option should work and don't modify postcss options: errors 1`] = `[]`;
227+
228+
exports[`"postcssOptions" option should work and don't modify postcss options: warnings 1`] = `[]`;
229+
216230
exports[`"postcssOptions" option should work and provide API for the configuration: css 1`] = `
217231
"a {
218232
color: black;

test/postcssOptions.test.js

+39
Original file line numberDiff line numberDiff line change
@@ -841,9 +841,48 @@ describe('"postcssOptions" option', () => {
841841
},
842842
});
843843
const stats = await compile(compiler);
844+
const codeFromBundle = getCodeFromBundle("style.css", stats);
845+
846+
expect(codeFromBundle.css).toMatchSnapshot("css");
847+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
848+
expect(getErrors(stats)).toMatchSnapshot("errors");
849+
});
844850

851+
it("should work and don't modify postcss options", async () => {
852+
const postcssOptions = {
853+
config: path.resolve(__dirname, "./fixtures/css/plugins.config.js"),
854+
from: "from.css",
855+
map: {
856+
inline: true,
857+
},
858+
parser: "postcss/lib/parse",
859+
stringifier: "postcss/lib/stringify",
860+
to: "to.css",
861+
plugins: [require.resolve("./fixtures/plugin/new-api.plugin")],
862+
};
863+
const compiler = getCompiler(
864+
"./config-scope/css/index.js",
865+
{
866+
postcssOptions,
867+
},
868+
{
869+
devtool: "source-map",
870+
}
871+
);
872+
const stats = await compile(compiler);
845873
const codeFromBundle = getCodeFromBundle("style.css", stats);
846874

875+
expect(postcssOptions).toEqual({
876+
config: path.resolve(__dirname, "./fixtures/css/plugins.config.js"),
877+
from: "from.css",
878+
map: {
879+
inline: true,
880+
},
881+
parser: "postcss/lib/parse",
882+
stringifier: "postcss/lib/stringify",
883+
to: "to.css",
884+
plugins: [require.resolve("./fixtures/plugin/new-api.plugin")],
885+
});
847886
expect(codeFromBundle.css).toMatchSnapshot("css");
848887
expect(getWarnings(stats)).toMatchSnapshot("warnings");
849888
expect(getErrors(stats)).toMatchSnapshot("errors");

0 commit comments

Comments
 (0)