Extract polyfillGlobal from InitializeCore
Reviewed By: jeanlauliac Differential Revision: D6987657 fbshipit-source-id: 8762732de671418520376a98bdd724bbb24e4e36
This commit is contained in:
parent
991b7aba57
commit
f7f5dc6649
|
@ -27,6 +27,8 @@
|
|||
*/
|
||||
'use strict';
|
||||
|
||||
const {polyfillObjectProperty, polyfillGlobal} = require('PolyfillFunctions');
|
||||
|
||||
if (global.GLOBAL === undefined) {
|
||||
global.GLOBAL = global;
|
||||
}
|
||||
|
@ -35,8 +37,6 @@ if (global.window === undefined) {
|
|||
global.window = global;
|
||||
}
|
||||
|
||||
const defineLazyObjectProperty = require('defineLazyObjectProperty');
|
||||
|
||||
// Set up collections
|
||||
const _shouldPolyfillCollection = require('_shouldPolyfillES6Collection');
|
||||
if (_shouldPolyfillCollection('Map')) {
|
||||
|
@ -46,50 +46,6 @@ if (_shouldPolyfillCollection('Set')) {
|
|||
polyfillGlobal('Set', () => require('Set'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets an object's property. If a property with the same name exists, this will
|
||||
* replace it but maintain its descriptor configuration. The property will be
|
||||
* replaced with a lazy getter.
|
||||
*
|
||||
* In DEV mode the original property value will be preserved as `original[PropertyName]`
|
||||
* so that, if necessary, it can be restored. For example, if you want to route
|
||||
* network requests through DevTools (to trace them):
|
||||
*
|
||||
* global.XMLHttpRequest = global.originalXMLHttpRequest;
|
||||
*
|
||||
* @see https://github.com/facebook/react-native/issues/934
|
||||
*/
|
||||
function defineLazyProperty<T>(
|
||||
object: Object,
|
||||
name: string,
|
||||
getValue: () => T,
|
||||
): void {
|
||||
const descriptor = Object.getOwnPropertyDescriptor(object, name);
|
||||
if (__DEV__ && descriptor) {
|
||||
const backupName = `original${name[0].toUpperCase()}${name.substr(1)}`;
|
||||
Object.defineProperty(object, backupName, {
|
||||
...descriptor,
|
||||
value: object[name],
|
||||
});
|
||||
}
|
||||
|
||||
const {enumerable, writable, configurable} = descriptor || {};
|
||||
if (descriptor && !configurable) {
|
||||
console.error('Failed to set polyfill. ' + name + ' is not configurable.');
|
||||
return;
|
||||
}
|
||||
|
||||
defineLazyObjectProperty(object, name, {
|
||||
get: getValue,
|
||||
enumerable: enumerable !== false,
|
||||
writable: writable !== false,
|
||||
});
|
||||
}
|
||||
|
||||
function polyfillGlobal<T>(name: string, getValue: () => T): void {
|
||||
defineLazyProperty(global, name, getValue);
|
||||
}
|
||||
|
||||
// Set up process
|
||||
global.process = global.process || {};
|
||||
global.process.env = global.process.env || {};
|
||||
|
@ -191,8 +147,8 @@ if (navigator === undefined) {
|
|||
}
|
||||
|
||||
// see https://github.com/facebook/react-native/issues/10881
|
||||
defineLazyProperty(navigator, 'product', () => 'ReactNative');
|
||||
defineLazyProperty(navigator, 'geolocation', () => require('Geolocation'));
|
||||
polyfillObjectProperty(navigator, 'product', () => 'ReactNative');
|
||||
polyfillObjectProperty(navigator, 'geolocation', () => require('Geolocation'));
|
||||
|
||||
// Just to make sure the JS gets packaged up. Wait until the JS environment has
|
||||
// been initialized before requiring them.
|
||||
|
|
|
@ -0,0 +1,62 @@
|
|||
/**
|
||||
* Copyright (c) 2013-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*
|
||||
* @providesModule PolyfillFunctions
|
||||
* @flow
|
||||
* @format
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const defineLazyObjectProperty = require('defineLazyObjectProperty');
|
||||
|
||||
/**
|
||||
* Sets an object's property. If a property with the same name exists, this will
|
||||
* replace it but maintain its descriptor configuration. The property will be
|
||||
* replaced with a lazy getter.
|
||||
*
|
||||
* In DEV mode the original property value will be preserved as `original[PropertyName]`
|
||||
* so that, if necessary, it can be restored. For example, if you want to route
|
||||
* network requests through DevTools (to trace them):
|
||||
*
|
||||
* global.XMLHttpRequest = global.originalXMLHttpRequest;
|
||||
*
|
||||
* @see https://github.com/facebook/react-native/issues/934
|
||||
*/
|
||||
function polyfillObjectProperty<T>(
|
||||
object: Object,
|
||||
name: string,
|
||||
getValue: () => T,
|
||||
): void {
|
||||
const descriptor = Object.getOwnPropertyDescriptor(object, name);
|
||||
if (__DEV__ && descriptor) {
|
||||
const backupName = `original${name[0].toUpperCase()}${name.substr(1)}`;
|
||||
Object.defineProperty(object, backupName, {
|
||||
...descriptor,
|
||||
value: object[name],
|
||||
});
|
||||
}
|
||||
|
||||
const {enumerable, writable, configurable} = descriptor || {};
|
||||
if (descriptor && !configurable) {
|
||||
console.error('Failed to set polyfill. ' + name + ' is not configurable.');
|
||||
return;
|
||||
}
|
||||
|
||||
defineLazyObjectProperty(object, name, {
|
||||
get: getValue,
|
||||
enumerable: enumerable !== false,
|
||||
writable: writable !== false,
|
||||
});
|
||||
}
|
||||
|
||||
function polyfillGlobal<T>(name: string, getValue: () => T): void {
|
||||
polyfillObjectProperty(global, name, getValue);
|
||||
}
|
||||
|
||||
module.exports = {polyfillObjectProperty, polyfillGlobal};
|
Loading…
Reference in New Issue