From 2963af0befa255ef9d5edc0ab32d76e3e3173309 Mon Sep 17 00:00:00 2001 From: Mark Vayngrib Date: Mon, 25 Jan 2016 10:08:43 -0800 Subject: [PATCH] breaking test and fix for browser field mapping from package to file Summary: breaks on mappings like: ```json "browser": { "node-package": "./dir/browser.js" } ``` Closes https://github.com/facebook/react-native/pull/5505 Reviewed By: svcscm Differential Revision: D2860579 Pulled By: androidtrunkagent fb-gh-sync-id: 0d64c0999c47a6cbbf084cc8e0c8a6ea209b0880 --- .../DependencyGraph/ResolutionRequest.js | 31 +++++--- .../__tests__/DependencyGraph-test.js | 79 +++++++++++++++++++ 2 files changed, 101 insertions(+), 9 deletions(-) diff --git a/react-packager/src/DependencyResolver/DependencyGraph/ResolutionRequest.js b/react-packager/src/DependencyResolver/DependencyGraph/ResolutionRequest.js index f6740439..87e00260 100644 --- a/react-packager/src/DependencyResolver/DependencyGraph/ResolutionRequest.js +++ b/react-packager/src/DependencyResolver/DependencyGraph/ResolutionRequest.js @@ -264,20 +264,33 @@ class ResolutionRequest { }); } + _resolveFileOrDir(fromModule, toModuleName) { + const potentialModulePath = isAbsolutePath(toModuleName) ? + toModuleName : + path.join(path.dirname(fromModule.path), toModuleName); + + return this._redirectRequire(fromModule, potentialModulePath).then( + realModuleName => this._tryResolve( + () => this._loadAsFile(realModuleName, fromModule, toModuleName), + () => this._loadAsDir(realModuleName, fromModule, toModuleName) + ) + ); + } + _resolveNodeDependency(fromModule, toModuleName) { if (toModuleName[0] === '.' || toModuleName[1] === '/') { - const potentialModulePath = isAbsolutePath(toModuleName) ? - toModuleName : - path.join(path.dirname(fromModule.path), toModuleName); - return this._redirectRequire(fromModule, potentialModulePath).then( - realModuleName => this._tryResolve( - () => this._loadAsFile(realModuleName, fromModule, toModuleName), - () => this._loadAsDir(realModuleName, fromModule, toModuleName) - ) - ); + return this._resolveFileOrDir(fromModule, toModuleName) } else { return this._redirectRequire(fromModule, toModuleName).then( realModuleName => { + if (realModuleName[0] === '.' || realModuleName[1] === '/') { + // derive absolute path /.../node_modules/fromModuleDir/realModuleName + let fromModuleParentIdx = fromModule.path.lastIndexOf('node_modules/') + 13 + let fromModuleDir = fromModule.path.slice(0, fromModule.path.indexOf('/', fromModuleParentIdx)) + let absPath = path.join(fromModuleDir, realModuleName) + return this._resolveFileOrDir(fromModule, absPath) + } + const searchQueue = []; for (let currDir = path.dirname(fromModule.path); currDir !== path.parse(fromModule.path).root; diff --git a/react-packager/src/DependencyResolver/DependencyGraph/__tests__/DependencyGraph-test.js b/react-packager/src/DependencyResolver/DependencyGraph/__tests__/DependencyGraph-test.js index b1126a4c..6c88acb6 100644 --- a/react-packager/src/DependencyResolver/DependencyGraph/__tests__/DependencyGraph-test.js +++ b/react-packager/src/DependencyResolver/DependencyGraph/__tests__/DependencyGraph-test.js @@ -1774,6 +1774,85 @@ describe('DependencyGraph', function() { }); }); + pit('should support browser mapping of a package to a file ("' + fieldName + '")', function() { + var root = '/root'; + fs.__setMockFilesystem({ + 'root': { + 'index.js': [ + '/**', + ' * @providesModule index', + ' */', + 'require("aPackage")', + ].join('\n'), + 'aPackage': { + 'package.json': JSON.stringify(replaceBrowserField({ + name: 'aPackage', + browser: { + 'node-package': './dir/browser.js', + }, + }, fieldName)), + 'index.js': 'require("./dir/ooga")', + 'dir': { + 'ooga.js': 'require("node-package")', + 'browser.js': 'some browser code', + }, + 'node-package': { + 'package.json': JSON.stringify({ + 'name': 'node-package', + }), + 'index.js': 'some node code', + }, + }, + }, + }); + + const dgraph = new DependencyGraph({ + ...defaults, + roots: [root], + }); + return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { + expect(deps) + .toEqual([ + { id: 'index', + path: '/root/index.js', + dependencies: ['aPackage'], + isAsset: false, + isAsset_DEPRECATED: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { id: 'aPackage/index.js', + path: '/root/aPackage/index.js', + dependencies: ['./dir/ooga'], + isAsset: false, + isAsset_DEPRECATED: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { id: 'aPackage/dir/ooga.js', + path: '/root/aPackage/dir/ooga.js', + dependencies: ['node-package'], + isAsset: false, + isAsset_DEPRECATED: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + { id: 'aPackage/dir/browser.js', + path: '/root/aPackage/dir/browser.js', + dependencies: [], + isAsset: false, + isAsset_DEPRECATED: false, + isJSON: false, + isPolyfill: false, + resolution: undefined, + }, + ]); + }); + }); + pit('should support browser mapping for packages ("' + fieldName + '")', function() { var root = '/root'; fs.__setMockFilesystem({