Skip to content

Commit df2eaa9

Browse files
Emily Janzerfacebook-github-bot
Emily Janzer
authored andcommitted
Modularize InitializeCore
Summary: Split up InitializeCore into a bunch of modules. The idea here is to make it easier for apps to just get the initialization logic they want and leave behind what they don't; for example, if you don't want the Map/Set polyfills, instead of requiring InitializeCore you can require the modules you want from it. Reviewed By: yungsters Differential Revision: D10842564 fbshipit-source-id: 3b12d54fddea8c4ee75886022338c214987a015c
1 parent 5d38264 commit df2eaa9

15 files changed

+404
-189
lines changed

Libraries/Core/InitializeCore.js

+14-189
Original file line numberDiff line numberDiff line change
@@ -28,196 +28,21 @@
2828
const startTime =
2929
global.nativePerformanceNow != null ? global.nativePerformanceNow() : null;
3030

31-
const {polyfillObjectProperty, polyfillGlobal} = require('PolyfillFunctions');
32-
33-
if (global.GLOBAL === undefined) {
34-
global.GLOBAL = global;
35-
}
36-
37-
if (global.window === undefined) {
38-
global.window = global;
39-
}
40-
41-
// Set up collections
42-
const _shouldPolyfillCollection = require('_shouldPolyfillES6Collection');
43-
if (_shouldPolyfillCollection('Map')) {
44-
polyfillGlobal('Map', () => require('Map'));
45-
}
46-
if (_shouldPolyfillCollection('Set')) {
47-
polyfillGlobal('Set', () => require('Set'));
48-
}
49-
50-
// Set up process
51-
global.process = global.process || {};
52-
global.process.env = global.process.env || {};
53-
if (!global.process.env.NODE_ENV) {
54-
global.process.env.NODE_ENV = __DEV__ ? 'development' : 'production';
55-
}
56-
57-
// Setup the Systrace profiling hooks if necessary
58-
if (global.__RCTProfileIsProfiling) {
59-
const Systrace = require('Systrace');
60-
Systrace.installReactHook();
61-
Systrace.setEnabled(true);
62-
}
63-
64-
// Set up console
65-
const ExceptionsManager = require('ExceptionsManager');
66-
ExceptionsManager.installConsoleErrorReporter();
67-
68-
// Set up error handler
69-
if (!global.__fbDisableExceptionsManager) {
70-
const handleError = (e, isFatal) => {
71-
try {
72-
ExceptionsManager.handleException(e, isFatal);
73-
} catch (ee) {
74-
console.log('Failed to print error: ', ee.message);
75-
throw e;
76-
}
77-
};
78-
79-
const ErrorUtils = require('ErrorUtils');
80-
ErrorUtils.setGlobalHandler(handleError);
81-
}
82-
83-
// Check for compatibility between the JS and native code
84-
const ReactNativeVersionCheck = require('ReactNativeVersionCheck');
85-
ReactNativeVersionCheck.checkVersions();
86-
87-
// Set up Promise
88-
// The native Promise implementation throws the following error:
89-
// ERROR: Event loop not supported.
90-
polyfillGlobal('Promise', () => require('Promise'));
91-
92-
// Set up regenerator.
93-
polyfillGlobal('regeneratorRuntime', () => {
94-
// The require just sets up the global, so make sure when we first
95-
// invoke it the global does not exist
96-
delete global.regeneratorRuntime;
97-
98-
// regenerator-runtime/runtime exports the regeneratorRuntime object, so we
99-
// can return it safely.
100-
return require('regenerator-runtime/runtime');
101-
});
102-
103-
// Set up timers
104-
const defineLazyTimer = name => {
105-
polyfillGlobal(name, () => require('JSTimers')[name]);
106-
};
107-
defineLazyTimer('setTimeout');
108-
defineLazyTimer('setInterval');
109-
defineLazyTimer('setImmediate');
110-
defineLazyTimer('clearTimeout');
111-
defineLazyTimer('clearInterval');
112-
defineLazyTimer('clearImmediate');
113-
defineLazyTimer('requestAnimationFrame');
114-
defineLazyTimer('cancelAnimationFrame');
115-
defineLazyTimer('requestIdleCallback');
116-
defineLazyTimer('cancelIdleCallback');
117-
118-
// Set up XHR
119-
// The native XMLHttpRequest in Chrome dev tools is CORS aware and won't
120-
// let you fetch anything from the internet
121-
polyfillGlobal('XMLHttpRequest', () => require('XMLHttpRequest'));
122-
polyfillGlobal('FormData', () => require('FormData'));
123-
124-
polyfillGlobal('fetch', () => require('fetch').fetch);
125-
polyfillGlobal('Headers', () => require('fetch').Headers);
126-
polyfillGlobal('Request', () => require('fetch').Request);
127-
polyfillGlobal('Response', () => require('fetch').Response);
128-
polyfillGlobal('WebSocket', () => require('WebSocket'));
129-
polyfillGlobal('Blob', () => require('Blob'));
130-
polyfillGlobal('File', () => require('File'));
131-
polyfillGlobal('FileReader', () => require('FileReader'));
132-
polyfillGlobal('URL', () => require('URL'));
133-
134-
// Set up alert
135-
if (!global.alert) {
136-
global.alert = function(text) {
137-
// Require Alert on demand. Requiring it too early can lead to issues
138-
// with things like Platform not being fully initialized.
139-
require('Alert').alert('Alert', '' + text);
140-
};
141-
}
142-
143-
// Set up Geolocation
144-
let navigator = global.navigator;
145-
if (navigator === undefined) {
146-
global.navigator = navigator = {};
147-
}
148-
149-
// see https://github.com/facebook/react-native/issues/10881
150-
polyfillObjectProperty(navigator, 'product', () => 'ReactNative');
151-
polyfillObjectProperty(navigator, 'geolocation', () => require('Geolocation'));
152-
153-
// Just to make sure the JS gets packaged up. Wait until the JS environment has
154-
// been initialized before requiring them.
155-
const BatchedBridge = require('BatchedBridge');
156-
BatchedBridge.registerLazyCallableModule('Systrace', () => require('Systrace'));
157-
BatchedBridge.registerLazyCallableModule('JSTimers', () => require('JSTimers'));
158-
BatchedBridge.registerLazyCallableModule('HeapCapture', () =>
159-
require('HeapCapture'),
160-
);
161-
BatchedBridge.registerLazyCallableModule('SamplingProfiler', () =>
162-
require('SamplingProfiler'),
163-
);
164-
BatchedBridge.registerLazyCallableModule('RCTLog', () => require('RCTLog'));
165-
BatchedBridge.registerLazyCallableModule('RCTDeviceEventEmitter', () =>
166-
require('RCTDeviceEventEmitter'),
167-
);
168-
BatchedBridge.registerLazyCallableModule('RCTNativeAppEventEmitter', () =>
169-
require('RCTNativeAppEventEmitter'),
170-
);
171-
BatchedBridge.registerLazyCallableModule('PerformanceLogger', () =>
172-
require('PerformanceLogger'),
173-
);
174-
BatchedBridge.registerLazyCallableModule('JSDevSupportModule', () =>
175-
require('JSDevSupportModule'),
176-
);
177-
178-
global.__fetchSegment = function(
179-
segmentId: number,
180-
options: {|+otaBuildNumber: ?string|},
181-
callback: (?Error) => void,
182-
) {
183-
const {SegmentFetcher} = require('NativeModules');
184-
if (!SegmentFetcher) {
185-
throw new Error(
186-
'SegmentFetcher is missing. Please ensure that it is ' +
187-
'included as a NativeModule.',
188-
);
189-
}
190-
191-
SegmentFetcher.fetchSegment(
192-
segmentId,
193-
options,
194-
(errorObject: ?{message: string, code: string}) => {
195-
if (errorObject) {
196-
const error = new Error(errorObject.message);
197-
(error: any).code = errorObject.code;
198-
callback(error);
199-
}
200-
201-
callback(null);
202-
},
203-
);
204-
};
205-
206-
// Set up devtools
31+
require('setUpGlobals');
32+
require('polyfillES6Collections');
33+
require('setUpSystrace');
34+
require('setUpErrorHandling');
35+
require('checkNativeVersion');
36+
require('polyfillPromise');
37+
require('setUpRegeneratorRuntime');
38+
require('setUpTimers');
39+
require('setUpXHR');
40+
require('setUpAlert');
41+
require('setUpGeolocation');
42+
require('setUpBatchedBridge');
43+
require('setUpSegmentFetcher');
20744
if (__DEV__) {
208-
if (!global.__RCTProfileIsProfiling) {
209-
BatchedBridge.registerCallableModule('HMRClient', require('HMRClient'));
210-
211-
// not when debugging in chrome
212-
// TODO(t12832058) This check is broken
213-
if (!window.document) {
214-
require('setupDevtools');
215-
}
216-
217-
// Set up inspector
218-
const JSInspector = require('JSInspector');
219-
JSInspector.registerAgent(require('NetworkAgent'));
220-
}
45+
require('setUpDeveloperTools');
22146
}
22247

