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): avoud using clona for stylus options #394

Merged
merged 1 commit 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
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
},
"dependencies": {
"fast-glob": "^3.2.12",
"klona": "^2.0.6",
"normalize-path": "^3.0.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default async function stylusLoader(source) {
const useSourceMap =
typeof options.sourceMap === "boolean" ? options.sourceMap : this.sourceMap;

if (stylusOptions.sourcemap || useSourceMap) {
if (useSourceMap || stylusOptions.sourcemap) {
styl.set(
"sourcemap",
useSourceMap
Expand Down
26 changes: 13 additions & 13 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import path from "path";

import { Parser, Compiler, Evaluator, nodes, utils } from "stylus";
import DepsResolver from "stylus/lib/visitor/deps-resolver";
import { klona } from "klona/full";
import fastGlob from "fast-glob";
import normalizePath from "normalize-path";

Expand All @@ -23,25 +22,26 @@ function isProductionLikeMode(loaderContext) {
}

function getStylusOptions(loaderContext, loaderOptions) {
const stylusOptions = klona(
const options =
typeof loaderOptions.stylusOptions === "function"
? loaderOptions.stylusOptions(loaderContext) || {}
: loaderOptions.stylusOptions || {}
);

stylusOptions.filename = loaderContext.resourcePath;
stylusOptions.dest = path.dirname(loaderContext.resourcePath);

// Keep track of imported files (used by Stylus CLI watch mode)
// eslint-disable-next-line no-underscore-dangle
stylusOptions._imports = [];
: loaderOptions.stylusOptions || {};
const stylusOptions = {
filename: loaderContext.resourcePath,
dest: path.dirname(loaderContext.resourcePath),
...options,
// Keep track of imported files (used by Stylus CLI watch mode)
// eslint-disable-next-line no-underscore-dangle
// Don't allow to override, because it is internally
_imports: [],
};

// https://github.com/stylus/stylus/issues/2119
stylusOptions.resolveURL =
typeof stylusOptions.resolveURL === "boolean" && !stylusOptions.resolveURL
? false
: typeof stylusOptions.resolveURL === "object"
? stylusOptions.resolveURL
? { ...stylusOptions.resolveURL }
: { nocheck: true };

if (
Expand Down Expand Up @@ -182,7 +182,7 @@ async function getDependencies(
seen.add(filename);

// See https://github.com/stylus/stylus/issues/2108
const newOptions = klona({ ...options, filename, cache: false });
const newOptions = { ...options, filename, cache: false };
const parser = new Parser(code, newOptions);

let ast;
Expand Down
16 changes: 16 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,22 @@ exports[`loader should work "use" option: errors 1`] = `[]`;

exports[`loader should work "use" option: warnings 1`] = `[]`;

exports[`loader should work and don't override loader options: css 1`] = `
"body {
font: 12px Helvetica, Arial, sans-serif;
}
a.button {
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}
/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImJhc2ljLnN0eWwiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBS0E7RUFDRSxNQUFtQixrQ0FBbkI7O0FBRUY7RUFQRSx1QkFBc0IsSUFBdEI7RUFDQSxvQkFBbUIsSUFBbkI7RUFDQSxlQUFjLElBQWQiLCJmaWxlIjoiYmFzaWMuY3NzIiwic291cmNlc0NvbnRlbnQiOlsiYm9yZGVyLXJhZGl1cygpXG4gIC13ZWJraXQtYm9yZGVyLXJhZGl1cyBhcmd1bWVudHNcbiAgLW1vei1ib3JkZXItcmFkaXVzIGFyZ3VtZW50c1xuICBib3JkZXItcmFkaXVzIGFyZ3VtZW50c1xuXG5ib2R5XG4gIGZvbnQgMTJweCBIZWx2ZXRpY2EsIEFyaWFsLCBzYW5zLXNlcmlmXG5cbmEuYnV0dG9uXG4gIGJvcmRlci1yYWRpdXMgNXB4Il19 */"
`;

exports[`loader should work and don't override loader options: errors 1`] = `[]`;

exports[`loader should work and don't override loader options: warnings 1`] = `[]`;

exports[`loader should work and respect the "compress" option with the "false" value: css 1`] = `
"body {
font: 12px Helvetica, Arial, sans-serif;
Expand Down
16 changes: 16 additions & 0 deletions test/__snapshots__/sourceMap-options.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,19 @@ a.button {
exports[`"sourceMap" options should not generate source maps when value is "false": errors 1`] = `[]`;

exports[`"sourceMap" options should not generate source maps when value is "false": warnings 1`] = `[]`;

exports[`"sourceMap" options should work and allow to override source maps options: css 1`] = `
"body {
font: 12px Helvetica, Arial, sans-serif;
}
a.button {
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}
/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImJhc2ljLnN0eWwiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBS0E7RUFDRSxNQUFtQixrQ0FBbkI7O0FBRUY7RUFQRSx1QkFBc0IsSUFBdEI7RUFDQSxvQkFBbUIsSUFBbkI7RUFDQSxlQUFjLElBQWQiLCJmaWxlIjoiYmFzaWMuY3NzIiwic291cmNlc0NvbnRlbnQiOlsiYm9yZGVyLXJhZGl1cygpXG4gIC13ZWJraXQtYm9yZGVyLXJhZGl1cyBhcmd1bWVudHNcbiAgLW1vei1ib3JkZXItcmFkaXVzIGFyZ3VtZW50c1xuICBib3JkZXItcmFkaXVzIGFyZ3VtZW50c1xuXG5ib2R5XG4gIGZvbnQgMTJweCBIZWx2ZXRpY2EsIEFyaWFsLCBzYW5zLXNlcmlmXG5cbmEuYnV0dG9uXG4gIGJvcmRlci1yYWRpdXMgNXB4Il19 */"
`;

exports[`"sourceMap" options should work and allow to override source maps options: errors 1`] = `[]`;

exports[`"sourceMap" options should work and allow to override source maps options: warnings 1`] = `[]`;
41 changes: 39 additions & 2 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,15 @@ describe("loader", () => {
const testId = "./shallow-deep-literal.styl";
const compiler = getCompiler(testId, {
stylusOptions: {
dest: "deep/deep-literal.css",
dest: path.resolve(__dirname, "fixtures/"),
Copy link
Member Author

Choose a reason for hiding this comment

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

Side effect after fixing logic, because developer can pass own options for source maps, we need to allow to change it too

},
});
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
const codeFromStylus = await getCodeFromStylus(testId, {
stylusOptions: {
resolveURL: { nocheck: true },
dest: "deep/deep-literal.css",
dest: path.resolve(__dirname, "fixtures/"),
},
});

Expand Down Expand Up @@ -1792,4 +1792,41 @@ describe("loader", () => {
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});

it("should work and don't override loader options", async () => {
const testId = "./basic.styl";
const stylusOptions = {
compress: false,
resolveURL: {
nocheck: false,
},
sourcemap: {
comment: true,
inline: true,
},
};
const compiler = getCompiler(
testId,
{ stylusOptions },
{ mode: "production" }
);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
const codeFromStylus = await getCodeFromStylus(testId, { stylusOptions });

expect(stylusOptions).toEqual({
compress: false,
resolveURL: {
nocheck: false,
},
sourcemap: {
comment: true,
inline: true,
},
});
expect(codeFromBundle.css).toBe(codeFromStylus.css);
expect(codeFromBundle.css).toMatchSnapshot("css");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});
});
19 changes: 19 additions & 0 deletions test/sourceMap-options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,23 @@ describe('"sourceMap" options', () => {
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});

it("should work and allow to override source maps options", async () => {
const testId = "./basic.styl";
const stylusOptions = {
sourcemap: {
comment: true,
inline: true,
},
};
const compiler = getCompiler(testId, { stylusOptions });
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
const codeFromStylus = await getCodeFromStylus(testId, { stylusOptions });

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