@@ -8,25 +8,73 @@ const UMD_PROD = bundleTypes.UMD_PROD;
88const FB_DEV = bundleTypes . FB_DEV ;
99const 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.
1273const 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+
128149module . exports = {
129150 getImportSideEffects,
130151 getPeerGlobals,
131152 getDependencies,
132- getShims ,
153+ getForks ,
133154} ;
0 commit comments