Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolution algorithm for node-sass #866

Merged
merged 1 commit into from Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/index.js
Expand Up @@ -37,7 +37,9 @@ function loader(content) {
if (shouldUseWebpackImporter) {
const { includePaths } = sassOptions;

sassOptions.importer.push(getWebpackImporter(this, includePaths));
sassOptions.importer.push(
getWebpackImporter(this, implementation, includePaths)
);
}

const callback = this.async();
Expand Down
12 changes: 11 additions & 1 deletion src/utils.js
Expand Up @@ -267,7 +267,7 @@ const isSpecialModuleImport = /^~[^/]+$/;
// `[drive_letter]:\` + `\\[server]\[sharename]\`
const isNativeWin32Path = /^[a-zA-Z]:[/\\]|^\\\\/i;

function getWebpackImporter(loaderContext, includePaths) {
function getWebpackImporter(loaderContext, implementation, includePaths) {
function startResolving(resolutionMap) {
if (resolutionMap.length === 0) {
return Promise.reject();
Expand Down Expand Up @@ -359,6 +359,16 @@ function getWebpackImporter(loaderContext, includePaths) {
//
// Because `sass`/`node-sass` run custom importers before `3`, `4` and `5` points, we need to emulate this behavior to avoid wrong resolution.
const sassPossibleRequests = getPossibleRequests(loaderContext, request);
const isDartSass = implementation.info.includes('dart-sass');

// `node-sass` calls our importer before `1. Filesystem imports relative to the base file.`, so we need emulate this too
if (!isDartSass) {
resolutionMap = resolutionMap.concat({
resolve: sassResolve,
context: path.dirname(prev),
possibleRequests: sassPossibleRequests,
});
}

resolutionMap = resolutionMap.concat(
includePaths.map((context) => ({
Expand Down
54 changes: 54 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Expand Up @@ -346,6 +346,60 @@ exports[`loader should prefer "mainFiles" with extension over without (node-sass

exports[`loader should prefer "mainFiles" with extension over without (node-sass) (scss): warnings 1`] = `Array []`;

exports[`loader should prefer relative import (dart-sass) (sass): css 1`] = `
".class {
color: blue;
}

a {
color: red;
}"
`;

exports[`loader should prefer relative import (dart-sass) (sass): errors 1`] = `Array []`;

exports[`loader should prefer relative import (dart-sass) (sass): warnings 1`] = `Array []`;

exports[`loader should prefer relative import (dart-sass) (scss): css 1`] = `
".class {
color: blue;
}

a {
color: red;
}"
`;

exports[`loader should prefer relative import (dart-sass) (scss): errors 1`] = `Array []`;

exports[`loader should prefer relative import (dart-sass) (scss): warnings 1`] = `Array []`;

exports[`loader should prefer relative import (node-sass) (sass): css 1`] = `
".class {
color: blue; }

a {
color: red; }
"
`;

exports[`loader should prefer relative import (node-sass) (sass): errors 1`] = `Array []`;

exports[`loader should prefer relative import (node-sass) (sass): warnings 1`] = `Array []`;

exports[`loader should prefer relative import (node-sass) (scss): css 1`] = `
".class {
color: blue; }

a {
color: red; }
"
`;

exports[`loader should prefer relative import (node-sass) (scss): errors 1`] = `Array []`;

exports[`loader should prefer relative import (node-sass) (scss): warnings 1`] = `Array []`;

exports[`loader should resolve absolute paths (dart-sass) (sass): css 1`] = `
"@charset \\"UTF-8\\";
body {
Expand Down
8 changes: 8 additions & 0 deletions test/helpers/getCodeFromSass.js
Expand Up @@ -267,6 +267,10 @@ function getCodeFromSass(testId, options) {
const pathToLanguage = isSass
? path.resolve(testFolder, 'sass/language.sass')
: path.resolve(testFolder, 'scss/language.scss');
const pathToPackageWithSameImport = path.resolve(
testFolder,
'node_modules/package-with-same-import/style.scss'
);

// Pseudo importer for tests
function testImporter(url) {
Expand Down Expand Up @@ -733,6 +737,10 @@ function getCodeFromSass(testId, options) {
.replace(/^\/scss\/language.scss/, pathToLanguage)
.replace(/^file:\/\/\/scss\/language.scss/, pathToLanguage)
.replace(/^file:\/\/\/sass\/language.sass/, pathToLanguage)
.replace(
/^package-with-same-import\/style/,
pathToPackageWithSameImport
)
.replace(/^~/, testNodeModules);
}

Expand Down
16 changes: 16 additions & 0 deletions test/loader.test.js
Expand Up @@ -884,6 +884,22 @@ describe('loader', () => {
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it(`should prefer relative import (${implementationName}) (${syntax})`, async () => {
const testId = getTestId('package-with-same-import', syntax);
const options = {
implementation: getImplementationByName(implementationName),
};
const compiler = getCompiler(testId, { loader: { options } });
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
const codeFromSass = getCodeFromSass(testId, options);

expect(codeFromBundle.css).toBe(codeFromSass.css);
expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});

if (implementation === dartSass) {
it(`should output an understandable error with a problem in "@use" (${implementationName}) (${syntax})`, async () => {
const testId = getTestId('error-use', syntax);
Expand Down
11 changes: 11 additions & 0 deletions test/node_modules/package-with-same-import/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/node_modules/package-with-same-import/style.scss

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/sass/package-with-package.sass
@@ -0,0 +1 @@
@import "package-with-same-import/style"
4 changes: 4 additions & 0 deletions test/sass/package-with-same-import.sass
@@ -0,0 +1,4 @@
@import "package-with-package"

a
color: red
1 change: 1 addition & 0 deletions test/scss/package-with-package.scss
@@ -0,0 +1 @@
@import "package-with-same-import/style";
5 changes: 5 additions & 0 deletions test/scss/package-with-same-import.scss
@@ -0,0 +1,5 @@
@import "package-with-package";

a {
color: red;
}