Skip to content

Commit bffbc5e

Browse files
refactor: import from scoped npm packages (#343)
1 parent b04cb0d commit bffbc5e

File tree

6 files changed

+30
-3
lines changed

6 files changed

+30
-3
lines changed

src/createWebpackLessPlugin.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const trailingSlash = /[/\\]$/;
88
// This somewhat changed in Less 3.x. Now the file name comes without the
99
// automatically added extension whereas the extension is passed in as `options.ext`.
1010
// So, if the file name matches this regexp, we simply ignore the proposed extension.
11-
const isModuleName = /^~[^/\\]+$/;
11+
const isModuleName = /^~([^/]+|[^/]+\/|@[^/]+[/][^/]+|@[^/]+\/?|@[^/]+[/][^/]+\/)$/;
1212

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

9090
try {
91+
if (isModuleName.test(filename)) {
92+
const error = new Error();
93+
94+
error.type = 'Next';
95+
throw error;
96+
}
97+
9198
result = await super.loadFile(filename, ...args);
9299
} catch (error) {
93-
if (error.type !== 'File') {
100+
if (error.type !== 'File' && error.type !== 'Next') {
94101
loaderContext.emitError(error);
95102

96103
return Promise.reject(error);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@import "~@scope/module";
2+
@import "~@scope/css.css";
3+
4+
#it-works:extend(.modules-dir-scope-module) {
5+
margin: 10px;
6+
}

test/fixtures/node_modules/@scope/css.css

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/node_modules/@scope/module.less

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/helpers/createSpec.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@ const ignore = [
2121
];
2222
const lessReplacements = [
2323
[/~some\//g, '../node_modules/some/'],
24+
[/~@scope\//g, '../node_modules/@scope/'],
2425
[/(~)?assets\//g, '../less/'],
2526
[/~fileAlias/g, '../less/img.less'],
2627
[/~(aliased-)?some"/g, '../node_modules/some/module.less"'],
2728
];
28-
const cssReplacements = [[/\.\.\/node_modules\/some\//g, '~some/']];
29+
const cssReplacements = [
30+
[/\.\.\/node_modules\/some\//g, '~some/'],
31+
[/\.\.\/node_modules\/@scope\//g, '~@scope/'],
32+
];
2933
// Maps test ids on cli arguments
3034
const lessOptions = {
3135
'source-map': [

test/index.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ test("should resolve all imports from node_modules using webpack's resolver", as
7272
await compileAndCompare('import-webpack');
7373
});
7474

75+
test("should resolve all imports from node_modules using webpack's resolver", async () => {
76+
await compileAndCompare('import-scope');
77+
});
78+
7579
test('should add all resolved imports as dependencies, including node_modules', async () => {
7680
const dependencies = [];
7781

0 commit comments

Comments
 (0)