Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4ac5c7e

Browse files
author
Amjad Masad
committed
[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
1 parent eb9c723 commit 4ac5c7e

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

packager/react-packager/src/DependencyResolver/DependencyGraph/__tests__/DependencyGraph-test.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2171,6 +2171,65 @@ describe('DependencyGraph', function() {
21712171
});
21722172
});
21732173

2174+
pit('should not be confused by prev occuring whitelisted names', function() {
2175+
var root = '/react-tools';
2176+
fs.__setMockFilesystem({
2177+
'react-tools': {
2178+
'index.js': [
2179+
'/**',
2180+
' * @providesModule index',
2181+
' */',
2182+
'require("shouldWork");',
2183+
].join('\n'),
2184+
'node_modules': {
2185+
'react-tools': {
2186+
'package.json': JSON.stringify({
2187+
name: 'react-tools',
2188+
main: 'main.js',
2189+
}),
2190+
'main.js': [
2191+
'/**',
2192+
' * @providesModule shouldWork',
2193+
' */',
2194+
].join('\n'),
2195+
},
2196+
},
2197+
}
2198+
});
2199+
2200+
var dgraph = new DependencyGraph({
2201+
roots: [root],
2202+
fileWatcher: fileWatcher,
2203+
assetExts: ['png', 'jpg'],
2204+
});
2205+
return dgraph.getOrderedDependencies('/react-tools/index.js').then(function(deps) {
2206+
expect(deps)
2207+
.toEqual([
2208+
{
2209+
id: 'index',
2210+
path: '/react-tools/index.js',
2211+
dependencies: ['shouldWork'],
2212+
isAsset: false,
2213+
isAsset_DEPRECATED: false,
2214+
isJSON: false,
2215+
isPolyfill: false,
2216+
resolution: undefined,
2217+
},
2218+
{
2219+
id: 'shouldWork',
2220+
path: '/react-tools/node_modules/react-tools/main.js',
2221+
dependencies: [],
2222+
isAsset: false,
2223+
isAsset_DEPRECATED: false,
2224+
isJSON: false,
2225+
isPolyfill: false,
2226+
resolution: undefined,
2227+
},
2228+
]);
2229+
});
2230+
});
2231+
2232+
21742233
pit('should ignore modules it cant find (assumes own require system)', function() {
21752234
// For example SourceMap.js implements it's own require system.
21762235
var root = '/root';

packager/react-packager/src/DependencyResolver/DependencyGraph/index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -414,18 +414,20 @@ class DependencyGraph {
414414
}
415415

416416
_isNodeModulesDir(file) {
417-
const inNodeModules = file.indexOf('/node_modules/') !== -1;
417+
let parts = path.normalize(file).split(path.sep);
418+
const indexOfNodeModules = parts.lastIndexOf('node_modules');
418419

419-
if (!inNodeModules) {
420+
if (indexOfNodeModules === -1) {
420421
return false;
421422
}
422423

424+
parts = parts.slice(indexOfNodeModules + 1);
425+
423426
const dirs = this._opts.providesModuleNodeModules;
424427

425428
for (let i = 0; i < dirs.length; i++) {
426-
const index = file.indexOf(dirs[i]);
427-
if (index !== -1) {
428-
return file.slice(index).indexOf('/node_modules/') !== -1;
429+
if (parts.indexOf(dirs[i]) > -1) {
430+
return false;
429431
}
430432
}
431433

0 commit comments

Comments
 (0)