Make the packager work with babel strict mode transform
Summary: At the moment we have to disable strict mode for the transform-es2015-modules-commonjs because strict mode leaks to the global scope and breaks the bridge. It was due to the way the polyfills were bundled in the package. To fix it, I wrapped the polyfill modules in an IIFE. Then when strict mode was enabled some polyfills were broken due to strict mode errors so that was fixed too. Also removed the IIFE from the polyfills that included one. This diff doesn't enable the strict mode transform since some internal facebook modules depend on it not being enabled. When #5214 lands we could make the default babel config shipped with OSS react-native use strict mode modules and facebook could just modify the babel config to disable it if needed. This will allow removing `"strict": false` from https://github.com/facebook/react-native/blob/master/packager/react-packager/.babelrc#L16 Fixes #5316 Closes https://github.com/facebook/react-native/pull/5422 Reviewed By: svcscm Differential Revision: D2846422 Pulled By: davidaurelio fb-gh-sync-id: a3e2f8909aa87dabab2b872c61b887e80220fb56
This commit is contained in:
parent
d806b75544
commit
d33b554f5d
|
@ -25,11 +25,11 @@
|
||||||
require('regenerator/runtime');
|
require('regenerator/runtime');
|
||||||
|
|
||||||
if (typeof GLOBAL === 'undefined') {
|
if (typeof GLOBAL === 'undefined') {
|
||||||
GLOBAL = this;
|
global.GLOBAL = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof window === 'undefined') {
|
if (typeof window === 'undefined') {
|
||||||
window = GLOBAL;
|
global.window = GLOBAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
function setUpConsole() {
|
function setUpConsole() {
|
||||||
|
@ -70,6 +70,15 @@ function polyfillGlobal(name, newValue, scope = GLOBAL) {
|
||||||
Object.defineProperty(scope, name, {...descriptor, value: newValue});
|
Object.defineProperty(scope, name, {...descriptor, value: newValue});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Polyfill a module if it is not already defined in `scope`.
|
||||||
|
*/
|
||||||
|
function polyfillIfNeeded(name, polyfill, scope = GLOBAL, descriptor = {}) {
|
||||||
|
if (scope[name] === undefined) {
|
||||||
|
Object.defineProperty(scope, name, {...descriptor, value: polyfill});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function setUpErrorHandler() {
|
function setUpErrorHandler() {
|
||||||
if (global.__fbDisableExceptionsManager) {
|
if (global.__fbDisableExceptionsManager) {
|
||||||
return;
|
return;
|
||||||
|
@ -78,7 +87,7 @@ function setUpErrorHandler() {
|
||||||
function handleError(e, isFatal) {
|
function handleError(e, isFatal) {
|
||||||
try {
|
try {
|
||||||
require('ExceptionsManager').handleException(e, isFatal);
|
require('ExceptionsManager').handleException(e, isFatal);
|
||||||
} catch(ee) {
|
} catch (ee) {
|
||||||
console.log('Failed to print error: ', ee.message);
|
console.log('Failed to print error: ', ee.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -146,7 +155,11 @@ function setUpXHR() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function setUpGeolocation() {
|
function setUpGeolocation() {
|
||||||
GLOBAL.navigator = GLOBAL.navigator || {};
|
polyfillIfNeeded('navigator', {}, GLOBAL, {
|
||||||
|
writable: true,
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true,
|
||||||
|
});
|
||||||
polyfillGlobal('geolocation', require('Geolocation'), GLOBAL.navigator);
|
polyfillGlobal('geolocation', require('Geolocation'), GLOBAL.navigator);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -179,9 +192,9 @@ function setUpProcessEnv() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function setUpNumber() {
|
function setUpNumber() {
|
||||||
Number.EPSILON = Number.EPSILON || Math.pow(2, -52);
|
polyfillIfNeeded('EPSILON', Math.pow(2, -52), Number);
|
||||||
Number.MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || Math.pow(2, 53) - 1;
|
polyfillIfNeeded('MAX_SAFE_INTEGER', Math.pow(2, 53) - 1, Number);
|
||||||
Number.MIN_SAFE_INTEGER = Number.MIN_SAFE_INTEGER || -(Math.pow(2, 53) - 1);
|
polyfillIfNeeded('MIN_SAFE_INTEGER', -(Math.pow(2, 53) - 1), Number);
|
||||||
}
|
}
|
||||||
|
|
||||||
function setUpDevTools() {
|
function setUpDevTools() {
|
||||||
|
|
|
@ -1,34 +1,30 @@
|
||||||
/* eslint global-strict: 0 */
|
/* eslint strict: 0 */
|
||||||
(function(GLOBAL) {
|
|
||||||
/**
|
|
||||||
* The document must be shimmed before anything else that might define the
|
|
||||||
* `ExecutionEnvironment` module (which checks for `document.createElement`).
|
|
||||||
*/
|
|
||||||
|
|
||||||
// The browser defines Text and Image globals by default. If you forget to
|
// TODO: Remove document polyfill now that chrome debugging is in a web worker.
|
||||||
// require them, then the error message is very confusing.
|
|
||||||
function getInvalidGlobalUseError(name) {
|
// The browser defines Text and Image globals by default. If you forget to
|
||||||
|
// require them, then the error message is very confusing.
|
||||||
|
function getInvalidGlobalUseError(name) {
|
||||||
return new Error(
|
return new Error(
|
||||||
'You are trying to render the global ' + name + ' variable as a ' +
|
'You are trying to render the global ' + name + ' variable as a ' +
|
||||||
'React element. You probably forgot to require ' + name + '.'
|
'React element. You probably forgot to require ' + name + '.'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
GLOBAL.Text = {
|
global.Text = {
|
||||||
get defaultProps() {
|
get defaultProps() {
|
||||||
throw getInvalidGlobalUseError('Text');
|
throw getInvalidGlobalUseError('Text');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
GLOBAL.Image = {
|
global.Image = {
|
||||||
get defaultProps() {
|
get defaultProps() {
|
||||||
throw getInvalidGlobalUseError('Image');
|
throw getInvalidGlobalUseError('Image');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// Force `ExecutionEnvironment.canUseDOM` to be false.
|
// Force `ExecutionEnvironment.canUseDOM` to be false.
|
||||||
if (GLOBAL.document) {
|
if (global.document) {
|
||||||
GLOBAL.document.createElement = null;
|
global.document.createElement = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// There is no DOM so MutationObserver doesn't make sense. It is used
|
// There is no DOM so MutationObserver doesn't make sense. It is used
|
||||||
// as feature detection in Bluebird Promise implementation
|
// as feature detection in Bluebird Promise implementation
|
||||||
GLOBAL.MutationObserver = undefined;
|
global.MutationObserver = undefined;
|
||||||
})(this);
|
|
||||||
|
|
|
@ -49,12 +49,13 @@ var TYPES_KEY = keyOf({__types: true});
|
||||||
*/
|
*/
|
||||||
function mixInEventEmitter(klass, types) {
|
function mixInEventEmitter(klass, types) {
|
||||||
invariant(types, 'Must supply set of valid event types');
|
invariant(types, 'Must supply set of valid event types');
|
||||||
invariant(!this.__eventEmitter, 'An active emitter is already mixed in');
|
|
||||||
|
|
||||||
// If this is a constructor, write to the prototype, otherwise write to the
|
// If this is a constructor, write to the prototype, otherwise write to the
|
||||||
// singleton object.
|
// singleton object.
|
||||||
var target = klass.prototype || klass;
|
var target = klass.prototype || klass;
|
||||||
|
|
||||||
|
invariant(!target.__eventEmitter, 'An active emitter is already mixed in');
|
||||||
|
|
||||||
var ctor = klass.constructor;
|
var ctor = klass.constructor;
|
||||||
if (ctor) {
|
if (ctor) {
|
||||||
invariant(
|
invariant(
|
||||||
|
|
|
@ -58,6 +58,14 @@ describe('Resolver', function() {
|
||||||
return module;
|
return module;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function createPolyfill(id, dependencies) {
|
||||||
|
var polyfill = new Polyfill({});
|
||||||
|
polyfill.getName.mockImpl(() => Promise.resolve(id));
|
||||||
|
polyfill.getDependencies.mockImpl(() => Promise.resolve(dependencies));
|
||||||
|
polyfill.isPolyfill.mockReturnValue(true);
|
||||||
|
return polyfill;
|
||||||
|
}
|
||||||
|
|
||||||
describe('getDependencies', function() {
|
describe('getDependencies', function() {
|
||||||
pit('should get dependencies with polyfills', function() {
|
pit('should get dependencies with polyfills', function() {
|
||||||
var module = createModule('index');
|
var module = createModule('index');
|
||||||
|
@ -1020,5 +1028,26 @@ describe('Resolver', function() {
|
||||||
].join('\n'));
|
].join('\n'));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
pit('should resolve polyfills', function () {
|
||||||
|
const depResolver = new Resolver({
|
||||||
|
projectRoot: '/root',
|
||||||
|
});
|
||||||
|
const polyfill = createPolyfill('test polyfill', []);
|
||||||
|
const code = [
|
||||||
|
'global.fetch = () => 1;',
|
||||||
|
].join('');
|
||||||
|
return depResolver.wrapModule(
|
||||||
|
null,
|
||||||
|
polyfill,
|
||||||
|
code
|
||||||
|
).then(processedCode => {
|
||||||
|
expect(processedCode.code).toEqual([
|
||||||
|
'(function(global) {',
|
||||||
|
'global.fetch = () => 1;',
|
||||||
|
"\n})(typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : this);",
|
||||||
|
].join(''));
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -214,7 +214,9 @@ class Resolver {
|
||||||
|
|
||||||
wrapModule(resolutionResponse, module, code) {
|
wrapModule(resolutionResponse, module, code) {
|
||||||
if (module.isPolyfill()) {
|
if (module.isPolyfill()) {
|
||||||
return Promise.resolve({code});
|
return Promise.resolve({
|
||||||
|
code: definePolyfillCode(code),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.resolveRequires(resolutionResponse, module, code).then(
|
return this.resolveRequires(resolutionResponse, module, code).then(
|
||||||
|
@ -239,4 +241,12 @@ function defineModuleCode(moduleName, code) {
|
||||||
].join('');
|
].join('');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function definePolyfillCode(code) {
|
||||||
|
return [
|
||||||
|
'(function(global) {',
|
||||||
|
code,
|
||||||
|
`\n})(typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : this);`,
|
||||||
|
].join('');
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = Resolver;
|
module.exports = Resolver;
|
||||||
|
|
|
@ -5,12 +5,10 @@
|
||||||
* @polyfill
|
* @polyfill
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*eslint-disable */
|
/* eslint-disable */
|
||||||
/*jslint bitwise: true */
|
|
||||||
|
|
||||||
(function(undefined) {
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
|
||||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
|
function findIndex(predicate, context) {
|
||||||
function findIndex(predicate, context) {
|
|
||||||
if (this == null) {
|
if (this == null) {
|
||||||
throw new TypeError(
|
throw new TypeError(
|
||||||
'Array.prototype.findIndex called on null or undefined'
|
'Array.prototype.findIndex called on null or undefined'
|
||||||
|
@ -27,19 +25,19 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Array.prototype.findIndex) {
|
if (!Array.prototype.findIndex) {
|
||||||
Object.defineProperty(Array.prototype, 'findIndex', {
|
Object.defineProperty(Array.prototype, 'findIndex', {
|
||||||
enumerable: false,
|
enumerable: false,
|
||||||
writable: true,
|
writable: true,
|
||||||
configurable: true,
|
configurable: true,
|
||||||
value: findIndex
|
value: findIndex
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
|
||||||
if (!Array.prototype.find) {
|
if (!Array.prototype.find) {
|
||||||
Object.defineProperty(Array.prototype, 'find', {
|
Object.defineProperty(Array.prototype, 'find', {
|
||||||
enumerable: false,
|
enumerable: false,
|
||||||
writable: true,
|
writable: true,
|
||||||
|
@ -54,5 +52,4 @@
|
||||||
return index === -1 ? undefined : this[index];
|
return index === -1 ? undefined : this[index];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
})();
|
|
||||||
|
|
|
@ -7,8 +7,7 @@
|
||||||
* of patent rights can be found in the PATENTS file in the same directory.
|
* of patent rights can be found in the PATENTS file in the same directory.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* global self:true */
|
/* eslint-disable */
|
||||||
/* eslint-disable strict */
|
|
||||||
|
|
||||||
// Created by running:
|
// Created by running:
|
||||||
// require('babel-core').buildExternalHelpers('_extends classCallCheck createClass createRawReactElement defineProperty get inherits interopRequireDefault interopRequireWildcard objectWithoutProperties possibleConstructorReturn slicedToArray taggedTemplateLiteral toArray toConsumableArray '.split(' '))
|
// require('babel-core').buildExternalHelpers('_extends classCallCheck createClass createRawReactElement defineProperty get inherits interopRequireDefault interopRequireWildcard objectWithoutProperties possibleConstructorReturn slicedToArray taggedTemplateLiteral toArray toConsumableArray '.split(' '))
|
||||||
|
@ -16,10 +15,9 @@
|
||||||
//
|
//
|
||||||
// actually, that's a lie, because babel6 omits _extends and createRawReactElement
|
// actually, that's a lie, because babel6 omits _extends and createRawReactElement
|
||||||
|
|
||||||
(function (global) {
|
var babelHelpers = global.babelHelpers = {};
|
||||||
var babelHelpers = global.babelHelpers = {};
|
|
||||||
|
|
||||||
babelHelpers.createRawReactElement = (function () {
|
babelHelpers.createRawReactElement = (function () {
|
||||||
var REACT_ELEMENT_TYPE = typeof Symbol === "function" && Symbol.for && Symbol.for("react.element") || 0xeac7;
|
var REACT_ELEMENT_TYPE = typeof Symbol === "function" && Symbol.for && Symbol.for("react.element") || 0xeac7;
|
||||||
return function createRawReactElement(type, key, props) {
|
return function createRawReactElement(type, key, props) {
|
||||||
return {
|
return {
|
||||||
|
@ -31,15 +29,15 @@
|
||||||
_owner: null
|
_owner: null
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|
||||||
babelHelpers.classCallCheck = function (instance, Constructor) {
|
babelHelpers.classCallCheck = function (instance, Constructor) {
|
||||||
if (!(instance instanceof Constructor)) {
|
if (!(instance instanceof Constructor)) {
|
||||||
throw new TypeError("Cannot call a class as a function");
|
throw new TypeError("Cannot call a class as a function");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
babelHelpers.createClass = (function () {
|
babelHelpers.createClass = (function () {
|
||||||
function defineProperties(target, props) {
|
function defineProperties(target, props) {
|
||||||
for (var i = 0; i < props.length; i++) {
|
for (var i = 0; i < props.length; i++) {
|
||||||
var descriptor = props[i];
|
var descriptor = props[i];
|
||||||
|
@ -55,9 +53,9 @@
|
||||||
if (staticProps) defineProperties(Constructor, staticProps);
|
if (staticProps) defineProperties(Constructor, staticProps);
|
||||||
return Constructor;
|
return Constructor;
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|
||||||
babelHelpers.defineProperty = function (obj, key, value) {
|
babelHelpers.defineProperty = function (obj, key, value) {
|
||||||
if (key in obj) {
|
if (key in obj) {
|
||||||
Object.defineProperty(obj, key, {
|
Object.defineProperty(obj, key, {
|
||||||
value: value,
|
value: value,
|
||||||
|
@ -70,9 +68,9 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
return obj;
|
return obj;
|
||||||
};
|
};
|
||||||
|
|
||||||
babelHelpers._extends = babelHelpers.extends = Object.assign || function (target) {
|
babelHelpers._extends = babelHelpers.extends = Object.assign || function (target) {
|
||||||
for (var i = 1; i < arguments.length; i++) {
|
for (var i = 1; i < arguments.length; i++) {
|
||||||
var source = arguments[i];
|
var source = arguments[i];
|
||||||
|
|
||||||
|
@ -84,9 +82,9 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
return target;
|
return target;
|
||||||
};
|
};
|
||||||
|
|
||||||
babelHelpers.get = function get(object, property, receiver) {
|
babelHelpers.get = function get(object, property, receiver) {
|
||||||
if (object === null) object = Function.prototype;
|
if (object === null) object = Function.prototype;
|
||||||
var desc = Object.getOwnPropertyDescriptor(object, property);
|
var desc = Object.getOwnPropertyDescriptor(object, property);
|
||||||
|
|
||||||
|
@ -109,9 +107,9 @@
|
||||||
|
|
||||||
return getter.call(receiver);
|
return getter.call(receiver);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
babelHelpers.inherits = function (subClass, superClass) {
|
babelHelpers.inherits = function (subClass, superClass) {
|
||||||
if (typeof superClass !== "function" && superClass !== null) {
|
if (typeof superClass !== "function" && superClass !== null) {
|
||||||
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
|
throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
|
||||||
}
|
}
|
||||||
|
@ -125,15 +123,15 @@
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
|
if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
|
||||||
};
|
};
|
||||||
|
|
||||||
babelHelpers.interopRequireDefault = function (obj) {
|
babelHelpers.interopRequireDefault = function (obj) {
|
||||||
return obj && obj.__esModule ? obj : {
|
return obj && obj.__esModule ? obj : {
|
||||||
default: obj
|
default: obj
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
babelHelpers.interopRequireWildcard = function (obj) {
|
babelHelpers.interopRequireWildcard = function (obj) {
|
||||||
if (obj && obj.__esModule) {
|
if (obj && obj.__esModule) {
|
||||||
return obj;
|
return obj;
|
||||||
} else {
|
} else {
|
||||||
|
@ -148,9 +146,9 @@
|
||||||
newObj.default = obj;
|
newObj.default = obj;
|
||||||
return newObj;
|
return newObj;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
babelHelpers.objectWithoutProperties = function (obj, keys) {
|
babelHelpers.objectWithoutProperties = function (obj, keys) {
|
||||||
var target = {};
|
var target = {};
|
||||||
|
|
||||||
for (var i in obj) {
|
for (var i in obj) {
|
||||||
|
@ -160,17 +158,17 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
return target;
|
return target;
|
||||||
};
|
};
|
||||||
|
|
||||||
babelHelpers.possibleConstructorReturn = function (self, call) {
|
babelHelpers.possibleConstructorReturn = function (self, call) {
|
||||||
if (!self) {
|
if (!self) {
|
||||||
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
||||||
}
|
}
|
||||||
|
|
||||||
return call && (typeof call === "object" || typeof call === "function") ? call : self;
|
return call && (typeof call === "object" || typeof call === "function") ? call : self;
|
||||||
};
|
};
|
||||||
|
|
||||||
babelHelpers.slicedToArray = (function () {
|
babelHelpers.slicedToArray = (function () {
|
||||||
function sliceIterator(arr, i) {
|
function sliceIterator(arr, i) {
|
||||||
var _arr = [];
|
var _arr = [];
|
||||||
var _n = true;
|
var _n = true;
|
||||||
|
@ -206,21 +204,21 @@
|
||||||
throw new TypeError("Invalid attempt to destructure non-iterable instance");
|
throw new TypeError("Invalid attempt to destructure non-iterable instance");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|
||||||
babelHelpers.taggedTemplateLiteral = function (strings, raw) {
|
babelHelpers.taggedTemplateLiteral = function (strings, raw) {
|
||||||
return Object.freeze(Object.defineProperties(strings, {
|
return Object.freeze(Object.defineProperties(strings, {
|
||||||
raw: {
|
raw: {
|
||||||
value: Object.freeze(raw)
|
value: Object.freeze(raw)
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
|
|
||||||
babelHelpers.toArray = function (arr) {
|
babelHelpers.toArray = function (arr) {
|
||||||
return Array.isArray(arr) ? arr : Array.from(arr);
|
return Array.isArray(arr) ? arr : Array.from(arr);
|
||||||
};
|
};
|
||||||
|
|
||||||
babelHelpers.toConsumableArray = function (arr) {
|
babelHelpers.toConsumableArray = function (arr) {
|
||||||
if (Array.isArray(arr)) {
|
if (Array.isArray(arr)) {
|
||||||
for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
|
for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
|
||||||
|
|
||||||
|
@ -228,5 +226,4 @@
|
||||||
} else {
|
} else {
|
||||||
return Array.from(arr);
|
return Array.from(arr);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
})(typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : this);
|
|
||||||
|
|
|
@ -14,10 +14,9 @@
|
||||||
* @nolint
|
* @nolint
|
||||||
*/
|
*/
|
||||||
|
|
||||||
(function(global) {
|
/* eslint-disable */
|
||||||
'use strict';
|
|
||||||
|
|
||||||
var inspect = (function() {
|
var inspect = (function() {
|
||||||
// Copyright Joyent, Inc. and other Node contributors.
|
// Copyright Joyent, Inc. and other Node contributors.
|
||||||
//
|
//
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
|
@ -354,18 +353,18 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
return inspect;
|
return inspect;
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
|
||||||
var OBJECT_COLUMN_NAME = '(index)';
|
var OBJECT_COLUMN_NAME = '(index)';
|
||||||
var LOG_LEVELS = {
|
var LOG_LEVELS = {
|
||||||
trace: 0,
|
trace: 0,
|
||||||
info: 1,
|
info: 1,
|
||||||
warn: 2,
|
warn: 2,
|
||||||
error: 3
|
error: 3
|
||||||
};
|
};
|
||||||
|
|
||||||
function setupConsole(global) {
|
function setupConsole(global) {
|
||||||
if (!global.nativeLoggingHook) {
|
if (!global.nativeLoggingHook) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -497,12 +496,10 @@
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof module !== 'undefined') {
|
if (typeof module !== 'undefined') {
|
||||||
module.exports = setupConsole;
|
module.exports = setupConsole;
|
||||||
} else {
|
} else {
|
||||||
setupConsole(global);
|
setupConsole(global);
|
||||||
}
|
}
|
||||||
|
|
||||||
})(this);
|
|
||||||
|
|
|
@ -13,9 +13,8 @@
|
||||||
* before any of the modules, this ErrorUtils must be defined (and the handler
|
* before any of the modules, this ErrorUtils must be defined (and the handler
|
||||||
* set) globally before requiring anything.
|
* set) globally before requiring anything.
|
||||||
*/
|
*/
|
||||||
/* eslint global-strict:0 */
|
/* eslint strict:0 */
|
||||||
(function(global) {
|
var ErrorUtils = {
|
||||||
var ErrorUtils = {
|
|
||||||
_inGuard: 0,
|
_inGuard: 0,
|
||||||
_globalHandler: null,
|
_globalHandler: null,
|
||||||
setGlobalHandler: function(fun) {
|
setGlobalHandler: function(fun) {
|
||||||
|
@ -67,20 +66,19 @@
|
||||||
|
|
||||||
return guarded;
|
return guarded;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
global.ErrorUtils = ErrorUtils;
|
global.ErrorUtils = ErrorUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is the error handler that is called when we encounter an exception
|
* This is the error handler that is called when we encounter an exception
|
||||||
* when loading a module. This will report any errors encountered before
|
* when loading a module. This will report any errors encountered before
|
||||||
* ExceptionsManager is configured.
|
* ExceptionsManager is configured.
|
||||||
*/
|
*/
|
||||||
function setupErrorGuard() {
|
function setupErrorGuard() {
|
||||||
var onError = function(e) {
|
var onError = function(e) {
|
||||||
global.console.error('Error: ' + e.message + ', stack:\n' + e.stack);
|
global.console.error('Error: ' + e.message + ', stack:\n' + e.stack);
|
||||||
};
|
};
|
||||||
global.ErrorUtils.setGlobalHandler(onError);
|
global.ErrorUtils.setGlobalHandler(onError);
|
||||||
}
|
}
|
||||||
|
|
||||||
setupErrorGuard();
|
setupErrorGuard();
|
||||||
})(this);
|
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
/* eslint global-strict:0 */
|
/* eslint strict:0 */
|
||||||
(global => {
|
|
||||||
let loadBundlesOnNative = (bundles) =>
|
let loadBundlesOnNative = (bundles) =>
|
||||||
new Promise((resolve) =>
|
new Promise((resolve) =>
|
||||||
require('NativeModules').RCTBundlesLoader.loadBundles(bundles, resolve));
|
require('NativeModules').RCTBundlesLoader.loadBundles(bundles, resolve));
|
||||||
|
|
||||||
let requestedBundles = Object.create(null);
|
let requestedBundles = Object.create(null);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a promise that is fulfilled once all the indicated bundles are
|
* Returns a promise that is fulfilled once all the indicated bundles are
|
||||||
* loaded into memory and injected into the JS engine.
|
* loaded into memory and injected into the JS engine.
|
||||||
* This invokation might need to go through the bridge
|
* This invokation might need to go through the bridge
|
||||||
|
@ -20,10 +20,9 @@
|
||||||
*
|
*
|
||||||
* Note this function should only be invoked by generated code.
|
* Note this function should only be invoked by generated code.
|
||||||
*/
|
*/
|
||||||
global.__loadBundles = function(bundles) {
|
global.__loadBundles = function(bundles) {
|
||||||
// split bundles by whether they've already been requested or not
|
// split bundles by whether they've already been requested or not
|
||||||
const bundlesToRequest = bundles.filter(b => !requestedBundles[b]);
|
const bundlesToRequest = bundles.filter(b => !requestedBundles[b]);
|
||||||
const bundlesAlreadyRequested = bundles.filter(b => !!requestedBundles[b]);
|
|
||||||
|
|
||||||
// keep a reference to the promise loading each bundle
|
// keep a reference to the promise loading each bundle
|
||||||
if (bundlesToRequest.length > 0) {
|
if (bundlesToRequest.length > 0) {
|
||||||
|
@ -32,19 +31,4 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
return Promise.all(bundles.map(bundle => requestedBundles[bundle]));
|
return Promise.all(bundles.map(bundle => requestedBundles[bundle]));
|
||||||
};
|
};
|
||||||
})(global);
|
|
||||||
|
|
||||||
|
|
||||||
// turns a callback async based function into a promised based one
|
|
||||||
function promisify(fn) {
|
|
||||||
return function() {
|
|
||||||
var self = this;
|
|
||||||
var args = Array.prototype.slice.call(arguments);
|
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
args.push(resolve);
|
|
||||||
fn.apply(self, args);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
|
|
||||||
// WARNING: This is an optimized version that fails on hasOwnProperty checks
|
// WARNING: This is an optimized version that fails on hasOwnProperty checks
|
||||||
// and non objects. It's not spec-compliant. It's a perf optimization.
|
// and non objects. It's not spec-compliant. It's a perf optimization.
|
||||||
/* eslint global-strict:0 */
|
/* eslint strict:0 */
|
||||||
Object.assign = function(target, sources) {
|
Object.assign = function(target, sources) {
|
||||||
if (__DEV__) {
|
if (__DEV__) {
|
||||||
if (target == null) {
|
if (target == null) {
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
/* eslint global-strict:0 */
|
/* eslint strict:0 */
|
||||||
__DEV__ = false;
|
global.__DEV__ = false;
|
||||||
|
|
||||||
/* global __BUNDLE_START_TIME__:true */
|
global.__BUNDLE_START_TIME__ = Date.now();
|
||||||
__BUNDLE_START_TIME__ = Date.now();
|
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
/* eslint global-strict:0 */
|
/* eslint strict:0 */
|
||||||
__DEV__ = true;
|
global.__DEV__ = true;
|
||||||
|
|
||||||
/* global __BUNDLE_START_TIME__:true */
|
global.__BUNDLE_START_TIME__ = Date.now();
|
||||||
__BUNDLE_START_TIME__ = Date.now();
|
|
||||||
|
|
|
@ -1,37 +1,36 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
((global) => {
|
const {ErrorUtils, __nativeRequire} = global;
|
||||||
const {ErrorUtils, __nativeRequire} = global;
|
global.require = require;
|
||||||
global.require = require;
|
global.__d = define;
|
||||||
global.__d = define;
|
|
||||||
|
|
||||||
const modules = Object.create(null);
|
const modules = Object.create(null);
|
||||||
|
|
||||||
const loadModule = ErrorUtils ?
|
const loadModule = ErrorUtils ?
|
||||||
guardedLoadModule : loadModuleImplementation;
|
guardedLoadModule : loadModuleImplementation;
|
||||||
|
|
||||||
function define(moduleId, factory) {
|
function define(moduleId, factory) {
|
||||||
modules[moduleId] = {
|
modules[moduleId] = {
|
||||||
factory,
|
factory,
|
||||||
hasError: false,
|
hasError: false,
|
||||||
exports: undefined,
|
exports: undefined,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function require(moduleId) {
|
function require(moduleId) {
|
||||||
const module = modules[moduleId];
|
const module = modules[moduleId];
|
||||||
return module && module.exports || loadModule(moduleId, module);
|
return module && module.exports || loadModule(moduleId, module);
|
||||||
}
|
}
|
||||||
|
|
||||||
function guardedLoadModule(moduleId, module) {
|
function guardedLoadModule(moduleId, module) {
|
||||||
try {
|
try {
|
||||||
return loadModuleImplementation(moduleId, module);
|
return loadModuleImplementation(moduleId, module);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
ErrorUtils.reportFatalError(e);
|
ErrorUtils.reportFatalError(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadModuleImplementation(moduleId, module) {
|
function loadModuleImplementation(moduleId, module) {
|
||||||
if (!module) {
|
if (!module) {
|
||||||
__nativeRequire(moduleId);
|
__nativeRequire(moduleId);
|
||||||
module = modules[moduleId];
|
module = modules[moduleId];
|
||||||
|
@ -51,23 +50,21 @@
|
||||||
const moduleObject = {exports};
|
const moduleObject = {exports};
|
||||||
factory(global, require, moduleObject, exports);
|
factory(global, require, moduleObject, exports);
|
||||||
return (module.exports = moduleObject.exports);
|
return (module.exports = moduleObject.exports);
|
||||||
} catch(e) {
|
} catch (e) {
|
||||||
module.hasError = true;
|
module.hasError = true;
|
||||||
module.exports = undefined;
|
module.exports = undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function unknownModuleError(id) {
|
function unknownModuleError(id) {
|
||||||
let message = 'Requiring unknown module "' + id + '".';
|
let message = 'Requiring unknown module "' + id + '".';
|
||||||
if (__DEV__) {
|
if (__DEV__) {
|
||||||
message +=
|
message +=
|
||||||
'If you are sure the module is there, try restarting the packager.';
|
'If you are sure the module is there, try restarting the packager.';
|
||||||
}
|
}
|
||||||
return Error(message);
|
return Error(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
function moduleThrewError(id) {
|
function moduleThrewError(id) {
|
||||||
return Error('Requiring module "' + id + '", which threw an exception.');
|
return Error('Requiring module "' + id + '", which threw an exception.');
|
||||||
}
|
}
|
||||||
|
|
||||||
})(this);
|
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
/* eslint strict:0 */
|
/* eslint strict:0 */
|
||||||
(function(global) {
|
var modules = Object.create(null);
|
||||||
var modules = Object.create(null);
|
var inGuard = false;
|
||||||
var inGuard = false;
|
|
||||||
|
|
||||||
function define(id, factory) {
|
function define(id, factory) {
|
||||||
modules[id] = {
|
modules[id] = {
|
||||||
factory,
|
factory,
|
||||||
module: {exports: {}},
|
module: {exports: {}},
|
||||||
|
@ -21,18 +20,18 @@
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function require(id) {
|
function require(id) {
|
||||||
var mod = modules[id];
|
var mod = modules[id];
|
||||||
if (mod && mod.isInitialized) {
|
if (mod && mod.isInitialized) {
|
||||||
return mod.module.exports;
|
return mod.module.exports;
|
||||||
}
|
}
|
||||||
|
|
||||||
return requireImpl(id);
|
return requireImpl(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
function requireImpl(id) {
|
function requireImpl(id) {
|
||||||
if (global.ErrorUtils && !inGuard) {
|
if (global.ErrorUtils && !inGuard) {
|
||||||
inGuard = true;
|
inGuard = true;
|
||||||
var returnValue;
|
var returnValue;
|
||||||
|
@ -79,22 +78,22 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
return mod.module.exports;
|
return mod.module.exports;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Systrace = __DEV__ && (() => {
|
const Systrace = __DEV__ && (() => {
|
||||||
var _Systrace;
|
var _Systrace;
|
||||||
try {
|
try {
|
||||||
_Systrace = require('Systrace');
|
_Systrace = require('Systrace');
|
||||||
} catch(e) {}
|
} catch (e) {}
|
||||||
|
|
||||||
return _Systrace && _Systrace.beginEvent ?
|
return _Systrace && _Systrace.beginEvent ?
|
||||||
_Systrace : { beginEvent: () => {}, endEvent: () => {} };
|
_Systrace : { beginEvent: () => {}, endEvent: () => {} };
|
||||||
});
|
});
|
||||||
|
|
||||||
global.__d = define;
|
global.__d = define;
|
||||||
global.require = require;
|
global.require = require;
|
||||||
|
|
||||||
if (__DEV__) { // HMR
|
if (__DEV__) { // HMR
|
||||||
function accept(id, factory) {
|
function accept(id, factory) {
|
||||||
var mod = modules[id];
|
var mod = modules[id];
|
||||||
|
|
||||||
|
@ -126,5 +125,4 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
global.__accept = accept;
|
global.__accept = accept;
|
||||||
}
|
}
|
||||||
})(this);
|
|
||||||
|
|
|
@ -33,7 +33,7 @@ const trim = (str) =>
|
||||||
describe('dead-module-elimination', () => {
|
describe('dead-module-elimination', () => {
|
||||||
it('should inline __DEV__', () => {
|
it('should inline __DEV__', () => {
|
||||||
compare(
|
compare(
|
||||||
`__DEV__ = false;
|
`global.__DEV__ = false;
|
||||||
var foo = __DEV__;`,
|
var foo = __DEV__;`,
|
||||||
`var foo = false;`
|
`var foo = false;`
|
||||||
);
|
);
|
||||||
|
@ -41,7 +41,7 @@ describe('dead-module-elimination', () => {
|
||||||
|
|
||||||
it('should accept unary operators with literals', () => {
|
it('should accept unary operators with literals', () => {
|
||||||
compare(
|
compare(
|
||||||
`__DEV__ = !1;
|
`global.__DEV__ = !1;
|
||||||
var foo = __DEV__;`,
|
var foo = __DEV__;`,
|
||||||
`var foo = false;`
|
`var foo = false;`
|
||||||
);
|
);
|
||||||
|
@ -49,7 +49,7 @@ describe('dead-module-elimination', () => {
|
||||||
|
|
||||||
it('should kill dead branches', () => {
|
it('should kill dead branches', () => {
|
||||||
compare(
|
compare(
|
||||||
`__DEV__ = false;
|
`global.__DEV__ = false;
|
||||||
if (__DEV__) {
|
if (__DEV__) {
|
||||||
doSomething();
|
doSomething();
|
||||||
}`,
|
}`,
|
||||||
|
@ -74,7 +74,7 @@ describe('dead-module-elimination', () => {
|
||||||
|
|
||||||
it('should kill modules referenced only from dead branches', () => {
|
it('should kill modules referenced only from dead branches', () => {
|
||||||
compare(
|
compare(
|
||||||
`__DEV__ = false;
|
`global.__DEV__ = false;
|
||||||
__d('bar', function() {});
|
__d('bar', function() {});
|
||||||
if (__DEV__) { require('bar'); }`,
|
if (__DEV__) { require('bar'); }`,
|
||||||
``
|
``
|
||||||
|
@ -83,7 +83,7 @@ describe('dead-module-elimination', () => {
|
||||||
|
|
||||||
it('should replace logical expressions with the result', () => {
|
it('should replace logical expressions with the result', () => {
|
||||||
compare(
|
compare(
|
||||||
`__DEV__ = false;
|
`global.__DEV__ = false;
|
||||||
__d('bar', function() {});
|
__d('bar', function() {});
|
||||||
__DEV__ && require('bar');`,
|
__DEV__ && require('bar');`,
|
||||||
`false;`
|
`false;`
|
||||||
|
@ -92,7 +92,7 @@ describe('dead-module-elimination', () => {
|
||||||
|
|
||||||
it('should keep if result branch', () => {
|
it('should keep if result branch', () => {
|
||||||
compare(
|
compare(
|
||||||
`__DEV__ = false;
|
`global.__DEV__ = false;
|
||||||
__d('bar', function() {});
|
__d('bar', function() {});
|
||||||
if (__DEV__) {
|
if (__DEV__) {
|
||||||
killWithFire();
|
killWithFire();
|
||||||
|
@ -106,7 +106,7 @@ describe('dead-module-elimination', () => {
|
||||||
|
|
||||||
it('should replace falsy ternaries with alternate expression', () => {
|
it('should replace falsy ternaries with alternate expression', () => {
|
||||||
compare(
|
compare(
|
||||||
`__DEV__ = false;
|
`global.__DEV__ = false;
|
||||||
__DEV__ ? foo() : bar();
|
__DEV__ ? foo() : bar();
|
||||||
`,
|
`,
|
||||||
`bar();`
|
`bar();`
|
||||||
|
|
|
@ -60,7 +60,12 @@ module.exports = function () {
|
||||||
AssignmentExpression(path) {
|
AssignmentExpression(path) {
|
||||||
const { node } = path;
|
const { node } = path;
|
||||||
|
|
||||||
if (node.left.type === 'Identifier' && node.left.name === '__DEV__') {
|
if (
|
||||||
|
node.left.type === 'MemberExpression' &&
|
||||||
|
node.left.object.name === 'global' &&
|
||||||
|
node.left.property.type === 'Identifier' &&
|
||||||
|
node.left.property.name === '__DEV__'
|
||||||
|
) {
|
||||||
var value;
|
var value;
|
||||||
if (node.right.type === 'BooleanLiteral') {
|
if (node.right.type === 'BooleanLiteral') {
|
||||||
value = node.right.value;
|
value = node.right.value;
|
||||||
|
@ -73,7 +78,7 @@ module.exports = function () {
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
globals[node.left.name] = value;
|
globals[node.left.property.name] = value;
|
||||||
|
|
||||||
// workaround babel/source map bug - the minifier should strip it
|
// workaround babel/source map bug - the minifier should strip it
|
||||||
path.replaceWith(t.booleanLiteral(value));
|
path.replaceWith(t.booleanLiteral(value));
|
||||||
|
|
Loading…
Reference in New Issue