Skip to content

Commit f4eeff1

Browse files
authored
fix: Wrong import precedence (#557)
- Fixes problem with foundation - Fixes #556
1 parent 9409e17 commit f4eeff1

File tree

9 files changed

+29
-11
lines changed

9 files changed

+29
-11
lines changed

lib/importsToResolve.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const matchModuleImport = /^~([^\/]+|@[^\/]+[\/][^\/]+)$/g;
88
/**
99
* When libsass tries to resolve an import, it uses a special algorithm.
1010
* Since the sass-loader uses webpack to resolve the modules, we need to simulate that algorithm. This function
11-
* returns an array of import paths to try. The first entry in the array is always the original url
11+
* returns an array of import paths to try. The last entry in the array is always the original url
1212
* to enable straight-forward webpack.config aliases.
1313
*
1414
* @param {string} url
@@ -21,7 +21,7 @@ function importsToResolve(url) {
2121
const ext = path.extname(request);
2222

2323
if (matchModuleImport.test(url)) {
24-
return [url, request];
24+
return [request, url];
2525
}
2626

2727
// libsass' import algorithm works like this:
@@ -33,7 +33,7 @@ function importsToResolve(url) {
3333
return [];
3434
}
3535
if (ext === ".scss" || ext === ".sass") {
36-
return [url, request];
36+
return [request, url];
3737
}
3838

3939
// In case there is no file extension...
@@ -43,17 +43,17 @@ function importsToResolve(url) {
4343

4444
if (basename.charAt(0) === "_") {
4545
return [
46-
url,
47-
`${ request }.scss`, `${ request }.sass`, `${ request }.css`
46+
`${ request }.scss`, `${ request }.sass`, `${ request }.css`,
47+
url
4848
];
4949
}
5050

5151
const dirname = path.dirname(request);
5252

5353
return [
54-
url,
5554
`${ dirname }/_${ basename }.scss`, `${ dirname }/_${ basename }.sass`, `${ dirname }/_${ basename }.css`,
56-
`${ request }.scss`, `${ request }.sass`, `${ request }.css`
55+
`${ request }.scss`, `${ request }.sass`, `${ request }.css`,
56+
url
5757
];
5858
}
5959

test/index.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ syntaxStyles.forEach(ext => {
8282
it("should resolve aliases", () => execTest("import-alias", {}, {
8383
resolve: {
8484
alias: {
85-
"path-to-alias": path.join(__dirname, ext, "alias." + ext)
85+
"path-to-alias": path.join(__dirname, ext, "another", "alias." + ext)
8686
}
8787
}
8888
}));
@@ -205,7 +205,7 @@ describe("sass-loader", () => {
205205
sourceMap.should.not.have.property("file");
206206
sourceMap.should.have.property("sourceRoot", fakeCwd);
207207
// This number needs to be updated if imports.scss or any dependency of that changes
208-
sourceMap.sources.should.have.length(9);
208+
sourceMap.sources.should.have.length(10);
209209
sourceMap.sources.forEach(sourcePath =>
210210
fs.existsSync(path.resolve(sourceMap.sourceRoot, sourcePath))
211211
);
File renamed without changes.

test/sass/imports.sass

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,10 @@
1515
@import ~animate.css/animate
1616
/* @import url(http://example.com/something/from/the/interwebs); */
1717
@import url(http://example.com/something/from/the/interwebs);
18+
/* scoped import @import language */
19+
.scoped-imporr
20+
@import language
21+
// The local util file should take precedence over Node's util module
22+
// See https://github.com/webpack-contrib/sass-loader/issues/556
23+
/* @import util */
24+
@import util

test/sass/util.sass

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// This file has been named "util" on purpose because in a wrong import setup it would conflict with Node's util
2+
.util
3+
color: hotpink
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
a {
1+
.alias {
22
color: red;
33
}

test/scss/imports.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@
1919
.scoped-import {
2020
@import "language";
2121
}
22+
// The local util file should take precedence over Node's util module
23+
// See https://github.com/webpack-contrib/sass-loader/issues/556
24+
@import "util";

test/scss/util.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// This file has been named "util" on purpose because in a wrong import setup it would conflict with Node's util
2+
// See https://github.com/webpack-contrib/sass-loader/issues/556
3+
.util {
4+
color: hotpink;
5+
}

test/tools/createSpec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function createSpec(ext) {
1515
const testNodeModules = path.relative(basePath, path.join(testFolder, "node_modules")) + path.sep;
1616
const pathToBootstrap = path.relative(basePath, path.resolve(testFolder, "..", "node_modules", "bootstrap-sass"));
1717
const pathToScopedNpmPkg = path.relative(basePath, path.resolve(testFolder, "node_modules", "@org", "pkg", "./index.scss"));
18-
const pathToFooAlias = path.relative(basePath, path.resolve(testFolder, ext, "./alias." + ext));
18+
const pathToFooAlias = path.relative(basePath, path.resolve(testFolder, ext, "another", "alias." + ext));
1919

2020
fs.readdirSync(path.join(testFolder, ext))
2121
.filter((file) => {

0 commit comments

Comments
 (0)