Skip to content

Commit

Permalink
refactor: import from scoped npm packages (#343)
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito authored Apr 22, 2020
1 parent b04cb0d commit bffbc5e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/createWebpackLessPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const trailingSlash = /[/\\]$/;
// This somewhat changed in Less 3.x. Now the file name comes without the
// automatically added extension whereas the extension is passed in as `options.ext`.
// So, if the file name matches this regexp, we simply ignore the proposed extension.
const isModuleName = /^~[^/\\]+$/;
const isModuleName = /^~([^/]+|[^/]+\/|@[^/]+[/][^/]+|@[^/]+\/?|@[^/]+[/][^/]+\/)$/;

/**
* Creates a Less plugin that uses webpack's resolving engine that is provided by the loaderContext.
Expand Down Expand Up @@ -88,9 +88,16 @@ function createWebpackLessPlugin(loaderContext) {
let result;

try {
if (isModuleName.test(filename)) {
const error = new Error();

error.type = 'Next';
throw error;
}

result = await super.loadFile(filename, ...args);
} catch (error) {
if (error.type !== 'File') {
if (error.type !== 'File' && error.type !== 'Next') {
loaderContext.emitError(error);

return Promise.reject(error);
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/less/import-scope.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@import "~@scope/module";
@import "~@scope/css.css";

#it-works:extend(.modules-dir-scope-module) {
margin: 10px;
}
3 changes: 3 additions & 0 deletions test/fixtures/node_modules/@scope/css.css

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/@scope/module.less

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

6 changes: 5 additions & 1 deletion test/helpers/createSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ const ignore = [
];
const lessReplacements = [
[/~some\//g, '../node_modules/some/'],
[/~@scope\//g, '../node_modules/@scope/'],
[/(~)?assets\//g, '../less/'],
[/~fileAlias/g, '../less/img.less'],
[/~(aliased-)?some"/g, '../node_modules/some/module.less"'],
];
const cssReplacements = [[/\.\.\/node_modules\/some\//g, '~some/']];
const cssReplacements = [
[/\.\.\/node_modules\/some\//g, '~some/'],
[/\.\.\/node_modules\/@scope\//g, '~@scope/'],
];
// Maps test ids on cli arguments
const lessOptions = {
'source-map': [
Expand Down
4 changes: 4 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ test("should resolve all imports from node_modules using webpack's resolver", as
await compileAndCompare('import-webpack');
});

test("should resolve all imports from node_modules using webpack's resolver", async () => {
await compileAndCompare('import-scope');
});

test('should add all resolved imports as dependencies, including node_modules', async () => {
const dependencies = [];

Expand Down

0 comments on commit bffbc5e

Please sign in to comment.