Skip to content

Commit 543a3a5

Browse files
committed
fix: Fix support for scoped dependencies
1 parent 75f766d commit 543a3a5

File tree

8 files changed

+22
-5
lines changed

8 files changed

+22
-5
lines changed

lib/find-package-root.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ module.exports = memoize(
1919
const parent = dirname(path);
2020
if (parent === path) return null;
2121
if (basename(parent) === "node_modules") return path;
22-
return module.exports(parent);
22+
return module.exports(parent)(result => {
23+
if (result !== parent || !basename(parent).startsWith("@")) {
24+
return result;
25+
}
26+
return path;
27+
});
2328
}
2429
)
2530
),

lib/parser.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ if (!isValue(sep)) {
3636
throw new Error("Unsupported Node version. Please upgrade, Webmake needs at least Node v0.8");
3737
}
3838
const dirEndMatch = new RegExp(`(?:^|/|\\${ sep })\\.*$`, "u");
39+
const packageNamePattern = /(@[^/]+\/[^/]+|[^@/][^/]*)(?:\/|$)/u;
3940

4041
const cssDeps = [
4142
{
@@ -481,7 +482,7 @@ const parser = {
481482
const org = dep.value, lScope = scope;
482483
let filename = join(dep.value), tree, currentRequire, main, path, ext;
483484

484-
const [name] = filename.split(sep, 1);
485+
const [, name] = filename.match(packageNamePattern);
485486
return deferred.promisifySync(() => {
486487
// If already processed, return result
487488
if (this.modules[name]) return this.modules[name];

lib/webmake.tpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
path.push(name);
3030
name = '';
3131
}
32-
while ((dir = path.shift()) != null) {
32+
while ((dir = path.shift()) != null) {
3333
if (!dir || (dir === '.')) continue;
3434
if (dir === '..') {
3535
scope = tree.pop();
@@ -81,7 +81,7 @@
8181
id = '/';
8282
tree = [];
8383
} else if (t !== '.') {
84-
name = path.split('/', 1)[0];
84+
name = path.indexOf('@') === 0 ? path.split('/', 2).join("/") : path.split('/', 1)[0];
8585
scope = modules[name];
8686
if (!scope) {
8787
if (envRequire) return envRequire(fullPath);

test/__playground/lib/program.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ exports.outerId = require("./sub/inner/inner").modId;
2121
exports.pathOther = require("./path/other");
2222
exports.nodeshim = require("path");
2323
exports.json = require("./mario");
24+
exports.externalByIndex = require("regular");
25+
exports.scopedByIndex = require("@scope/package");
2426
exports.modId = module.id;
2527

2628
try {

test/__playground/node_modules/@scope/package/index.js

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/__playground/node_modules/regular/index.js

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/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ module.exports = {
3939
a(program.pathFile.name, "path.js", "Dir/file collision: file");
4040
a(program.pathDir.name, "path", "Dir/file collision: dir");
4141
a(program.pathIndex.name, "path", "Dir/file collision: dir/index");
42+
a(program.externalByIndex.name, "external-by-index", "External package by index");
43+
a(program.scopedByIndex.name, "scoped-by-index", "External scoped package by index");
4244

4345
a(program.commonPathPart.id, "sub-longer-bar", "Common path part: main");
4446
a(program.commonPathPart.sub.id, "sub-foo", "Common path part: outer");

test/lib/parser.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ module.exports = {
1414
.readInput(input)(path => {
1515
a(path, "__playground/lib/program", "Path");
1616
a.deep(
17-
Object.keys(parser.modules).sort(), ["__playground", "no-main", "path", "test"],
17+
Object.keys(parser.modules).sort(),
18+
["@scope/package", "__playground", "no-main", "path", "regular", "test"],
1819
"Modules"
1920
);
2021
})

0 commit comments

Comments
 (0)