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

test: resolution logic #852

Merged
merged 1 commit into from
Jun 22, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/getPossibleRequests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';

import utils from 'loader-utils';
import { urlToRequest } from 'loader-utils';

// Examples:
// - ~package
Expand All @@ -9,7 +9,7 @@ import utils from 'loader-utils';
// - ~@org/
// - ~@org/package
// - ~@org/package/
const matchModuleImport = /^~([^/]+|[^/]+\/|@[^/]+[/][^/]+|@[^/]+\/?|@[^/]+[/][^/]+\/)$/;
const isModuleImport = /^~([^/]+|[^/]+\/|@[^/]+[/][^/]+|@[^/]+\/?|@[^/]+[/][^/]+\/)$/;

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

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

Expand Down
3 changes: 2 additions & 1 deletion src/webpackImporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import path from 'path';
import getPossibleRequests from './getPossibleRequests';

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

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

let resolutionMap = [];

if (includePaths.length > 0 && !isFileScheme) {
if (includePaths.length > 0 && !isFileScheme && !isModuleImport.test(url)) {
// The order of import precedence is as follows:
//
// 1. Filesystem imports relative to the base file.
Expand Down
54 changes: 54 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,60 @@ exports[`loader should prefer "mainFiles" over "mainFields" when the field conta

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

exports[`loader should prefer "mainFiles" with extension over without (dart-sass) (sass): css 1`] = `
".load-me {
color: red;
}

.class {
color: blue;
}"
`;

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

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

exports[`loader should prefer "mainFiles" with extension over without (dart-sass) (scss): css 1`] = `
".load-me {
color: red;
}

.class {
color: blue;
}"
`;

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

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

exports[`loader should prefer "mainFiles" with extension over without (node-sass) (sass): css 1`] = `
".load-me {
color: red; }

.class {
color: blue; }
"
`;

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

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

exports[`loader should prefer "mainFiles" with extension over without (node-sass) (scss): css 1`] = `
".load-me {
color: red; }

.class {
color: blue; }
"
`;

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

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

exports[`loader should respect resolving directory with the "index" file from "process.cwd()" (dart-sass) (sass): css 1`] = `
".dir-with-underscore-index {
color: red;
Expand Down
5 changes: 5 additions & 0 deletions test/helpers/getCodeFromSass.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ function getCodeFromSass(testId, options) {
testFolder,
'node_modules/package-with-js-main-field/index.scss'
);
const pathToPackageWithIndex = path.resolve(
testFolder,
'node_modules/package-with-index/_index.scss'
);
const pathToLanguage = isSass
? path.resolve(testFolder, 'sass/language.sass')
: path.resolve(testFolder, 'scss/language.scss');
Expand Down Expand Up @@ -718,6 +722,7 @@ function getCodeFromSass(testId, options) {
pathToPackageWithJsAndCssMainFiles
)
.replace(/^~package-with-js-main-field/, pathToPackageWithJsMainField)
.replace(/^~package-with-index/, pathToPackageWithIndex)
.replace(/^file:language/, pathToLanguage)
.replace(/^~/, testNodeModules);
}
Expand Down
22 changes: 22 additions & 0 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,28 @@ describe('loader', () => {
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it(`should prefer "mainFiles" with extension over without (${implementationName}) (${syntax})`, async () => {
const testId = getTestId(
'import-prefer-main-files-with-extension',
syntax
);
const options = {
implementation: getImplementationByName(implementationName),
sassOptions: {
includePaths: ['node_modules/foundation-sites/scss'],
},
};
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');
});

it(`should work and use the "_index" file in package (${implementationName}) (${syntax})`, async () => {
const testId = getTestId('import-_index', syntax);
const options = {
Expand Down
3 changes: 3 additions & 0 deletions test/node_modules/package-with-index/_index.scss

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

Empty file.
4 changes: 4 additions & 0 deletions test/sass/import-prefer-main-files-with-extension.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@import "~package-with-index"

.class
color: blue
5 changes: 5 additions & 0 deletions test/scss/import-prefer-main-files-with-extension.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import "~package-with-index";

.class {
color: blue;
}