Skip to content

Commit 36b31bc

Browse files
committed
Unify the way we fork modules
1 parent d83916e commit 36b31bc

File tree

11 files changed

+98
-84
lines changed

11 files changed

+98
-84
lines changed

.flowconfig

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
<PROJECT_ROOT>/scripts/rollup/shims/facebook-www/.*
99
<PROJECT_ROOT>/scripts/rollup/shims/react-native/.*
1010

11-
# Note: intentionally *don't* ignore /scripts/rollup/shims/rollup/
12-
# because it is part of the build and isn't external.
13-
1411
<PROJECT_ROOT>/.*/node_modules/y18n/.*
1512
<PROJECT_ROOT>/node_modules/chrome-devtools-frontend/.*
1613
<PROJECT_ROOT>/node_modules/devtools-timeline-model/.*

packages/react-cs-renderer/src/ReactNativeCSFeatureFlags.js renamed to packages/shared/forks/ReactFeatureFlags.native-cs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import invariant from 'fbjs/lib/invariant';
1111

1212
import typeof * as FeatureFlagsType from 'shared/ReactFeatureFlags';
13-
import typeof * as CSFeatureFlagsType from './ReactNativeCSFeatureFlags';
13+
import typeof * as CSFeatureFlagsType from './ReactFeatureFlags.native-cs';
1414

1515
export const debugRenderPhaseSideEffects = false;
1616
export const enableAsyncSubtreeAPI = true;

packages/react-native-renderer/src/ReactNativeFeatureFlags.js renamed to packages/shared/forks/ReactFeatureFlags.native.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import invariant from 'fbjs/lib/invariant';
1111

1212
import typeof * as FeatureFlagsType from 'shared/ReactFeatureFlags';
13-
import typeof * as FeatureFlagsShimType from './ReactNativeFeatureFlags';
13+
import typeof * as FeatureFlagsShimType from './ReactFeatureFlags.native';
1414

1515
// Re-export dynamic flags from the fbsource version.
1616
export const {debugRenderPhaseSideEffects} = require('ReactFeatureFlags');

scripts/rollup/shims/rollup/ReactFeatureFlags-www.js renamed to packages/shared/forks/ReactFeatureFlags.www.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010
import typeof * as FeatureFlagsType from 'shared/ReactFeatureFlags';
11-
import typeof * as FeatureFlagsShimType from './ReactFeatureFlags-www';
11+
import typeof * as FeatureFlagsShimType from './ReactFeatureFlags.www';
1212

