Skip to content

Commit

Permalink
Default to lazy requires for all public RN exports (#362)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ide authored and facebook-github-bot committed Mar 13, 2019
1 parent 123bdf7 commit 23e3503
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 1 deletion.
115 changes: 115 additions & 0 deletions packages/metro-react-native-babel-preset/src/configs/lazy-imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* This is the set of modules that React Native publicly exports and that we
* want to require lazily. Keep this list in sync with
* Libraries/react-native/react-native-implementation.js (though having extra
* entries here is fairly harmless).
* @format
*/
'use strict';

module.exports = new Set([
'AccessibilityInfo',
'ActivityIndicator',
'ReactNativeART',
'Button',
'CheckBox',
'DatePickerIOS',
'DrawerLayoutAndroid',
'FlatList',
'Image',
'ImageBackground',
'ImageEditor',
'ImageStore',
'InputAccessoryView',
'KeyboardAvoidingView',
'ListView',
'MaskedViewIOS',
'Modal',
'Picker',
'PickerIOS',
'ProgressBarAndroid',
'ProgressViewIOS',
'SafeAreaView',
'ScrollView',
'SectionList',
'SegmentedControlIOS',
'Slider',
'Switch',
'RefreshControl',
'StatusBar',
'SwipeableFlatList',
'SwipeableListView',
'Text',
'TextInput',
'ToolbarAndroid',
'Touchable',
'TouchableHighlight',
'TouchableNativeFeedback',
'TouchableOpacity',
'TouchableWithoutFeedback',
'View',
'ViewPagerAndroid',
'VirtualizedList',
'WebView',

// APIs
'ActionSheetIOS',
'Alert',
'Animated',
'AppRegistry',
'AppState',
'AsyncStorage',
'BackHandler',
'CameraRoll',
'Clipboard',
'DatePickerAndroid',
'DeviceInfo',
'Dimensions',
'Easing',
'ReactNative',
'I18nManager',
'ImagePickerIOS',
'InteractionManager',
'Keyboard',
'LayoutAnimation',
'Linking',
'NativeEventEmitter',
'NetInfo',
'PanResponder',
'PermissionsAndroid',
'PixelRatio',
'PushNotificationIOS',
'Settings',
'Share',
'StatusBarIOS',
'StyleSheet',
'Systrace',
'TimePickerAndroid',
'ToastAndroid',
'TVEventHandler',
'UIManager',
'ReactNative',
'UTFSequence',
'Vibration',
'YellowBox',

// Plugins
'RCTDeviceEventEmitter',
'RCTNativeAppEventEmitter',
'NativeModules',
'Platform',
'processColor',
'requireNativeComponent',

// Prop Types
'DeprecatedColorPropType',
'DeprecatedEdgeInsetsPropType',
'DeprecatedPointPropType',
'DeprecatedViewPropTypes',
]);
7 changes: 6 additions & 1 deletion packages/metro-react-native-babel-preset/src/configs/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

'use strict';

const lazyImports = require('./lazy-imports');

function isTypeScriptSource(fileName) {
return !!fileName && fileName.endsWith('.ts');
}
Expand Down Expand Up @@ -93,7 +95,10 @@ const getPreset = (src, options) => {
{
strict: false,
strictMode: false, // prevent "use strict" injections
lazy: !!(options && options.lazyImportExportTransform),
lazy:
options && options.lazyImportExportTransform != null
? options.lazyImportExportTransform
: importSpecifier => lazyImports.has(importSpecifier),
allowTopLevelThis: true, // dont rewrite global `this` -> `undefined`
},
],
Expand Down

0 comments on commit 23e3503

Please sign in to comment.