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:
Janic Duplessis 2016-01-21 07:22:20 -08:00 committed by facebook-github-bot-4
parent d806b75544
commit d33b554f5d
17 changed files with 1032 additions and 1012 deletions

View File

@ -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;
@ -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() {

View File

@ -1,9 +1,6 @@
/* eslint global-strict: 0 */ /* eslint strict: 0 */
(function(GLOBAL) {
/** // TODO: Remove document polyfill now that chrome debugging is in a web worker.
* 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 // The browser defines Text and Image globals by default. If you forget to
// require them, then the error message is very confusing. // require them, then the error message is very confusing.
@ -13,22 +10,21 @@
'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);

View File

@ -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(

View File

@ -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(''));
});
});
}); });
}); });

View File

@ -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;

View File

@ -6,9 +6,7 @@
*/ */
/* 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) {
@ -55,4 +53,3 @@
} }
}); });
} }
})();

View File

@ -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,7 +15,6 @@
// //
// 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 () {
@ -229,4 +227,3 @@
return Array.from(arr); return Array.from(arr);
} }
}; };
})(typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : this);

View File

@ -14,8 +14,7 @@
* @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.
@ -504,5 +503,3 @@
} else { } else {
setupConsole(global); setupConsole(global);
} }
})(this);

View File

@ -13,8 +13,7 @@
* 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,
@ -83,4 +82,3 @@
} }
setupErrorGuard(); setupErrorGuard();
})(this);

View File

@ -1,5 +1,5 @@
/* 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));
@ -23,7 +23,6 @@
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) {
@ -33,18 +32,3 @@
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);
});
};
}

View File

@ -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) {

View File

@ -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();

View File

@ -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();

View File

@ -1,6 +1,5 @@
'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;
@ -69,5 +68,3 @@
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);

View File

@ -1,5 +1,4 @@
/* eslint strict:0 */ /* eslint strict:0 */
(function(global) {
var modules = Object.create(null); var modules = Object.create(null);
var inGuard = false; var inGuard = false;
@ -127,4 +126,3 @@
global.__accept = accept; global.__accept = accept;
} }
})(this);

View File

@ -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();`

View File

@ -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));