Skip to content

Commit

Permalink
fix: allow to import CSS using @use with css extension (#1254)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Feb 14, 2025
1 parent 115b9a0 commit 3352e49
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,9 @@ function getPossibleRequests(
// - imports that have media queries.
//
// The `node-sass` package sends `@import` ending on `.css` to importer, it is bug, so we skip resolve
// Also sass outputs as is `@import "style.css"`, but `@use "style.css"` should include CSS content
if (extension === ".css") {
return [];
return fromImport ? [] : [url];
}

const dirname = path.dirname(request);
Expand Down Expand Up @@ -710,7 +711,12 @@ function getWebpackImporter(loaderContext, implementation, includePaths) {
return function importer(originalUrl, prev, done) {
const { fromImport } = this;

resolve(prev, originalUrl, fromImport)
resolve(
prev,
originalUrl,
// For `node-sass`
typeof fromImport === "undefined" ? true : fromImport,
)
.then((result) => {
// Add the result as dependency.
// Although we're also using stats.includedFiles, this might come in handy when an error occurs.
Expand Down
48 changes: 48 additions & 0 deletions test/__snapshots__/loader.test.js.no-node-sass.snap
Original file line number Diff line number Diff line change
Expand Up @@ -21303,6 +21303,10 @@ exports[`loader should work when "@use" at-rules with extensions ('dart-sass', '

.example-5 {
color: aquamarine;
}

.some-css-module {
background: hotpink;
}"
`;

Expand All @@ -21325,6 +21329,10 @@ exports[`loader should work when "@use" at-rules with extensions ('dart-sass', '

.example-5 {
color: aquamarine;
}

.some-css-module {
background: hotpink;
}"
`;

Expand All @@ -21347,6 +21355,10 @@ exports[`loader should work when "@use" at-rules with extensions ('dart-sass', '

.example-5 {
color: aquamarine;
}

.some-css-module {
background: hotpink;
}"
`;

Expand All @@ -21369,6 +21381,10 @@ exports[`loader should work when "@use" at-rules with extensions ('dart-sass', '

.example-5 {
color: aquamarine;
}

.some-css-module {
background: hotpink;
}"
`;

Expand All @@ -21391,6 +21407,10 @@ exports[`loader should work when "@use" at-rules with extensions ('dart-sass', '

.example-5 {
color: aquamarine;
}

.some-css-module {
background: hotpink;
}"
`;

Expand All @@ -21413,6 +21433,10 @@ exports[`loader should work when "@use" at-rules with extensions ('dart-sass', '

.example-5 {
color: aquamarine;
}

.some-css-module {
background: hotpink;
}"
`;

Expand All @@ -21435,6 +21459,10 @@ exports[`loader should work when "@use" at-rules with extensions ('sass-embedded

.example-5 {
color: aquamarine;
}

.some-css-module {
background: hotpink;
}"
`;

Expand All @@ -21457,6 +21485,10 @@ exports[`loader should work when "@use" at-rules with extensions ('sass-embedded

.example-5 {
color: aquamarine;
}

.some-css-module {
background: hotpink;
}"
`;

Expand All @@ -21479,6 +21511,10 @@ exports[`loader should work when "@use" at-rules with extensions ('sass-embedded

.example-5 {
color: aquamarine;
}

.some-css-module {
background: hotpink;
}"
`;

Expand All @@ -21501,6 +21537,10 @@ exports[`loader should work when "@use" at-rules with extensions ('sass-embedded

.example-5 {
color: aquamarine;
}

.some-css-module {
background: hotpink;
}"
`;

Expand All @@ -21523,6 +21563,10 @@ exports[`loader should work when "@use" at-rules with extensions ('sass-embedded

.example-5 {
color: aquamarine;
}

.some-css-module {
background: hotpink;
}"
`;

Expand All @@ -21545,6 +21589,10 @@ exports[`loader should work when "@use" at-rules with extensions ('sass-embedded

.example-5 {
color: aquamarine;
}

.some-css-module {
background: hotpink;
}"
`;

Expand Down
48 changes: 48 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -23711,6 +23711,10 @@ exports[`loader should work when "@use" at-rules with extensions ('dart-sass', '

.example-5 {
color: aquamarine;
}

.some-css-module {
background: hotpink;
}"
`;

Expand All @@ -23733,6 +23737,10 @@ exports[`loader should work when "@use" at-rules with extensions ('dart-sass', '

.example-5 {
color: aquamarine;
}

.some-css-module {
background: hotpink;
}"
`;

Expand All @@ -23755,6 +23763,10 @@ exports[`loader should work when "@use" at-rules with extensions ('dart-sass', '

.example-5 {
color: aquamarine;
}

.some-css-module {
background: hotpink;
}"
`;

Expand All @@ -23777,6 +23789,10 @@ exports[`loader should work when "@use" at-rules with extensions ('dart-sass', '

.example-5 {
color: aquamarine;
}

.some-css-module {
background: hotpink;
}"
`;

Expand All @@ -23799,6 +23815,10 @@ exports[`loader should work when "@use" at-rules with extensions ('dart-sass', '

.example-5 {
color: aquamarine;
}

.some-css-module {
background: hotpink;
}"
`;

Expand All @@ -23821,6 +23841,10 @@ exports[`loader should work when "@use" at-rules with extensions ('dart-sass', '

.example-5 {
color: aquamarine;
}

.some-css-module {
background: hotpink;
}"
`;

Expand All @@ -23843,6 +23867,10 @@ exports[`loader should work when "@use" at-rules with extensions ('sass-embedded

.example-5 {
color: aquamarine;
}

.some-css-module {
background: hotpink;
}"
`;

Expand All @@ -23865,6 +23893,10 @@ exports[`loader should work when "@use" at-rules with extensions ('sass-embedded

.example-5 {
color: aquamarine;
}

.some-css-module {
background: hotpink;
}"
`;

Expand All @@ -23887,6 +23919,10 @@ exports[`loader should work when "@use" at-rules with extensions ('sass-embedded

.example-5 {
color: aquamarine;
}

.some-css-module {
background: hotpink;
}"
`;

Expand All @@ -23909,6 +23945,10 @@ exports[`loader should work when "@use" at-rules with extensions ('sass-embedded

.example-5 {
color: aquamarine;
}

.some-css-module {
background: hotpink;
}"
`;

Expand All @@ -23931,6 +23971,10 @@ exports[`loader should work when "@use" at-rules with extensions ('sass-embedded

.example-5 {
color: aquamarine;
}

.some-css-module {
background: hotpink;
}"
`;

Expand All @@ -23953,6 +23997,10 @@ exports[`loader should work when "@use" at-rules with extensions ('sass-embedded

.example-5 {
color: aquamarine;
}

.some-css-module {
background: hotpink;
}"
`;

Expand Down
15 changes: 14 additions & 1 deletion test/helpers/getCodeFromSass.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,10 @@ async function getCodeFromSass(testId, options, context = {}) {
testFolder,
"node_modules/webpack-export-field/dist/styles/webpack/file.scss",
);
const pathToCSSModule = path.resolve(
testFolder,
"node_modules/css/some-css-module.css",
);

// Pseudo importer for tests
const testImporter = function testImporter(url) {
Expand Down Expand Up @@ -858,7 +862,6 @@ async function getCodeFromSass(testId, options, context = {}) {
}
}
}

// eslint-disable-next-line no-param-reassign
url = url
.replace(
Expand Down Expand Up @@ -939,6 +942,15 @@ async function getCodeFromSass(testId, options, context = {}) {
.replace(/^~/, testNodeModules);
}

const fromImport =
typeof this.fromImport === "undefined" ? true : this.fromImport;

if (!fromImport && /css\/some-css-module\.css/.test(url)) {
return {
file: url.replace(/css\/some-css-module\.css/, pathToCSSModule),
};
}

return {
file: url,
};
Expand All @@ -956,6 +968,7 @@ async function getCodeFromSass(testId, options, context = {}) {
}

sassOptions.logger = { debug: () => {}, warn: () => {} };
sassOptions.silenceDeprecations = ["legacy-js-api"];

let css;
let map;
Expand Down
1 change: 1 addition & 0 deletions test/sass/use-with-extension.sass
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
@use "../sass/nested/example-5.sass" as module14
// TODO https://github.com/sass/dart-sass/issues/798#issuecomment-521229911
// @use "nested/example-6.SASS" as module15
@use 'css/some-css-module.css'
1 change: 1 addition & 0 deletions test/scss/use-with-extension.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
@use "../scss/nested/example-5.scss" as module14;
// TODO https://github.com/sass/dart-sass/issues/798#issuecomment-521229911
// @use "nested/example-6.SCSS" as module15;
@use 'css/some-css-module.css';

0 comments on commit 3352e49

Please sign in to comment.