Skip to content

Commit 1655baf

Browse files
fix: resolution logic (#831)
1 parent fe3b33b commit 1655baf

File tree

7 files changed

+74
-27
lines changed

7 files changed

+74
-27
lines changed

src/getPossibleRequests.js

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@ const matchModuleImport = /^~([^/]+|[^/]+\/|@[^/]+[/][^/]+|@[^/]+\/?|@[^/]+[/][^
2222
*/
2323
export default function getPossibleRequests(url) {
2424
const request = utils.urlToRequest(url);
25-
// Keep in mind: ext can also be something like '.datepicker' when the true extension is omitted and the filename contains a dot.
26-
// @see https://github.com/webpack-contrib/sass-loader/issues/167
27-
const ext = path.extname(request).toLowerCase();
2825

2926
// In case there is module request, send this to webpack resolver
3027
if (matchModuleImport.test(url)) {
3128
return [request, url];
3229
}
3330

31+
// Keep in mind: ext can also be something like '.datepicker' when the true extension is omitted and the filename contains a dot.
32+
// @see https://github.com/webpack-contrib/sass-loader/issues/167
33+
const ext = path.extname(request).toLowerCase();
34+
3435
// Because @import is also defined in CSS, Sass needs a way of compiling plain CSS @imports without trying to import the files at compile time.
3536
// To accomplish this, and to ensure SCSS is as much of a superset of CSS as possible, Sass will compile any @imports with the following characteristics to plain CSS imports:
3637
// - imports where the URL ends with .css.
@@ -55,34 +56,17 @@ export default function getPossibleRequests(url) {
5556
return [`${dirname}/_${basename}`, `${dirname}/${basename}`, url];
5657
}
5758

58-
// In case there is no file extension and filename starts with `_`:
59-
//
60-
// 1. Try to resolve files with `scss`, `sass` and `css` extensions.
61-
// 2. Try to resolve directory with `_index` or `index` filename.
62-
// 3. Send the original url to webpack resolver, maybe it's alias.
63-
if (basename.startsWith('_')) {
64-
return [
65-
`${request}.scss`,
66-
`${request}.sass`,
67-
`${request}.css`,
68-
request,
69-
url,
70-
];
71-
}
72-
73-
// In case there is no file extension and filename doesn't start with `_`:
59+
// In case there is no file extension
7460
//
75-
// 1. Try to resolve file starts with `_` and with extensions
76-
// 2. Try to resolve file with extensions
77-
// 3. Try to resolve directory with `_index` or `index` filename.
78-
// 4. Send a original url to webpack resolver, maybe it is alias.
61+
// 1. Try to resolve files starts with `_` and normal with order `sass`, `scss` and `css`
62+
// 2. Send a original url to webpack resolver, maybe it is alias.
7963
return [
80-
`${dirname}/_${basename}.scss`,
8164
`${dirname}/_${basename}.sass`,
65+
`${dirname}/${basename}.sass`,
66+
`${dirname}/_${basename}.scss`,
67+
`${dirname}/${basename}.scss`,
8268
`${dirname}/_${basename}.css`,
83-
`${request}.scss`,
84-
`${request}.sass`,
85-
`${request}.css`,
69+
`${dirname}/${basename}.css`,
8670
request,
8771
url,
8872
];

test/__snapshots__/loader.test.js.snap

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,45 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`loader should load files with underscore in the name (dart-sass) (sass): css 1`] = `
4+
"a {
5+
color: red;
6+
}"
7+
`;
8+
9+
exports[`loader should load files with underscore in the name (dart-sass) (sass): errors 1`] = `Array []`;
10+
11+
exports[`loader should load files with underscore in the name (dart-sass) (sass): warnings 1`] = `Array []`;
12+
13+
exports[`loader should load files with underscore in the name (dart-sass) (scss): css 1`] = `
14+
"a {
15+
color: red;
16+
}"
17+
`;
18+
19+
exports[`loader should load files with underscore in the name (dart-sass) (scss): errors 1`] = `Array []`;
20+
21+
exports[`loader should load files with underscore in the name (dart-sass) (scss): warnings 1`] = `Array []`;
22+
23+
exports[`loader should load files with underscore in the name (node-sass) (sass): css 1`] = `
24+
"a {
25+
color: red; }
26+
"
27+
`;
28+
29+
exports[`loader should load files with underscore in the name (node-sass) (sass): errors 1`] = `Array []`;
30+
31+
exports[`loader should load files with underscore in the name (node-sass) (sass): warnings 1`] = `Array []`;
32+
33+
exports[`loader should load files with underscore in the name (node-sass) (scss): css 1`] = `
34+
"a {
35+
color: red; }
36+
"
37+
`;
38+
39+
exports[`loader should load files with underscore in the name (node-sass) (scss): errors 1`] = `Array []`;
40+
41+
exports[`loader should load files with underscore in the name (node-sass) (scss): warnings 1`] = `Array []`;
42+
343
exports[`loader should load only sass/scss files for the "mainFiles" (dart-sass) (sass): css 1`] = `
444
"a {
545
color: red;

test/loader.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,22 @@ describe('loader', () => {
639639
expect(getErrors(stats)).toMatchSnapshot('errors');
640640
});
641641

642+
it(`should load files with underscore in the name (${implementationName}) (${syntax})`, async () => {
643+
const testId = getTestId('import-underscore-file', syntax);
644+
const options = {
645+
implementation: getImplementationByName(implementationName),
646+
};
647+
const compiler = getCompiler(testId, { loader: { options } });
648+
const stats = await compile(compiler);
649+
const codeFromBundle = getCodeFromBundle(stats, compiler);
650+
const codeFromSass = getCodeFromSass(testId, options);
651+
652+
expect(codeFromBundle.css).toBe(codeFromSass.css);
653+
expect(codeFromBundle.css).toMatchSnapshot('css');
654+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
655+
expect(getErrors(stats)).toMatchSnapshot('errors');
656+
});
657+
642658
if (implementation === dartSass) {
643659
it(`should output an understandable error with a problem in "@use" (${implementationName}) (${syntax})`, async () => {
644660
const testId = getTestId('error-use', syntax);

test/sass/__file.sass

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
a
2+
color: red

test/sass/import-underscore-file.sass

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import '_file'

test/scss/__file.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
a {
2+
color: red;
3+
}

test/scss/import-underscore-file.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import '_file';

0 commit comments

Comments
 (0)