Skip to content

Commit

Permalink
test: resolution logic (#852)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Jun 22, 2020
1 parent 3abe3f5 commit b3ffd5b
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/getPossibleRequests.js
@@ -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
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
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
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
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
@@ -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
@@ -0,0 +1,5 @@
@import "~package-with-index";

.class {
color: blue;
}

0 comments on commit b3ffd5b

Please sign in to comment.