@samitouri / QOS-React-2 / commits / 07da821bfd

[react-native] Rewrite Haste imports in RN shims and add .fb.js extension (#15786)

This commit is a follow-up to https://github.com/facebook/react/pull/15604, which explains more of the rationale behind moving React Native to path-based imports and the work needed in the React repository. In that linked PR, the generated renderers were updated but not the shims; this commit updates the shims. The problem is that FB needs a different copy of the built renderers than the OSS versions so we need a way for FB code to import different modules than in OSS. This was previously done with Haste, but with the removal of Haste from RN, we need another mechanism. Talking with cpojer, we are using a `.fb.js` extension that Metro can be configured to prioritize over `.js`. This commit generates FB's renderers with the `.fb.js` extension and OSS renderers with just `.js`. This way, FB can internally configure Metro to use the `.fb.js` implementations and OSS will use the `.js` ones, letting us swap out which implementation gets bundled. Test Plan: Generated the renderers and shims with `yarn build` and then verified that the generated shims don't contain any Haste-style imports. Copied the renderers and shims into RN manually and launched the RNTester app to verify it loads end-to-end. Added `.fb.js` to the extensions in `metro.config.js` and verified that the FB-specific bundles loaded.

James Ide committed Jun 3, 2019 at 07:58 UTC 07da821bfda181a7ee750c12d73d09fbef87d0f3
9 files changed +28 -19
packages/shared/forks/ReactFeatureFlags.native-fb.js
+3 -1
@@ -13,7 +13,9 @@ import typeof * as FeatureFlagsType from 'shared/ReactFeatureFlags';
13 import typeof * as FeatureFlagsShimType from './ReactFeatureFlags.native-fb';
14
15 // Re-export dynamic flags from the fbsource version.
16 -export const {debugRenderPhaseSideEffects} = require('ReactFeatureFlags');
16 +export const {
17 + debugRenderPhaseSideEffects,
18 +} = require('../shims/ReactFeatureFlags');
19
20 // The rest of the flags are static for better dead code elimination.
21 export const enableUserTimingAPI = __DEV__;
scripts/flow/react-native-host-hooks.js
+5
@@ -157,3 +157,8 @@ declare module 'RTManager' {
157
158 declare function completeUpdates(): void;
159 }
160 +
161 +// shims/ReactFeatureFlags is generated by the packaging script
162 +declare module '../shims/ReactFeatureFlags' {
163 + declare export var debugRenderPhaseSideEffects: boolean;
164 +}
scripts/rollup/packaging.js
+7 -3
@@ -56,7 +56,7 @@ function getBundleOutputPaths(bundleType, filename, packageName) {
56 case RN_OSS_PROFILING:
57 switch (packageName) {
58 case 'react-native-renderer':
59 - return [`build/react-native/oss/${filename}`];
59 + return [`build/react-native/implementations/${filename}`];
60 default:
61 throw new Error('Unknown RN package.');
62 }
@@ -65,7 +65,12 @@ function getBundleOutputPaths(bundleType, filename, packageName) {
65 case RN_FB_PROFILING:
66 switch (packageName) {
67 case 'react-native-renderer':
68 - return [`build/react-native/fb/${filename}`];
68 + return [
69 + `build/react-native/implementations/${filename.replace(
70 + /\.js$/,
71 + '.fb.js'
72 + )}`,
73 + ];
74 default:
75 throw new Error('Unknown RN package.');
76 }
@@ -93,7 +98,6 @@ async function copyRNShims() {
98 require.resolve('react-native-renderer/src/ReactNativeTypes.js'),
99 'build/react-native/shims/ReactNativeTypes.js'
100 ),
96 - asyncCopyTo(`${__dirname}/shims/react-native-fb`, 'build/react-native/fb'),
101 ]);
102 }
103
scripts/rollup/shims/react-native/NativeMethodsMixin.js
+2 -2
@@ -12,9 +12,9 @@
12
13 const {
14 __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
15 -} = require('ReactNative');
15 +} = require('./ReactNative');
16
17 -import type {NativeMethodsMixinType} from 'ReactNativeTypes';
17 +import type {NativeMethodsMixinType} from './ReactNativeTypes';
18
19 const {NativeMethodsMixin} = __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
20
scripts/rollup/shims/react-native/ReactFabric.js
+4 -4
@@ -10,17 +10,17 @@
10
11 'use strict';
12
13 -const BatchedBridge = require('BatchedBridge');
13 +import {BatchedBridge} from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface';
14
15 // TODO @sema: Adjust types
16 -import type {ReactNativeType} from 'ReactNativeTypes';
16 +import type {ReactNativeType} from './ReactNativeTypes';
17
18 let ReactFabric;
19
20 if (__DEV__) {
21 - ReactFabric = require('ReactFabric-dev');
21 + ReactFabric = require('../implementations/ReactFabric-dev');
22 } else {
23 - ReactFabric = require('ReactFabric-prod');
23 + ReactFabric = require('../implementations/ReactFabric-prod');
24 }
25
26 BatchedBridge.registerCallableModule('ReactFabric', ReactFabric);
scripts/rollup/shims/react-native/ReactFeatureFlags.fb.js renamed
scripts/rollup/shims/react-native/ReactNative.js
+3 -3
@@ -10,14 +10,14 @@
10
11 'use strict';
12
13 -import type {ReactNativeType} from 'ReactNativeTypes';
13 +import type {ReactNativeType} from './ReactNativeTypes';
14
15 let ReactNative;
16
17 if (__DEV__) {
18 - ReactNative = require('ReactNativeRenderer-dev');
18 + ReactNative = require('../implementations/ReactNativeRenderer-dev');
19 } else {
20 - ReactNative = require('ReactNativeRenderer-prod');
20 + ReactNative = require('../implementations/ReactNativeRenderer-prod');
21 }
22
23 module.exports = (ReactNative: ReactNativeType);
scripts/rollup/shims/react-native/createReactNativeComponentClass.js
+3 -1
@@ -10,9 +10,11 @@
10
11 'use strict';
12
13 +import {ReactNativeViewConfigRegistry} from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface';
14 +
15 import type {ViewConfigGetter} from './ReactNativeTypes';
16
15 -const {register} = require('ReactNativeViewConfigRegistry');
17 +const {register} = ReactNativeViewConfigRegistry;
18
19 /**
20 * Creates a renderable ReactNative host component.
scripts/rollup/validate/index.js
+1 -5
@@ -60,11 +60,7 @@ const bundles = [
60 },
61 {
62 format: 'rn',
63 - filePatterns: [
64 - `./build/react-native/oss/*.js`,
65 - `./build/react-native/fb/ReactFabric-*.js`,
66 - `./build/react-native/fb/ReactNativeRenderer-*.js`,
67 - ],
63 + filePatterns: [`./build/react-native/implementations/*.js`],
64 },
65 {
66 format: 'umd',