Skip to content

Commit

Permalink
Fix scoped modules (#1013)
Browse files Browse the repository at this point in the history
  • Loading branch information
fathyb authored and devongovett committed Mar 18, 2018
1 parent c469751 commit 9488fc6
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
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

0 comments on commit 9488fc6

Please sign in to comment.