Skip to content

Commit

Permalink
fix: resolution logic
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito authored Jun 22, 2020
1 parent 3d01b82 commit 2c3a23a
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function createWebpackLessPlugin(loaderContext) {
mainFields: ['less', 'style', 'main', '...'],
mainFiles: ['index', '...'],
extensions: ['.less', '.css'],
restrictions: [/\.(le|c)ss$/i],
});

class WebpackFileManager extends less.FileManager {
Expand Down
23 changes: 23 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ exports[`loader should not alter the original options object: errors 1`] = `Arra
exports[`loader should not alter the original options object: warnings 1`] = `Array []`;
exports[`loader should not resolve when resolve.restrictions no passed: errors 1`] = `
Array [
"ModuleBuildError: Module build failed (from \`replaced original path\`):
",
]
`;
exports[`loader should not resolve when resolve.restrictions no passed: warnings 1`] = `Array []`;
exports[`loader should not to disable webpack's resolver by passing an empty paths array: css 1`] = `
".img {
background: url(some/img.jpg);
Expand Down Expand Up @@ -179,6 +188,20 @@ exports[`loader should not try to resolve CSS imports with URLs: errors 1`] = `A
exports[`loader should not try to resolve CSS imports with URLs: warnings 1`] = `Array []`;
exports[`loader should prefer a main file with "less" extension over main field with js file: css 1`] = `
".modules-dir-some-module {
color: hotpink;
}
.some-class {
background: hotpink;
}
"
`;
exports[`loader should prefer a main file with "less" extension over main field with js file: errors 1`] = `Array []`;
exports[`loader should prefer a main file with "less" extension over main field with js file: warnings 1`] = `Array []`;
exports[`loader should provide a useful error message if the import could not be found: errors 1`] = `
Array [
"ModuleBuildError: Module build failed (from \`replaced original path\`):
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/import-webpack-js-package-2.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import "~some-illegal-2";

.some-class {
background: hotpink;
}
5 changes: 5 additions & 0 deletions test/fixtures/import-webpack-js-package.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import "~some-illegal";

.some-class {
background: hotpink;
}
1 change: 1 addition & 0 deletions test/fixtures/node_modules/some-illegal-2/index.js

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

3 changes: 3 additions & 0 deletions test/fixtures/node_modules/some-illegal-2/index.less

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

4 changes: 4 additions & 0 deletions test/fixtures/node_modules/some-illegal-2/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/fixtures/node_modules/some-illegal/module.js

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

3 changes: 3 additions & 0 deletions test/fixtures/node_modules/some-illegal/module.less

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

4 changes: 4 additions & 0 deletions test/fixtures/node_modules/some-illegal/package.json

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

20 changes: 20 additions & 0 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -596,4 +596,24 @@ describe('loader', () => {
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should not resolve when resolve.restrictions no passed', async () => {
const testId = './import-webpack-js-package.less';
const compiler = getCompiler(testId);
const stats = await compile(compiler);

expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should prefer a main file with "less" extension over main field with js file', async () => {
const testId = './import-webpack-js-package-2.less';
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);

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

0 comments on commit 2c3a23a

Please sign in to comment.