Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix scoped modules #1013

Merged
merged 2 commits into from
Mar 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/Resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class Resolver {
return {path: builtins[filename]};
}

let parts = filename.split(path.sep);
let parts = this.getModuleParts(filename);
let root = path.parse(dir).root;

while (dir !== root) {
Expand Down Expand Up @@ -298,7 +298,7 @@ class Resolver {
alias = aliases[filename];
if (alias == null) {
// If it didn't match, try only the module name.
let parts = filename.split(path.sep);
let parts = this.getModuleParts(filename);
alias = aliases[parts[0]];
if (typeof alias === 'string') {
// Append the filename back onto the aliased module.
Expand Down Expand Up @@ -346,6 +346,16 @@ class Resolver {
let pkg = await this.findPackage(dir);
return this.resolveAliases(filename, pkg);
}

getModuleParts(name) {
let parts = path.normalize(name).split(path.sep);
if (parts[0].charAt(0) === '@') {
// Scoped module (e.g. @scope/module). Merge the first two parts back together.
parts.splice(0, 2, `${parts[0]}/${parts[1]}`);
}

return parts;
}
}

module.exports = Resolver;
Empty file.
Empty file.

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

24 changes: 24 additions & 0 deletions test/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,30 @@ describe('resolver', function() {
);
assert.equal(resolved.pkg.name, 'foo');
});

it('should resolve a scoped module', async function() {
let resolved = await resolver.resolve(
'@scope/pkg',
path.join(rootDir, 'foo.js')
);
assert.equal(
resolved.path,
path.resolve(rootDir, 'node_modules/@scope/pkg/index.js')
);
assert.equal(resolved.pkg.name, 'scope-pkg');
});

it('should resolve a file inside a scoped module', async function() {
let resolved = await resolver.resolve(
'@scope/pkg/foo/bar',
path.join(rootDir, 'foo.js')
);
assert.equal(
resolved.path,
path.resolve(rootDir, 'node_modules/@scope/pkg/foo/bar.js')
);
assert.equal(resolved.pkg.name, 'scope-pkg');
});
});

describe('aliases', function() {
Expand Down