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 postcss options #658

Merged
merged 2 commits into from
Jun 9, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
"dependencies": {
"cosmiconfig": "^8.1.3",
"jiti": "^1.18.2",
"klona": "^2.0.6",
"semver": "^7.3.8"
},
"devDependencies": {
Expand Down
10 changes: 5 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,17 @@ export default async function loader(content, sourceMap, meta) {
}
}

const useSourceMap =
typeof options.sourceMap !== "undefined"
? options.sourceMap
: this.sourceMap;

const { plugins, processOptions } = await getPostcssOptions(
this,
loadedConfig,
options.postcssOptions
);

const useSourceMap =
typeof options.sourceMap !== "undefined"
? options.sourceMap
: this.sourceMap;

if (useSourceMap) {
processOptions.map = {
inline: false,
Expand Down
26 changes: 12 additions & 14 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import path from "path";
import url from "url";
import Module from "module";

import { klona } from "klona/full";
import { cosmiconfig, defaultLoaders } from "cosmiconfig";

const parentModule = module;
Expand Down Expand Up @@ -185,11 +184,9 @@ async function loadConfig(loaderContext, config, postcssOptions) {
options: postcssOptions || {},
};

result.config = result.config(api);
return { ...result, config: result.config(api) };

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very uninformed questions, but this is not the same as a deep clone :

  • Will it not create issue ?
  • Was a deep clone here an overkill in the first place ?

Thank you for your time ! You are a pillar of the Webpack ecosystem !

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it is not deep clone, but here result, contains only path, so we can use to use ..., just to avoid mutable result

Will it not create issue ?
Was a deep clone here an overkill in the first place ?

As I said above, there is no deep only, here the config property from require and path (it just a string)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok thank you so mush for the details and your time. Have a nice day !

}

result = klona(result);

return result;
}

Expand Down Expand Up @@ -330,7 +327,7 @@ async function getPostcssOptions(
loaderContext.emitError(error);
}

const processOptionsFromConfig = loadedConfig.config || {};
const processOptionsFromConfig = { ...loadedConfig.config } || {};

if (processOptionsFromConfig.from) {
processOptionsFromConfig.from = path.resolve(
Expand All @@ -346,10 +343,7 @@ async function getPostcssOptions(
);
}

// No need them for processOptions
delete processOptionsFromConfig.plugins;

const processOptionsFromOptions = klona(normalizedPostcssOptions);
const processOptionsFromOptions = { ...normalizedPostcssOptions };

if (processOptionsFromOptions.from) {
processOptionsFromOptions.from = path.resolve(
Expand All @@ -365,16 +359,20 @@ async function getPostcssOptions(
);
}

// No need them for processOptions
delete processOptionsFromOptions.config;
delete processOptionsFromOptions.plugins;
// No need `plugins` and `config` for processOptions
const { plugins: __plugins, ...optionsFromConfig } = processOptionsFromConfig;
const {
config: _config,
plugins: _plugins,
...optionsFromOptions
} = processOptionsFromOptions;

const processOptions = {
from: file,
to: file,
map: false,
...processOptionsFromConfig,
...processOptionsFromOptions,
...optionsFromConfig,
...optionsFromOptions,
};

if (typeof processOptions.parser === "string") {
Expand Down
14 changes: 14 additions & 0 deletions test/__snapshots__/postcssOptions.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,20 @@ exports[`"postcssOptions" option should work "Function" value: errors 1`] = `[]`

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

exports[`"postcssOptions" option should work and don't modify postcss options: css 1`] = `
"a { color: black }

.foo {
float: right;
}

/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZyb20uY3NzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLElBQUksYUFBYTs7QUFFakI7RUFDRSxZQUFZO0FBQ2QiLCJmaWxlIjoidG8uY3NzIiwic291cmNlc0NvbnRlbnQiOlsiYSB7IGNvbG9yOiBibGFjayB9XG5cbi5mb28ge1xuICBmbG9hdDogcmlnaHQ7XG59XG4iXX0= */"
`;

exports[`"postcssOptions" option should work and don't modify postcss options: errors 1`] = `[]`;

exports[`"postcssOptions" option should work and don't modify postcss options: warnings 1`] = `[]`;

exports[`"postcssOptions" option should work and provide API for the configuration: css 1`] = `
"a {
color: black;
Expand Down
39 changes: 39 additions & 0 deletions test/postcssOptions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -841,9 +841,48 @@ describe('"postcssOptions" option', () => {
},
});
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle("style.css", stats);

expect(codeFromBundle.css).toMatchSnapshot("css");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});

it("should work and don't modify postcss options", async () => {
const postcssOptions = {
config: path.resolve(__dirname, "./fixtures/css/plugins.config.js"),
from: "from.css",
map: {
inline: true,
},
parser: "postcss/lib/parse",
stringifier: "postcss/lib/stringify",
to: "to.css",
plugins: [require.resolve("./fixtures/plugin/new-api.plugin")],
};
const compiler = getCompiler(
"./config-scope/css/index.js",
{
postcssOptions,
},
{
devtool: "source-map",
}
);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle("style.css", stats);

expect(postcssOptions).toEqual({
config: path.resolve(__dirname, "./fixtures/css/plugins.config.js"),
from: "from.css",
map: {
inline: true,
},
parser: "postcss/lib/parse",
stringifier: "postcss/lib/stringify",
to: "to.css",
plugins: [require.resolve("./fixtures/plugin/new-api.plugin")],
});
expect(codeFromBundle.css).toMatchSnapshot("css");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
Expand Down