Skip to content

Commit b3ffd5b

Browse files
test: resolution logic (#852)
1 parent 3abe3f5 commit b3ffd5b

9 files changed

+99
-5
lines changed

src/getPossibleRequests.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'path';
22

3-
import utils from 'loader-utils';
3+
import { urlToRequest } from 'loader-utils';
44

55
// Examples:
66
// - ~package
@@ -9,7 +9,7 @@ import utils from 'loader-utils';
99
// - ~@org/
1010
// - ~@org/package
1111
// - ~@org/package/
12-
const matchModuleImport = /^~([^/]+|[^/]+\/|@[^/]+[/][^/]+|@[^/]+\/?|@[^/]+[/][^/]+\/)$/;
12+
const isModuleImport = /^~([^/]+|[^/]+\/|@[^/]+[/][^/]+|@[^/]+\/?|@[^/]+[/][^/]+\/)$/;
1313

1414
/**
1515
* When libsass tries to resolve an import, it uses a special algorithm.
@@ -22,10 +22,10 @@ const matchModuleImport = /^~([^/]+|[^/]+\/|@[^/]+[/][^/]+|@[^/]+\/?|@[^/]+[/][^
2222
* @returns {Array<string>}
2323
*/
2424
export default function getPossibleRequests(url, forWebpackResolver = false) {
25-
const request = utils.urlToRequest(url);
25+
const request = urlToRequest(url);
2626

2727
// In case there is module request, send this to webpack resolver
28-
if (forWebpackResolver && matchModuleImport.test(url)) {
28+
if (forWebpackResolver && isModuleImport.test(url)) {
2929
return [request, url];
3030
}
3131

src/webpackImporter.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import path from 'path';
1111
import getPossibleRequests from './getPossibleRequests';
1212

1313
const matchCss = /\.css$/i;
14+
const isModuleImport = /^~([^/]+|[^/]+\/|@[^/]+[/][^/]+|@[^/]+\/?|@[^/]+[/][^/]+\/)$/;
1415

1516
/**
1617
* Returns an importer that uses webpack's resolving algorithm.
@@ -84,7 +85,7 @@ function webpackImporter(loaderContext, includePaths) {
8485

8586
let resolutionMap = [];
8687

87-
if (includePaths.length > 0 && !isFileScheme) {
88+
if (includePaths.length > 0 && !isFileScheme && !isModuleImport.test(url)) {
8889
// The order of import precedence is as follows:
8990
//
9091
// 1. Filesystem imports relative to the base file.

test/__snapshots__/loader.test.js.snap

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,60 @@ exports[`loader should prefer "mainFiles" over "mainFields" when the field conta
292292

293293
exports[`loader should prefer "mainFiles" over "mainFields" when the field contains "js" file (node-sass) (scss): warnings 1`] = `Array []`;
294294

295+
exports[`loader should prefer "mainFiles" with extension over without (dart-sass) (sass): css 1`] = `
296+
".load-me {
297+
color: red;
298+
}
299+
300+
.class {
301+
color: blue;
302+
}"
303+
`;
304+
305+
exports[`loader should prefer "mainFiles" with extension over without (dart-sass) (sass): errors 1`] = `Array []`;
306+
307+
exports[`loader should prefer "mainFiles" with extension over without (dart-sass) (sass): warnings 1`] = `Array []`;
308+
309+
exports[`loader should prefer "mainFiles" with extension over without (dart-sass) (scss): css 1`] = `
310+
".load-me {
311+
color: red;
312+
}
313+
314+
.class {
315+
color: blue;
316+
}"
317+
`;
318+
319+
exports[`loader should prefer "mainFiles" with extension over without (dart-sass) (scss): errors 1`] = `Array []`;
320+
321+
exports[`loader should prefer "mainFiles" with extension over without (dart-sass) (scss): warnings 1`] = `Array []`;
322+
323+
exports[`loader should prefer "mainFiles" with extension over without (node-sass) (sass): css 1`] = `
324+
".load-me {
325+
color: red; }
326+
327+
.class {
328+
color: blue; }
329+
"
330+
`;
331+
332+
exports[`loader should prefer "mainFiles" with extension over without (node-sass) (sass): errors 1`] = `Array []`;
333+
334+
exports[`loader should prefer "mainFiles" with extension over without (node-sass) (sass): warnings 1`] = `Array []`;
335+
336+
exports[`loader should prefer "mainFiles" with extension over without (node-sass) (scss): css 1`] = `
337+
".load-me {
338+
color: red; }
339+
340+
.class {
341+
color: blue; }
342+
"
343+
`;
344+
345+
exports[`loader should prefer "mainFiles" with extension over without (node-sass) (scss): errors 1`] = `Array []`;
346+
347+
exports[`loader should prefer "mainFiles" with extension over without (node-sass) (scss): warnings 1`] = `Array []`;
348+
295349
exports[`loader should respect resolving directory with the "index" file from "process.cwd()" (dart-sass) (sass): css 1`] = `
296350
".dir-with-underscore-index {
297351
color: red;

test/helpers/getCodeFromSass.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,10 @@ function getCodeFromSass(testId, options) {
256256
testFolder,
257257
'node_modules/package-with-js-main-field/index.scss'
258258
);
259+
const pathToPackageWithIndex = path.resolve(
260+
testFolder,
261+
'node_modules/package-with-index/_index.scss'
262+
);
259263
const pathToLanguage = isSass
260264
? path.resolve(testFolder, 'sass/language.sass')
261265
: path.resolve(testFolder, 'scss/language.scss');
@@ -718,6 +722,7 @@ function getCodeFromSass(testId, options) {
718722
pathToPackageWithJsAndCssMainFiles
719723
)
720724
.replace(/^~package-with-js-main-field/, pathToPackageWithJsMainField)
725+
.replace(/^~package-with-index/, pathToPackageWithIndex)
721726
.replace(/^file:language/, pathToLanguage)
722727
.replace(/^~/, testNodeModules);
723728
}

test/loader.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,28 @@ describe('loader', () => {
445445
expect(getErrors(stats)).toMatchSnapshot('errors');
446446
});
447447

448+
it(`should prefer "mainFiles" with extension over without (${implementationName}) (${syntax})`, async () => {
449+
const testId = getTestId(
450+
'import-prefer-main-files-with-extension',
451+
syntax
452+
);
453+
const options = {
454+
implementation: getImplementationByName(implementationName),
455+
sassOptions: {
456+
includePaths: ['node_modules/foundation-sites/scss'],
457+
},
458+
};
459+
const compiler = getCompiler(testId, { loader: { options } });
460+
const stats = await compile(compiler);
461+
const codeFromBundle = getCodeFromBundle(stats, compiler);
462+
const codeFromSass = getCodeFromSass(testId, options);
463+
464+
expect(codeFromBundle.css).toBe(codeFromSass.css);
465+
expect(codeFromBundle.css).toMatchSnapshot('css');
466+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
467+
expect(getErrors(stats)).toMatchSnapshot('errors');
468+
});
469+
448470
it(`should work and use the "_index" file in package (${implementationName}) (${syntax})`, async () => {
449471
const testId = getTestId('import-_index', syntax);
450472
const options = {

test/node_modules/package-with-index/_index.scss

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/node_modules/package-with-index/index

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@import "~package-with-index"
2+
3+
.class
4+
color: blue
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@import "~package-with-index";
2+
3+
.class {
4+
color: blue;
5+
}

0 commit comments

Comments
 (0)