Skip to content

Commit 23e3503

Browse files
idefacebook-github-bot
authored andcommitted
Default to lazy requires for all public RN exports (#362)
Summary: **Summary** This commit changes `metro-react-native-babel-preset` to emit lazy `require` calls for all of RN's public exports. This is so that we can move RN to use ES import/export, which syntactically have no lazy version, while preserving the lazy behavior we have today by default. If the developer specifies `lazyImportExportTransform: true`, all imports/exports will be lazy including RN's public exports, so they will remain lazy. (Also, this PR allows developers to specify non-boolean values for `lazyImportExportTransform` e.g. an array of whitelisted paths.) Note: this commit must land and be published before RN can use ESM since the iOS E2E tests depend on the current lazy `require` calls. See the draft PR for RN here: facebook/react-native#23591 **Test plan** Used this module inside of the RN E2E iOS tests and loaded the bundle successfully. Also used it in RNTester. Pull Request resolved: #362 Differential Revision: D14434999 Pulled By: cpojer fbshipit-source-id: d280a8216c6e1c6604e8fea508f5a48f4c1c071b
1 parent 123bdf7 commit 23e3503

File tree

2 files changed

+121
-1
lines changed

2 files changed

+121
-1
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* This is the set of modules that React Native publicly exports and that we
8+
* want to require lazily. Keep this list in sync with
9+
* Libraries/react-native/react-native-implementation.js (though having extra
10+
* entries here is fairly harmless).
11+
12+
* @format
13+
*/
14+
'use strict';
15+
16+
module.exports = new Set([
17+
'AccessibilityInfo',
18+
'ActivityIndicator',
19+
'ReactNativeART',
20+
'Button',
21+
'CheckBox',
22+
'DatePickerIOS',
23+
'DrawerLayoutAndroid',
24+
'FlatList',
25+
'Image',
26+
'ImageBackground',
27+
'ImageEditor',
28+
'ImageStore',
29+
'InputAccessoryView',
30+
'KeyboardAvoidingView',
31+
'ListView',
32+
'MaskedViewIOS',
33+
'Modal',
34+
'Picker',
35+
'PickerIOS',
36+
'ProgressBarAndroid',
37+
'ProgressViewIOS',
38+
'SafeAreaView',
39+
'ScrollView',
40+
'SectionList',
41+
'SegmentedControlIOS',
42+
'Slider',
43+
'Switch',
44+
'RefreshControl',
45+
'StatusBar',
46+
'SwipeableFlatList',
47+
'SwipeableListView',
48+
'Text',
49+
'TextInput',
50+
'ToolbarAndroid',
51+
'Touchable',
52+
'TouchableHighlight',
53+
'TouchableNativeFeedback',
54+
'TouchableOpacity',
55+
'TouchableWithoutFeedback',
56+
'View',
57+
'ViewPagerAndroid',
58+
'VirtualizedList',
59+
'WebView',
60+
61+
// APIs
62+
'ActionSheetIOS',
63+
'Alert',
64+
'Animated',
65+
'AppRegistry',
66+
'AppState',
67+
'AsyncStorage',
68+
'BackHandler',
69+
'CameraRoll',
70+
'Clipboard',
71+
'DatePickerAndroid',
72+
'DeviceInfo',
73+
'Dimensions',
74+
'Easing',
75+
'ReactNative',
76+
'I18nManager',
77+
'ImagePickerIOS',
78+
'InteractionManager',
79+
'Keyboard',
80+
'LayoutAnimation',
81+
'Linking',
82+
'NativeEventEmitter',
83+
'NetInfo',
84+
'PanResponder',
85+
'PermissionsAndroid',
86+
'PixelRatio',
87+
'PushNotificationIOS',
88+
'Settings',
89+
'Share',
90+
'StatusBarIOS',
91+
'StyleSheet',
92+
'Systrace',
93+
'TimePickerAndroid',
94+
'ToastAndroid',
95+
'TVEventHandler',
96+
'UIManager',
97+
'ReactNative',
98+
'UTFSequence',
99+
'Vibration',
100+
'YellowBox',
101+
102+
// Plugins
103+
'RCTDeviceEventEmitter',
104+
'RCTNativeAppEventEmitter',
105+
'NativeModules',
106+
'Platform',
107+
'processColor',
108+
'requireNativeComponent',
109+
110+
// Prop Types
111+
'DeprecatedColorPropType',
112+
'DeprecatedEdgeInsetsPropType',
113+
'DeprecatedPointPropType',
114+
'DeprecatedViewPropTypes',
115+
]);

packages/metro-react-native-babel-preset/src/configs/main.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
'use strict';
1111

12+
const lazyImports = require('./lazy-imports');
13+
1214
function isTypeScriptSource(fileName) {
1315
return !!fileName && fileName.endsWith('.ts');
1416
}
@@ -93,7 +95,10 @@ const getPreset = (src, options) => {
9395
{
9496
strict: false,
9597
strictMode: false, // prevent "use strict" injections
96-
lazy: !!(options && options.lazyImportExportTransform),
98+
lazy:
99+
options && options.lazyImportExportTransform != null
100+
? options.lazyImportExportTransform
101+
: importSpecifier => lazyImports.has(importSpecifier),
97102
allowTopLevelThis: true, // dont rewrite global `this` -> `undefined`
98103
},
99104
],

0 commit comments

Comments
 (0)