Skip to content

Commit

Permalink
Fix aliasing of folder relative to project folder (#1485) (#1506)
Browse files Browse the repository at this point in the history
  • Loading branch information
theodm authored and devongovett committed Jul 4, 2018
1 parent 96856ba commit 69b64cc
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 22 deletions.
40 changes: 19 additions & 21 deletions src/Resolver.js 100644 → 100755
Expand Up @@ -351,14 +351,14 @@ class Resolver {
filename = './' + filename;
}

alias = this.lookupAlias(aliases, filename);
alias = this.lookupAlias(aliases, filename, dir);
} else {
// It is a node_module. First try the entire filename as a key.
alias = aliases[filename];
alias = this.lookupAlias(aliases, filename, dir);
if (alias == null) {
// If it didn't match, try only the module name.
let parts = this.getModuleParts(filename);
alias = aliases[parts[0]];
alias = this.lookupAlias(aliases, parts[0], dir);
if (typeof alias === 'string') {
// Append the filename back onto the aliased module.
alias = path.join(alias, ...parts.slice(1));
Expand All @@ -371,32 +371,30 @@ class Resolver {
return EMPTY_SHIM;
}

// If the alias is a relative path, then resolve
// relative to the package.json directory.
if (alias && alias[0] === '.') {
return path.resolve(dir, alias);
}

// Otherwise, assume the alias is a module
return alias;
}

lookupAlias(aliases, filename) {
lookupAlias(aliases, filename, dir) {
// First, try looking up the exact filename
let alias = aliases[filename];
if (alias != null) {
return alias;
}

// Otherwise, try replacing glob keys
for (let key in aliases) {
if (isGlob(key)) {
let re = micromatch.makeRe(key, {capture: true});
if (re.test(filename)) {
return filename.replace(re, aliases[key]);
if (alias == null) {
// Otherwise, try replacing glob keys
for (let key in aliases) {
if (isGlob(key)) {
let re = micromatch.makeRe(key, {capture: true});
if (re.test(filename)) {
alias = filename.replace(re, aliases[key]);
break;
}
}
}
}

if (typeof alias === 'string') {
return this.resolveFilename(alias, dir);
}

return alias;
}

async findPackage(dir) {
Expand Down
Empty file.
6 changes: 5 additions & 1 deletion test/integration/resolver/package.json 100644 → 100755
Expand Up @@ -4,6 +4,10 @@
"alias": {
"aliased": "foo",
"aliased-file": "./bar.js",
"something": "./nested/test.js"
"something": "./nested/test.js",
"aliasedfolder": "./nested",
"aliasedabsolute": "/nested",
"foo/bar": "./bar.js",
"glob/*/*": "./nested/$2"
}
}
54 changes: 54 additions & 0 deletions test/resolver.js
Expand Up @@ -367,6 +367,60 @@ describe('resolver', function() {
assert.equal(resolved.pkg.name, 'resolver');
});

it('should apply an alias for a virtual module folder (relative to project dir)', async function() {
let resolved = await resolver.resolve(
'aliasedfolder/test.js',
path.join(rootDir, 'foo.js')
);
assert.equal(resolved.path, path.join(rootDir, 'nested', 'test.js'));
assert.equal(resolved.pkg.name, 'resolver');
});

it('should apply an alias for a virtual module folder only (relative to project dir)', async function() {
let resolved = await resolver.resolve(
'aliasedfolder',
path.join(rootDir, 'foo.js')
);
assert.equal(resolved.path, path.join(rootDir, 'nested', 'index.js'));
assert.equal(resolved.pkg.name, 'resolver');
});

it('should apply an alias for a virtual module folder (relative to root dir)', async function() {
let resolved = await resolver.resolve(
'aliasedabsolute/test.js',
path.join(rootDir, 'foo.js')
);
assert.equal(resolved.path, path.join(rootDir, 'nested', 'test.js'));
assert.equal(resolved.pkg.name, 'resolver');
});

it('should apply an alias for a virtual module folder only (relative to root dir)', async function() {
let resolved = await resolver.resolve(
'aliasedabsolute',
path.join(rootDir, 'foo.js')
);
assert.equal(resolved.path, path.join(rootDir, 'nested', 'index.js'));
assert.equal(resolved.pkg.name, 'resolver');
});

it('should apply an alias for a virtual module folder sub-path', async function() {
let resolved = await resolver.resolve(
'foo/bar',
path.join(rootDir, 'foo.js')
);
assert.equal(resolved.path, path.join(rootDir, 'bar.js'));
assert.equal(resolved.pkg.name, 'resolver');
});

it('should apply an alias for a virtual module folder glob sub-path', async function() {
let resolved = await resolver.resolve(
'glob/bar/test',
path.join(rootDir, 'foo.js')
);
assert.equal(resolved.path, path.join(rootDir, 'nested', 'test.js'));
assert.equal(resolved.pkg.name, 'resolver');
});

it('should apply an alias for a virtual module', async function() {
let resolved = await resolver.resolve(
'something',
Expand Down

0 comments on commit 69b64cc

Please sign in to comment.