From 69b64cc76530ea038f54bda239b1d271c6a09562 Mon Sep 17 00:00:00 2001 From: theodm Date: Wed, 4 Jul 2018 10:50:28 +0200 Subject: [PATCH] Fix aliasing of folder relative to project folder (#1485) (#1506) --- src/Resolver.js | 40 ++++++++--------- test/integration/resolver/nested/index.js | 0 test/integration/resolver/package.json | 6 ++- test/resolver.js | 54 +++++++++++++++++++++++ 4 files changed, 78 insertions(+), 22 deletions(-) mode change 100644 => 100755 src/Resolver.js create mode 100644 test/integration/resolver/nested/index.js mode change 100644 => 100755 test/integration/resolver/package.json diff --git a/src/Resolver.js b/src/Resolver.js old mode 100644 new mode 100755 index d8f71c5d585..24834f58d69 --- a/src/Resolver.js +++ b/src/Resolver.js @@ -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)); @@ -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) { diff --git a/test/integration/resolver/nested/index.js b/test/integration/resolver/nested/index.js new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/integration/resolver/package.json b/test/integration/resolver/package.json old mode 100644 new mode 100755 index 7df4a9bd822..23f96e8842f --- a/test/integration/resolver/package.json +++ b/test/integration/resolver/package.json @@ -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" } } diff --git a/test/resolver.js b/test/resolver.js index 5d36bd739d2..467a569900b 100644 --- a/test/resolver.js +++ b/test/resolver.js @@ -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',