Skip to content

Commit

Permalink
fix(perf): avoud using clona for stylus options (#394)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Jun 8, 2023
1 parent 48f1615 commit 261f70e
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 17 deletions.
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/"),
},
});
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");
});
});

0 comments on commit 261f70e

Please sign in to comment.