Skip to content

Commit 1487ebf

Browse files
jspahrsummersfacebook-github-bot-7
authored andcommitted
Revert packager randomization revert
1 parent 23564fe commit 1487ebf

3 files changed

Lines changed: 42 additions & 12 deletions

File tree

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

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@
88
*/
99
'use strict';
1010

11+
const chalk = require('chalk');
1112
const path = require('path');
12-
const getPontentialPlatformExt = require('../../lib/getPlatformExtension');
13+
const getPlatformExtension = require('../../lib/getPlatformExtension');
1314

1415
class HasteMap {
1516
constructor({ fastfs, moduleCache, helpers }) {
1617
this._fastfs = fastfs;
1718
this._moduleCache = moduleCache;
1819
this._helpers = helpers;
1920
this._map = Object.create(null);
21+
this._warnedAbout = Object.create(null);
2022
}
2123

2224
build() {
@@ -35,6 +37,9 @@ class HasteMap {
3537

3638
processFileChange(type, absPath) {
3739
return Promise.resolve().then(() => {
40+
// Rewarn after file changes.
41+
this._warnedAbout = Object.create(null);
42+
3843
/*eslint no-labels: 0 */
3944
if (type === 'delete' || type === 'change') {
4045
loop: for (let name in this._map) {
@@ -64,19 +69,39 @@ class HasteMap {
6469
}
6570

6671
getModule(name, platform = null) {
67-
if (this._map[name]) {
68-
const modules = this._map[name];
69-
if (platform != null) {
70-
for (let i = 0; i < modules.length; i++) {
71-
if (getPontentialPlatformExt(modules[i].path) === platform) {
72-
return modules[i];
73-
}
72+
if (!this._map[name]) {
73+
return null;
74+
}
75+
76+
const modules = this._map[name];
77+
if (platform != null) {
78+
for (let i = 0; i < modules.length; i++) {
79+
if (getPlatformExtension(modules[i].path) === platform) {
80+
return modules[i];
7481
}
7582
}
7683

77-
return modules[0];
84+
if (modules.length > 1) {
85+
if (!this._warnedAbout[name]) {
86+
this._warnedAbout[name] = true;
87+
console.warn(
88+
chalk.yellow(
89+
'\nWARNING: Found multiple haste modules or packages ' +
90+
'with the name `%s`. Please fix this by adding it to ' +
91+
'the blacklist or deleting the modules keeping only one.\n' +
92+
'One of the following modules will be selected at random:\n%s\n'
93+
),
94+
name,
95+
modules.map(m => m.path).join('\n'),
96+
);
97+
}
98+
99+
const randomIndex = Math.floor(Math.random() * modules.length);
100+
return modules[randomIndex];
101+
}
78102
}
79-
return null;
103+
104+
return modules[0];
80105
}
81106

82107
_processHasteModule(file) {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,11 @@ class ResolutionRequest {
252252
_loadAsFile(potentialModulePath) {
253253
return Promise.resolve().then(() => {
254254
if (this._helpers.isAssetFile(potentialModulePath)) {
255+
const dirname = path.dirname(potentialModulePath);
256+
if (!this._fastfs.dirExists(dirname)) {
257+
throw new UnableToResolveError(`Directory ${dirname} doesn't exist`);
258+
}
259+
255260
const {name, type} = getAssetDataFromName(potentialModulePath);
256261

257262
let pattern = '^' + name + '(@[\\d\\.]+x)?';
@@ -263,7 +268,7 @@ class ResolutionRequest {
263268
// We arbitrarly grab the first one, because scale selection
264269
// will happen somewhere
265270
const [assetFile] = this._fastfs.matches(
266-
path.dirname(potentialModulePath),
271+
dirname,
267272
new RegExp(pattern)
268273
);
269274

packager/react-packager/src/lib/getPlatformExtension.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
const path = require('path');
1212

13-
const SUPPORTED_PLATFORM_EXTS = ['android', 'ios'];
13+
const SUPPORTED_PLATFORM_EXTS = ['android', 'ios', 'web'];
1414

1515
const re = new RegExp(
1616
'[^\\.]+\\.(' + SUPPORTED_PLATFORM_EXTS.join('|') + ')\\.\\w+$'

0 commit comments

Comments
 (0)