22348
if (startTime != null) {

Libraries/Core/checkNativeVersion.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
* @flow strict-local
8+
* @format
9+
*/
10+
'use strict';
11+
12+
/**
13+
* Check for compatibility between the JS and native code.
14+
* You can use this module directly, or just require InitializeCore.
15+
*/
16+
const ReactNativeVersionCheck = require('ReactNativeVersionCheck');
17+
ReactNativeVersionCheck.checkVersions();
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
* @flow strict-local
8+
* @format
9+
*/
10+
'use strict';
11+
12+
const {polyfillGlobal} = require('PolyfillFunctions');
13+
14+
/**
15+
* Polyfill ES6 collections (Map and Set).
16+
* If you don't need these polyfills, don't use InitializeCore; just directly
17+
* require the modules you need from InitializeCore for setup.
18+
*/
19+
const _shouldPolyfillCollection = require('_shouldPolyfillES6Collection');
20+
if (_shouldPolyfillCollection('Map')) {
21+
polyfillGlobal('Map', () => require('Map'));
22+
}
23+
if (_shouldPolyfillCollection('Set')) {
24+
polyfillGlobal('Set', () => require('Set'));
25+
}

Libraries/Core/polyfillPromise.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
* @flow strict-local
8+
* @format
9+
*/
10+
'use strict';
11+
12+
const {polyfillGlobal} = require('PolyfillFunctions');
13+
14+
/**
15+
* Set up Promise. The native Promise implementation throws the following error:
16+
* ERROR: Event loop not supported.
17+
*
18+
* If you don't need these polyfills, don't use InitializeCore; just directly
19+
* require the modules you need from InitializeCore for setup.
20+
*/
21+
polyfillGlobal('Promise', () => require('Promise'));

Libraries/Core/setUpAlert.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
* @flow strict-local
8+
* @format
9+
*/
10+
'use strict';
11+
12+
/**
13+
* Set up alert().
14+
* You can use this module directly, or just require InitializeCore.
15+
*/
16+
if (!global.alert) {
17+
global.alert = function(text) {
18+
// Require Alert on demand. Requiring it too early can lead to issues
19+
// with things like Platform not being fully initialized.
20+
require('Alert').alert('Alert', '' + text);
21+
};
22+
}

Libraries/Core/setUpBatchedBridge.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
* @flow strict-local
8+
* @format
9+
*/
10+
'use strict';
11+
12+
/**
13+
* Set up the BatchedBridge. This must be done after the other steps in
14+
* InitializeCore to ensure that the JS environment has been initialized.
15+
* You can use this module directly, or just require InitializeCore.
16+
*/
17+
const BatchedBridge = require('BatchedBridge');
18+
BatchedBridge.registerLazyCallableModule('Systrace', () => require('Systrace'));
19+
BatchedBridge.registerLazyCallableModule('JSTimers', () => require('JSTimers'));
20+
BatchedBridge.registerLazyCallableModule('HeapCapture', () =>
21+
require('HeapCapture'),
22+
);
23+
BatchedBridge.registerLazyCallableModule('SamplingProfiler', () =>
24+
require('SamplingProfiler'),
25+
);
26+
BatchedBridge.registerLazyCallableModule('RCTLog', () => require('RCTLog'));
27+
BatchedBridge.registerLazyCallableModule('RCTDeviceEventEmitter', () =>
28+
require('RCTDeviceEventEmitter'),
29+
);
30+
BatchedBridge.registerLazyCallableModule('RCTNativeAppEventEmitter', () =>
31+
require('RCTNativeAppEventEmitter'),
32+
);
33+
BatchedBridge.registerLazyCallableModule('PerformanceLogger', () =>
34+
require('PerformanceLogger'),
35+
);
36+
BatchedBridge.registerLazyCallableModule('JSDevSupportModule', () =>
37+
require('JSDevSupportModule'),
38+
);
39+
40+
if (__DEV__ && !global.__RCTProfileIsProfiling) {
41+
BatchedBridge.registerCallableModule('HMRClient', require('HMRClient'));
42+
}

Libraries/Core/setUpDeveloperTools.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
* @flow strict-local
8+
* @format
9+
*/
10+
'use strict';
11+
12+
/**
13+
* Sets up developer tools for React Native.
14+
* You can use this module directly, or just require InitializeCore.
15+
*/
16+
if (__DEV__) {
17+
if (!global.__RCTProfileIsProfiling) {
18+
// not when debugging in chrome
19+
// TODO(t12832058) This check is broken
20+
if (!window.document) {
21+
require('setupDevtools');
22+
}
23+
24+
// Set up inspector
25+
const JSInspector = require('JSInspector');
26+
JSInspector.registerAgent(require('NetworkAgent'));
27+
}
28+
}

0 commit comments

Comments
 (0)