1313
// Re-export dynamic flags from the www version.
1414
export const {

scripts/rollup/build.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,10 @@ function getPlugins(
218218
bundleType,
219219
globalName,
220220
moduleType,
221-
modulesToStub,
222-
featureFlags
221+
modulesToStub
223222
) {
224223
const findAndRecordErrorCodes = extractErrorCodes(errorCodeOpts);
225-
const shims = Modules.getShims(bundleType, entry, featureFlags);
224+
const forks = Modules.getForks(bundleType, entry);
226225
const isProduction = isProductionBundleType(bundleType);
227226
const isInGlobalScope = bundleType === UMD_DEV || bundleType === UMD_PROD;
228227
const isFBBundle = bundleType === FB_DEV || bundleType === FB_PROD;
@@ -236,8 +235,8 @@ function getPlugins(
236235
return source;
237236
},
238237
},
239-
// Shim some modules for www custom behavior and optimizations.
240-
alias(shims),
238+
// Shim any modules that need forking in this environment.
239+
alias(forks),
241240
// Use Node resolution mechanism.
242241
resolvePlugin({
243242
skip: externals,
@@ -384,8 +383,7 @@ async function createBundle(bundle, bundleType) {
384383
bundleType,
385384
bundle.global,
386385
bundle.moduleType,
387-
bundle.modulesToStub,
388-
bundle.featureFlags
386+
bundle.modulesToStub
389387
),
390388
// We can't use getters in www.
391389
legacy: bundleType === FB_DEV || bundleType === FB_PROD,

scripts/rollup/bundles.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ const bundles = [
129129
'deepFreezeAndThrowOnMutationInDev',
130130
'flattenStyle',
131131
],
132-
featureFlags: 'react-native-renderer/src/ReactNativeFeatureFlags',
133132
},
134133

135134
/******* React Native RT *******/
@@ -156,7 +155,6 @@ const bundles = [
156155
entry: 'react-cs-renderer',
157156
global: 'ReactCSRenderer',
158157
externals: ['CSStatefulComponent'],
159-
featureFlags: 'react-cs-renderer/src/ReactNativeCSFeatureFlags',
160158
},
161159

162160
/******* React Test Renderer *******/

scripts/rollup/modules.js

Lines changed: 86 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,73 @@ const UMD_PROD = bundleTypes.UMD_PROD;
88
const FB_DEV = bundleTypes.FB_DEV;
99
const FB_PROD = bundleTypes.FB_PROD;
1010

11+
// If you need to replace a file with another file for a specific environment,
12+
// add it to this list with the logic for choosing the right replacement.
13+
const forkedModules = Object.freeze({
14+
// Optimization: for UMDs, use object-assign polyfill that is already a part
15+
// of the React package instead of bundling it again.
16+
'object-assign': (bundleType, entry) => {
17+
if (bundleType !== UMD_DEV && bundleType !== UMD_PROD) {
18+
// It's only relevant for UMD bundles since that's where the duplication
19+
// happens. Other bundles just require('object-assign') anyway.
20+
return null;
21+
}
22+
if (getDependencies(bundleType, entry).indexOf('react') === -1) {
23+
// We can only apply the optimizations to bundle that depend on React
24+
// because we read assign() from an object exposed on React internals.
25+
return null;
26+
}
27+
// We can use the fork!
28+
return 'shared/forks/object-assign.umd.js';
29+
},
30+
31+
// We have a few forks for different environments.
32+
'shared/ReactFeatureFlags': (bundleType, entry) => {
33+
switch (entry) {
34+
case 'react-native-renderer':
35+
return 'shared/forks/ReactFeatureFlags.native.js';
36+
case 'react-cs-renderer':
37+
return 'shared/forks/ReactFeatureFlags.native-cs.js';
38+
default:
39+
switch (bundleType) {
40+
case FB_DEV:
41+
case FB_PROD:
42+
return 'shared/forks/ReactFeatureFlags.www.js';
43+
}
44+
}
45+
return null;
46+
},
47+
48+
// This logic is forked on www to blacklist warnings.
49+
'shared/lowPriorityWarning': (bundleType, entry) => {
50+
switch (bundleType) {
51+
case FB_DEV:
52+
case FB_PROD:
53+
return 'shared/forks/lowPriorityWarning.www.js';
54+
default:
55+
return null;
56+
}
57+
},
58+
59+
// In FB bundles, we preserve an inline require to ReactCurrentOwner.
60+
// See the explanation in FB version of ReactCurrentOwner in www:
61+
'react/src/ReactCurrentOwner': (bundleType, entry) => {
62+
switch (bundleType) {
63+
case FB_DEV:
64+
case FB_PROD:
65+
return 'react/src/forks/ReactCurrentOwner.www.js';
66+
default:
67+
return null;
68+
}
69+
},
70+
});
71+
1172
// Bundles exporting globals that other modules rely on.
1273
const knownGlobals = Object.freeze({
1374
react: 'React',
1475
'react-dom': 'ReactDOM',
1576
});
1677

17-
// Redirect some modules to Haste forks in www.
18-
// Assuming their names in www are the same, and also match
19-
// imported names in corresponding ./shims/rollup/*-www.js shims.
20-
const forkedFBModules = Object.freeze([
21-
// At FB, we don't know them statically:
22-
'shared/ReactFeatureFlags',
23-
// This logic is also forked internally.
24-
'shared/lowPriorityWarning',
25-
// In FB bundles, we preserve an inline require to ReactCurrentOwner.
26-
// See the explanation in FB version of ReactCurrentOwner in www:
27-
'react/src/ReactCurrentOwner',
28-
]);
29-
3078
// For any external that is used in a DEV-only condition, explicitly
3179
// specify whether it has side effects during import or not. This lets
3280
// us know whether we can safely omit them when they are unused.
@@ -62,72 +110,45 @@ function getDependencies(bundleType, entry) {
62110
path.dirname(require.resolve(entry))
63111
) + '/package.json');
64112
// Both deps and peerDeps are assumed as accessible.
65-
let deps = Array.from(
113+
return Array.from(
66114
new Set([
67115
...Object.keys(packageJson.dependencies || {}),
68116
...Object.keys(packageJson.peerDependencies || {}),
69117
])
70118
);
71-
// In www, forked modules are also require-able.
72-
if (bundleType === FB_DEV || bundleType === FB_PROD) {
73-
deps = [...deps, ...forkedFBModules.map(name => path.basename(name))];
74-
}
75-
return deps;
76-
}
77-
78-
function getImportSideEffects() {
79-
return importSideEffects;
80119
}
81120

82121
// Hijacks some modules for optimization and integration reasons.
83-
function getShims(bundleType, entry, featureFlags) {
122+
function getForks(bundleType, entry) {
84123
const shims = {};
85-
switch (bundleType) {
86-
case UMD_DEV:
87-
case UMD_PROD:
88-
if (getDependencies(bundleType, entry).indexOf('react') !== -1) {
89-
// Optimization: rely on object-assign polyfill that is already a part
90-
// of the React package instead of bundling it again.
91-
shims['object-assign'] = path.resolve(
92-
__dirname + '/shims/rollup/assign-umd.js'
93-
);
94-
}
95-
break;
96-
case FB_DEV:
97-
case FB_PROD:
98-
// FB forks a few modules in www that are usually bundled.
99-
// Instead of bundling them, they need to be kept as require()s in the
100-
// final bundles so that they import www modules with the same names.
101-
// Rollup doesn't make it very easy to rewrite and ignore such a require,
102-
// so we resort to using a shim that re-exports the www module, and then
103-
// treating shim's target destinations as external (see getDependencies).
104-
forkedFBModules.forEach(srcPath => {
105-
const fileName = path.parse(srcPath).name;
106-
const shimPath = path.resolve(
107-
__dirname + `/shims/rollup/${fileName}-www.js`
108-
);
109-
shims[srcPath] = shimPath;
110-
// <hack>
111-
// Unfortunately the above doesn't work for relative imports,
112-
// and Rollup isn't smart enough to understand they refer to the same file.
113-
// We should come up with a real fix for this, but for now this will do.
114-
// FIXME: this is gross.
115-
shims['./' + fileName] = shimPath;
116-
shims['../' + fileName] = shimPath;
117-
// We don't have deeper relative requires between source files.
118-
// </hack>
119-
});
120-
break;
121-
}
122-
if (featureFlags) {
123-
shims['shared/ReactFeatureFlags'] = require.resolve(featureFlags);
124-
}
124+
Object.keys(forkedModules).forEach(srcModule => {
125+
const targetModule = forkedModules[srcModule](bundleType, entry);
126+
if (targetModule === null) {
127+
return;
128+
}
129+
const targetPath = require.resolve(targetModule);
130+
shims[srcModule] = targetPath;
131+
// <hack>
132+
// Unfortunately the above doesn't work for relative imports,
133+
// and Rollup isn't smart enough to understand they refer to the same file.
134+
// We should come up with a real fix for this, but for now this will do.
135+
// FIXME: this is gross.
136+
const fileName = path.parse(srcModule).name;
137+
shims['./' + fileName] = targetPath;
138+
shims['../' + fileName] = targetPath;
139+
// We don't have deeper relative requires between source files.
140+
// </hack>
141+
});
125142
return shims;
126143
}
127144

145+
function getImportSideEffects() {
146+
return importSideEffects;
147+
}
148+
128149
module.exports = {
129150
getImportSideEffects,
130151
getPeerGlobals,
131152
getDependencies,
132-
getShims,
153+
getForks,
133154
};

0 commit comments

Comments
 (0)