Skip to content

Commit

Permalink
fix: do not crash on a custom scheme in @import/@use for the mode…
Browse files Browse the repository at this point in the history
…rn API
  • Loading branch information
alexander-akait committed Mar 18, 2023
1 parent de29518 commit 21966ee
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .cspell.json
Expand Up @@ -15,7 +15,8 @@
"klona",
"sharename",
"wekbit",
"commitlint"
"commitlint",
"bgcolor"
],

"ignorePaths": [
Expand Down
2 changes: 1 addition & 1 deletion lint-staged.config.js
@@ -1,4 +1,4 @@
module.exports = {
"*": ["prettier --write --ignore-unknown", "cspell"],
"*": ["prettier --write --ignore-unknown", "cspell --no-must-find-files"],
"*.js": ["eslint --cache --fix"],
};
18 changes: 10 additions & 8 deletions src/index.js
Expand Up @@ -97,14 +97,16 @@ async function loader(content) {

// Modern API
if (typeof result.loadedUrls !== "undefined") {
result.loadedUrls.forEach((includedFile) => {
const normalizedIncludedFile = url.fileURLToPath(includedFile);

// Custom `importer` can return only `contents` so includedFile will be relative
if (path.isAbsolute(normalizedIncludedFile)) {
this.addDependency(normalizedIncludedFile);
}
});
result.loadedUrls
.filter((url) => url.protocol === "file")
.forEach((includedFile) => {
const normalizedIncludedFile = url.fileURLToPath(includedFile);

// Custom `importer` can return only `contents` so includedFile will be relative
if (path.isAbsolute(normalizedIncludedFile)) {
this.addDependency(normalizedIncludedFile);
}
});
}
// Legacy API
else if (
Expand Down
16 changes: 16 additions & 0 deletions test/__snapshots__/sassOptions-option.test.js.snap
Expand Up @@ -4158,6 +4158,22 @@ exports[`sassOptions option should work when the option like "Object" ('sass-emb

exports[`sassOptions option should work when the option like "Object" ('sass-embedded', 'modern' API, 'scss' syntax): warnings 1`] = `[]`;

exports[`sassOptions option should work with custom scheme import ('dart-sass', 'modern' API, 'sass' syntax): errors 1`] = `[]`;

exports[`sassOptions option should work with custom scheme import ('dart-sass', 'modern' API, 'sass' syntax): warnings 1`] = `[]`;

exports[`sassOptions option should work with custom scheme import ('dart-sass', 'modern' API, 'scss' syntax): errors 1`] = `[]`;

exports[`sassOptions option should work with custom scheme import ('dart-sass', 'modern' API, 'scss' syntax): warnings 1`] = `[]`;

exports[`sassOptions option should work with custom scheme import ('sass-embedded', 'modern' API, 'sass' syntax): errors 1`] = `[]`;

exports[`sassOptions option should work with custom scheme import ('sass-embedded', 'modern' API, 'sass' syntax): warnings 1`] = `[]`;

exports[`sassOptions option should work with custom scheme import ('sass-embedded', 'modern' API, 'scss' syntax): errors 1`] = `[]`;

exports[`sassOptions option should work with custom scheme import ('sass-embedded', 'modern' API, 'scss' syntax): warnings 1`] = `[]`;

exports[`sassOptions option should work with the "fiber" option ('dart-sass', 'legacy' API, 'sass' syntax): css 1`] = `
"@charset "UTF-8";
@import "./file.css";
Expand Down
1 change: 1 addition & 0 deletions test/sass/modern-vars.sass
@@ -0,0 +1 @@
$color-fg: #000
6 changes: 6 additions & 0 deletions test/sass/modern.sass
@@ -0,0 +1,6 @@
@import 'modern-vars'
// this is import by custom importer
@import 'bgcolor:cornflowerblue'

a
--color-fg: $color-fg
33 changes: 33 additions & 0 deletions test/sassOptions-option.test.js
Expand Up @@ -149,6 +149,39 @@ describe("sassOptions option", () => {
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});

it(`should work with custom scheme import ('${implementationName}', '${api}' API, '${syntax}' syntax)`, async () => {
const testId = getTestId("modern", syntax);
const options = {
implementation,
api,
sassOptions: {
// https://sass-lang.com/documentation/js-api/interfaces/Importer
importers: [
{
canonicalize(url) {
if (!url.startsWith("bgcolor:")) {
return null;
}

return new URL(url);
},
load(canonicalUrl) {
return {
contents: `body {background-color: ${canonicalUrl.pathname}}`,
syntax: "scss",
};
},
},
],
},
};
const compiler = getCompiler(testId, { loader: { options } });
const stats = await compile(compiler);

expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});
} else {
it(`should ignore the "file" option ('${implementationName}', '${api}' API, '${syntax}' syntax)`, async () => {
const testId = getTestId("language", syntax);
Expand Down
1 change: 1 addition & 0 deletions test/scss/modern-vars.scss
@@ -0,0 +1 @@
$color-fg: #000;
7 changes: 7 additions & 0 deletions test/scss/modern.scss
@@ -0,0 +1,7 @@
@import 'modern-vars';
// this is import by custom importer
@import 'bgcolor:cornflowerblue';

a {
--color-fg: $color-fg;
}

0 comments on commit 21966ee

Please sign in to comment.