Skip to content

Commit 21966ee

Browse files
fix: do not crash on a custom scheme in @import/@use for the modern API
1 parent de29518 commit 21966ee

9 files changed

+77
-10
lines changed

.cspell.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"klona",
1616
"sharename",
1717
"wekbit",
18-
"commitlint"
18+
"commitlint",
19+
"bgcolor"
1920
],
2021

2122
"ignorePaths": [

lint-staged.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
module.exports = {
2-
"*": ["prettier --write --ignore-unknown", "cspell"],
2+
"*": ["prettier --write --ignore-unknown", "cspell --no-must-find-files"],
33
"*.js": ["eslint --cache --fix"],
44
};

src/index.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,16 @@ async function loader(content) {
9797

9898
// Modern API
9999
if (typeof result.loadedUrls !== "undefined") {
100-
result.loadedUrls.forEach((includedFile) => {
101-
const normalizedIncludedFile = url.fileURLToPath(includedFile);
102-
103-
// Custom `importer` can return only `contents` so includedFile will be relative
104-
if (path.isAbsolute(normalizedIncludedFile)) {
105-
this.addDependency(normalizedIncludedFile);
106-
}
107-
});
100+
result.loadedUrls
101+
.filter((url) => url.protocol === "file")
102+
.forEach((includedFile) => {
103+
const normalizedIncludedFile = url.fileURLToPath(includedFile);
104+
105+
// Custom `importer` can return only `contents` so includedFile will be relative
106+
if (path.isAbsolute(normalizedIncludedFile)) {
107+
this.addDependency(normalizedIncludedFile);
108+
}
109+
});
108110
}
109111
// Legacy API
110112
else if (

test/__snapshots__/sassOptions-option.test.js.snap

+16
Original file line numberDiff line numberDiff line change
@@ -4158,6 +4158,22 @@ exports[`sassOptions option should work when the option like "Object" ('sass-emb
41584158

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

4161+
exports[`sassOptions option should work with custom scheme import ('dart-sass', 'modern' API, 'sass' syntax): errors 1`] = `[]`;
4162+
4163+
exports[`sassOptions option should work with custom scheme import ('dart-sass', 'modern' API, 'sass' syntax): warnings 1`] = `[]`;
4164+
4165+
exports[`sassOptions option should work with custom scheme import ('dart-sass', 'modern' API, 'scss' syntax): errors 1`] = `[]`;
4166+
4167+
exports[`sassOptions option should work with custom scheme import ('dart-sass', 'modern' API, 'scss' syntax): warnings 1`] = `[]`;
4168+
4169+
exports[`sassOptions option should work with custom scheme import ('sass-embedded', 'modern' API, 'sass' syntax): errors 1`] = `[]`;
4170+
4171+
exports[`sassOptions option should work with custom scheme import ('sass-embedded', 'modern' API, 'sass' syntax): warnings 1`] = `[]`;
4172+
4173+
exports[`sassOptions option should work with custom scheme import ('sass-embedded', 'modern' API, 'scss' syntax): errors 1`] = `[]`;
4174+
4175+
exports[`sassOptions option should work with custom scheme import ('sass-embedded', 'modern' API, 'scss' syntax): warnings 1`] = `[]`;
4176+
41614177
exports[`sassOptions option should work with the "fiber" option ('dart-sass', 'legacy' API, 'sass' syntax): css 1`] = `
41624178
"@charset "UTF-8";
41634179
@import "./file.css";

test/sass/modern-vars.sass

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$color-fg: #000

test/sass/modern.sass

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@import 'modern-vars'
2+
// this is import by custom importer
3+
@import 'bgcolor:cornflowerblue'
4+
5+
a
6+
--color-fg: $color-fg

test/sassOptions-option.test.js

+33
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,39 @@ describe("sassOptions option", () => {
149149
expect(getWarnings(stats)).toMatchSnapshot("warnings");
150150
expect(getErrors(stats)).toMatchSnapshot("errors");
151151
});
152+
153+
it(`should work with custom scheme import ('${implementationName}', '${api}' API, '${syntax}' syntax)`, async () => {
154+
const testId = getTestId("modern", syntax);
155+
const options = {
156+
implementation,
157+
api,
158+
sassOptions: {
159+
// https://sass-lang.com/documentation/js-api/interfaces/Importer
160+
importers: [
161+
{
162+
canonicalize(url) {
163+
if (!url.startsWith("bgcolor:")) {
164+
return null;
165+
}
166+
167+
return new URL(url);
168+
},
169+
load(canonicalUrl) {
170+
return {
171+
contents: `body {background-color: ${canonicalUrl.pathname}}`,
172+
syntax: "scss",
173+
};
174+
},
175+
},
176+
],
177+
},
178+
};
179+
const compiler = getCompiler(testId, { loader: { options } });
180+
const stats = await compile(compiler);
181+
182+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
183+
expect(getErrors(stats)).toMatchSnapshot("errors");
184+
});
152185
} else {
153186
it(`should ignore the "file" option ('${implementationName}', '${api}' API, '${syntax}' syntax)`, async () => {
154187
const testId = getTestId("language", syntax);

test/scss/modern-vars.scss

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$color-fg: #000;

test/scss/modern.scss

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@import 'modern-vars';
2+
// this is import by custom importer
3+
@import 'bgcolor:cornflowerblue';
4+
5+
a {
6+
--color-fg: $color-fg;
7+
}

0 commit comments

Comments
 (0)