Skip to content

Commit

Permalink
breaking test and fix for browser field mapping from package to file
Browse files Browse the repository at this point in the history
Summary:
breaks on mappings like:

```json
"browser": {
  "node-package": "./dir/browser.js"
}
```
Closes #5505

Reviewed By: svcscm

Differential Revision: D2860579

Pulled By: androidtrunkagent

fb-gh-sync-id: 0d64c0999c47a6cbbf084cc8e0c8a6ea209b0880
  • Loading branch information
mvayngrib authored and facebook-github-bot-6 committed Jan 25, 2016
1 parent 193df8a commit 191b692
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 9 deletions.
Expand Up @@ -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;
Expand Down
Expand Up @@ -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({
Expand Down

0 comments on commit 191b692

Please sign in to comment.