Skip to content

Commit 8f26950

Browse files
idegaearon
authored andcommitted
[react-native] Rewrite Haste imports in RN shims and add .fb.js extension (facebook#15786)
This commit is a follow-up to facebook#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.
1 parent 1272753 commit 8f26950

File tree

9 files changed

+28
-19
lines changed

9 files changed

+28
-19
lines changed

packages/shared/forks/ReactFeatureFlags.native-fb.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import typeof * as FeatureFlagsType from 'shared/ReactFeatureFlags';
1313
import typeof * as FeatureFlagsShimType from './ReactFeatureFlags.native-fb';
1414

1515
// Re-export dynamic flags from the fbsource version.
16-
export const {debugRenderPhaseSideEffects} = require('ReactFeatureFlags');
16+
export const {
17+
debugRenderPhaseSideEffects,
18+
} = require('../shims/ReactFeatureFlags');
1719

1820
// The rest of the flags are static for better dead code elimination.
1921
export const enableUserTimingAPI = __DEV__;

scripts/flow/react-native-host-hooks.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,8 @@ declare module 'RTManager' {
157157

158158
declare function completeUpdates(): void;
159159
}
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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function getBundleOutputPaths(bundleType, filename, packageName) {
5656
case RN_OSS_PROFILING:
5757
switch (packageName) {
5858
case 'react-native-renderer':
59-
return [`build/react-native/oss/${filename}`];
59+
return [`build/react-native/implementations/${filename}`];
6060
default:
6161
throw new Error('Unknown RN package.');
6262
}
@@ -65,7 +65,12 @@ function getBundleOutputPaths(bundleType, filename, packageName) {
6565
case RN_FB_PROFILING:
6666
switch (packageName) {
6767
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+
];
6974
default:
7075
throw new Error('Unknown RN package.');
7176
}
@@ -93,7 +98,6 @@ async function copyRNShims() {
9398
require.resolve('react-native-renderer/src/ReactNativeTypes.js'),
9499
'build/react-native/shims/ReactNativeTypes.js'
95100
),
96-
asyncCopyTo(`${__dirname}/shims/react-native-fb`, 'build/react-native/fb'),
97101
]);
98102
}
99103

scripts/rollup/shims/react-native/NativeMethodsMixin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
const {
1414
__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED,
15-
} = require('ReactNative');
15+
} = require('./ReactNative');
1616

17-
import type {NativeMethodsMixinType} from 'ReactNativeTypes';
17+
import type {NativeMethodsMixinType} from './ReactNativeTypes';
1818

1919
const {NativeMethodsMixin} = __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
2020

scripts/rollup/shims/react-native/ReactFabric.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@
1010

1111
'use strict';
1212

13-
const BatchedBridge = require('BatchedBridge');
13+
import {BatchedBridge} from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface';
1414

1515
// TODO @sema: Adjust types
16-
import type {ReactNativeType} from 'ReactNativeTypes';
16+
import type {ReactNativeType} from './ReactNativeTypes';
1717

1818
let ReactFabric;
1919

2020
if (__DEV__) {
21-
ReactFabric = require('ReactFabric-dev');
21+
ReactFabric = require('../implementations/ReactFabric-dev');
2222
} else {
23-
ReactFabric = require('ReactFabric-prod');
23+
ReactFabric = require('../implementations/ReactFabric-prod');
2424
}
2525

2626
BatchedBridge.registerCallableModule('ReactFabric', ReactFabric);

scripts/rollup/shims/react-native/ReactNative.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010

1111
'use strict';
1212

13-
import type {ReactNativeType} from 'ReactNativeTypes';
13+
import type {ReactNativeType} from './ReactNativeTypes';
1414

1515
let ReactNative;
1616

1717
if (__DEV__) {
18-
ReactNative = require('ReactNativeRenderer-dev');
18+
ReactNative = require('../implementations/ReactNativeRenderer-dev');
1919
} else {
20-
ReactNative = require('ReactNativeRenderer-prod');
20+
ReactNative = require('../implementations/ReactNativeRenderer-prod');
2121
}
2222

2323
module.exports = (ReactNative: ReactNativeType);

scripts/rollup/shims/react-native/createReactNativeComponentClass.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010

1111
'use strict';
1212

13+
import {ReactNativeViewConfigRegistry} from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface';
14+
1315
import type {ViewConfigGetter} from './ReactNativeTypes';
1416

15-
const {register} = require('ReactNativeViewConfigRegistry');
17+
const {register} = ReactNativeViewConfigRegistry;
1618

1719
/**
1820
* Creates a renderable ReactNative host component.

scripts/rollup/validate/index.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,7 @@ const bundles = [
6060
},
6161
{
6262
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`],
6864
},
6965
{
7066
format: 'umd',

0 commit comments

Comments
 (0)