From 3412be4ae31fe0aea7f3214249ff159b2974788f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Thu, 11 May 2017 12:28:08 +0200 Subject: [PATCH] Support absolute path to custom extension through haste package (#3537) --- .../resolve/__tests__/resolve-test.js | 5 ++++ integration_tests/resolve/package.json | 1 + packages/jest-resolve/src/index.js | 23 +++++++++++++------ 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/integration_tests/resolve/__tests__/resolve-test.js b/integration_tests/resolve/__tests__/resolve-test.js index dca2c4f4f1e2..242e7573e2d7 100644 --- a/integration_tests/resolve/__tests__/resolve-test.js +++ b/integration_tests/resolve/__tests__/resolve-test.js @@ -38,6 +38,11 @@ test('should resolve filename..js', () => { expect(platform.extension).toBe('android.js'); }); +test('should resolve filename..js from haste package', () => { + expect(testRequire('custom-resolve/test1')).not.toThrow(); + expect(platform.extension).toBe('android.js'); +}); + test('should resolve filename.native.js', () => { expect(testRequire('../test2')).not.toThrow(); expect(platform.extension).toBe('native.js'); diff --git a/integration_tests/resolve/package.json b/integration_tests/resolve/package.json index e8e7b74eb2bb..743e0a83f202 100644 --- a/integration_tests/resolve/package.json +++ b/integration_tests/resolve/package.json @@ -1,4 +1,5 @@ { + "name": "custom-resolve", "jest": { "haste": { "platforms": ["native"], diff --git a/packages/jest-resolve/src/index.js b/packages/jest-resolve/src/index.js index 73132c4f9816..952ba4026ed2 100644 --- a/packages/jest-resolve/src/index.js +++ b/packages/jest-resolve/src/index.js @@ -138,8 +138,8 @@ class Resolver { const skipResolution = options && options.skipNodeResolution && !moduleName.includes(path.sep); - if (!skipResolution) { - module = Resolver.findNodeModule(moduleName, { + const resolveNodeModule = name => { + return Resolver.findNodeModule(name, { basedir: dirname, browser: this._options.browser, extensions, @@ -147,6 +147,10 @@ class Resolver { paths, resolver: this._options.resolver, }); + }; + + if (!skipResolution) { + module = resolveNodeModule(moduleName); if (module) { return (this._moduleNameCache[key] = module); @@ -156,12 +160,17 @@ class Resolver { // 3. Resolve "haste packages" which are `package.json` files outside of // `node_modules` folders anywhere in the file system. const parts = moduleName.split('/'); - module = this.getPackage(parts.shift()); - if (module) { + const hastePackage = this.getPackage(parts.shift()); + if (hastePackage) { try { - return (this._moduleNameCache[key] = require.resolve( - path.join.apply(path, [path.dirname(module)].concat(parts)), - )); + const module = path.join.apply( + path, + [path.dirname(hastePackage)].concat(parts), + ); + // try resolving with custom resolver first to support extensions, + // then fallback to require.resolve + return (this._moduleNameCache[key] = + resolveNodeModule(module) || require.resolve(module)); } catch (ignoredError) {} }