[react-packager] Fix the confused node_modules detection function

Summary:
@public

We have a function that detects whether a give file is to be treated as a node_modules. If so it doesn't have access to the haste module map. There is an exception to this rule which is a few modules that are allowed to do that. Currently thats react-native, react-tools, and parse.

The current implementation had a bug where if you had `react-native` (or react-tools etc) in the name before the actual package root then the detection will be off. This fixes the problem by starting from the `lastIndexOf('node_modules')` directory, that way nothing confuses us.

Test Plan:
./runJestTests.sh
export OSS, patch, run e2e test
This commit is contained in:
Amjad Masad 2015-06-24 14:50:00 -07:00
parent fef3c7294d
commit 95800d1564
2 changed files with 66 additions and 5 deletions

View File

@ -2171,6 +2171,65 @@ describe('DependencyGraph', function() {
});
});
pit('should not be confused by prev occuring whitelisted names', function() {
var root = '/react-tools';
fs.__setMockFilesystem({
'react-tools': {
'index.js': [
'/**',
' * @providesModule index',
' */',
'require("shouldWork");',
].join('\n'),
'node_modules': {
'react-tools': {
'package.json': JSON.stringify({
name: 'react-tools',
main: 'main.js',
}),
'main.js': [
'/**',
' * @providesModule shouldWork',
' */',
].join('\n'),
},
},
}
});
var dgraph = new DependencyGraph({
roots: [root],
fileWatcher: fileWatcher,
assetExts: ['png', 'jpg'],
});
return dgraph.getOrderedDependencies('/react-tools/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
id: 'index',
path: '/react-tools/index.js',
dependencies: ['shouldWork'],
isAsset: false,
isAsset_DEPRECATED: false,
isJSON: false,
isPolyfill: false,
resolution: undefined,
},
{
id: 'shouldWork',
path: '/react-tools/node_modules/react-tools/main.js',
dependencies: [],
isAsset: false,
isAsset_DEPRECATED: false,
isJSON: false,
isPolyfill: false,
resolution: undefined,
},
]);
});
});
pit('should ignore modules it cant find (assumes own require system)', function() {
// For example SourceMap.js implements it's own require system.
var root = '/root';

View File

@ -414,18 +414,20 @@ class DependencyGraph {
}
_isNodeModulesDir(file) {
const inNodeModules = file.indexOf('/node_modules/') !== -1;
let parts = path.normalize(file).split(path.sep);
const indexOfNodeModules = parts.lastIndexOf('node_modules');
if (!inNodeModules) {
if (indexOfNodeModules === -1) {
return false;
}
parts = parts.slice(indexOfNodeModules + 1);
const dirs = this._opts.providesModuleNodeModules;
for (let i = 0; i < dirs.length; i++) {
const index = file.indexOf(dirs[i]);
if (index !== -1) {
return file.slice(index).indexOf('/node_modules/') !== -1;
if (parts.indexOf(dirs[i]) > -1) {
return false;
}
}