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
This commit is contained in:
Mark Vayngrib 2016-01-25 10:08:43 -08:00 committed by facebook-github-bot-6
parent c00871f086
commit 2963af0bef
2 changed files with 101 additions and 9 deletions

View File

@ -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;

View File

@ -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({