mirror of https://github.com/status-im/metro.git
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:
parent
c00871f086
commit
2963af0bef
|
@ -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;
|
||||
|
|
|
@ -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({
|
||||
|
|
Loading…
Reference in New Issue