Skip to content

Commit

Permalink
fix: Fix support for scoped dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Mar 24, 2020
1 parent 75f766d commit 543a3a5
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 5 deletions.
7 changes: 6 additions & 1 deletion lib/find-package-root.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ module.exports = memoize(
const parent = dirname(path);
if (parent === path) return null;
if (basename(parent) === "node_modules") return path;
return module.exports(parent);
return module.exports(parent)(result => {
if (result !== parent || !basename(parent).startsWith("@")) {
return result;
}
return path;
});
}
)
),
Expand Down
3 changes: 2 additions & 1 deletion lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ if (!isValue(sep)) {
throw new Error("Unsupported Node version. Please upgrade, Webmake needs at least Node v0.8");
}
const dirEndMatch = new RegExp(`(?:^|/|\\${ sep })\\.*$`, "u");
const packageNamePattern = /(@[^/]+\/[^/]+|[^@/][^/]*)(?:\/|$)/u;

const cssDeps = [
{
Expand Down Expand Up @@ -481,7 +482,7 @@ const parser = {
const org = dep.value, lScope = scope;
let filename = join(dep.value), tree, currentRequire, main, path, ext;

const [name] = filename.split(sep, 1);
const [, name] = filename.match(packageNamePattern);
return deferred.promisifySync(() => {
// If already processed, return result
if (this.modules[name]) return this.modules[name];
Expand Down
4 changes: 2 additions & 2 deletions lib/webmake.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
path.push(name);
name = '';
}
while ((dir = path.shift()) != null) {
while ((dir = path.shift()) != null) {
if (!dir || (dir === '.')) continue;
if (dir === '..') {
scope = tree.pop();
Expand Down Expand Up @@ -81,7 +81,7 @@
id = '/';
tree = [];
} else if (t !== '.') {
name = path.split('/', 1)[0];
name = path.indexOf('@') === 0 ? path.split('/', 2).join("/") : path.split('/', 1)[0];
scope = modules[name];
if (!scope) {
if (envRequire) return envRequire(fullPath);
Expand Down
2 changes: 2 additions & 0 deletions test/__playground/lib/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ exports.outerId = require("./sub/inner/inner").modId;
exports.pathOther = require("./path/other");
exports.nodeshim = require("path");
exports.json = require("./mario");
exports.externalByIndex = require("regular");
exports.scopedByIndex = require("@scope/package");
exports.modId = module.id;

try {
Expand Down
3 changes: 3 additions & 0 deletions test/__playground/node_modules/@scope/package/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/__playground/node_modules/regular/index.js

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

2 changes: 2 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ module.exports = {
a(program.pathFile.name, "path.js", "Dir/file collision: file");
a(program.pathDir.name, "path", "Dir/file collision: dir");
a(program.pathIndex.name, "path", "Dir/file collision: dir/index");
a(program.externalByIndex.name, "external-by-index", "External package by index");
a(program.scopedByIndex.name, "scoped-by-index", "External scoped package by index");

a(program.commonPathPart.id, "sub-longer-bar", "Common path part: main");
a(program.commonPathPart.sub.id, "sub-foo", "Common path part: outer");
Expand Down
3 changes: 2 additions & 1 deletion test/lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ module.exports = {
.readInput(input)(path => {
a(path, "__playground/lib/program", "Path");
a.deep(
Object.keys(parser.modules).sort(), ["__playground", "no-main", "path", "test"],
Object.keys(parser.modules).sort(),
["@scope/package", "__playground", "no-main", "path", "regular", "test"],
"Modules"
);
})
Expand Down

0 comments on commit 543a3a5

Please sign in to comment.