@ignore-signedsource [react-native-packager] use a single require implementation

Reviewed By: javache, bestander

Differential Revision: D3252718

fb-gh-sync-id: bfd85acc28dd6e2df72a3227743514cb6f8c32f1
fbshipit-source-id: bfd85acc28dd6e2df72a3227743514cb6f8c32f1
This commit is contained in:
David Aurelio 2016-05-03 15:18:10 -07:00 committed by Facebook Github Bot 6
parent 1fb8643cb6
commit ee5a1deb0b
3 changed files with 106 additions and 199 deletions

View File

@ -163,9 +163,7 @@ class Resolver {
? path.join(__dirname, 'polyfills/prelude_dev.js') ? path.join(__dirname, 'polyfills/prelude_dev.js')
: path.join(__dirname, 'polyfills/prelude.js'); : path.join(__dirname, 'polyfills/prelude.js');
const moduleSystem = opts.unbundle const moduleSystem = path.join(__dirname, 'polyfills/require.js');
? path.join(__dirname, 'polyfills/require-unbundle.js')
: path.join(__dirname, 'polyfills/require.js');
return [ return [
prelude, prelude,

View File

@ -1,117 +0,0 @@
/**
* 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.
*/
'use strict';
global.require = require;
global.__d = define;
const modules = Object.create(null);
function define(moduleId, factory) {
if (moduleId in modules) {
// prevent repeated calls to `global.nativeRequire` to overwrite modules
// that are already loaded
return;
}
modules[moduleId] = {
factory,
hasError: false,
isInitialized: false,
exports: undefined,
};
}
function require(moduleId) {
const module = modules[moduleId];
return module && module.isInitialized
? module.exports
: guardedLoadModule(moduleId, module);
}
var inGuard = false;
function guardedLoadModule(moduleId, module) {
if (global.ErrorUtils && !inGuard) {
inGuard = true;
var returnValue;
try {
returnValue = loadModuleImplementation(moduleId, module);
} catch (e) {
global.ErrorUtils.reportFatalError(e);
}
inGuard = false;
return returnValue;
} else {
return loadModuleImplementation(moduleId, module);
}
}
function loadModuleImplementation(moduleId, module) {
if (!module) {
global.nativeRequire(moduleId);
module = modules[moduleId];
}
if (!module) {
throw unknownModuleError(moduleId);
}
if (module.hasError) {
throw moduleThrewError(moduleId);
}
// `require` calls int the require polyfill itself are not analyzed and
// replaced so that they use numeric module IDs.
// The systrace module will expose itself on the require function so that
// it can be used here.
// TODO(davidaurelio) Scan polyfills for dependencies, too (t9759686)
if (__DEV__) {
var {Systrace} = require;
}
const exports = module.exports = {};
module.isInitialized = true;
const {factory} = module;
try {
if (__DEV__) {
Systrace.beginEvent('JS_require_' + moduleId);
}
const moduleObject = {exports};
factory(global, require, moduleObject, exports);
module.factory = undefined;
if (__DEV__) {
Systrace.endEvent();
}
return (module.exports = moduleObject.exports);
} catch (e) {
module.isInitialized = false;
module.hasError = true;
module.exports = undefined;
throw e;
}
}
function unknownModuleError(id) {
let message = 'Requiring unknown module "' + id + '".';
if (__DEV__) {
message +=
'If you are sure the module is there, try restarting the packager or running "npm install".';
}
return Error(message);
}
function moduleThrewError(id) {
return Error('Requiring module "' + id + '", which threw an exception.');
}
if (__DEV__) {
require.Systrace = { beginEvent: () => {}, endEvent: () => {} };
}

View File

@ -7,65 +7,67 @@
* 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.
*/ */
/* eslint strict:0 */ 'use strict';
var modules = Object.create(null);
var inGuard = false;
function define(id, factory) { global.require = require;
modules[id] = { global.__d = define;
const modules = Object.create(null);
function define(moduleId, factory) {
if (moduleId in modules) {
// prevent repeated calls to `global.nativeRequire` to overwrite modules
// that are already loaded
return;
}
modules[moduleId] = {
factory, factory,
module: {exports: {}},
isInitialized: false,
hasError: false, hasError: false,
isInitialized: false,
exports: undefined,
}; };
if (__DEV__) { // HMR if (__DEV__) { // HMR
Object.assign(modules[id].module, { modules[moduleId].hot = createHotReloadingObject();
hot: {
acceptCallback: null,
accept: function(callback) {
modules[id].module.hot.acceptCallback = callback;
}
}
});
} }
} }
function require(id) { function require(moduleId) {
var mod = modules[id]; const module = modules[moduleId];
if (mod && mod.isInitialized) { return module && module.isInitialized
return mod.module.exports; ? module.exports
} : guardedLoadModule(moduleId, module);
return requireImpl(id);
} }
function requireImpl(id) { var inGuard = false;
if (global.ErrorUtils && !inGuard) { function guardedLoadModule(moduleId, module) {
if (!inGuard && global.ErrorUtils) {
inGuard = true; inGuard = true;
var returnValue; var returnValue;
try { try {
returnValue = requireImpl(id); returnValue = loadModuleImplementation(moduleId, module);
} catch (e) { } catch (e) {
global.ErrorUtils.reportFatalError(e); global.ErrorUtils.reportFatalError(e);
} }
inGuard = false; inGuard = false;
return returnValue; return returnValue;
} else {
return loadModuleImplementation(moduleId, module);
}
}
function loadModuleImplementation(moduleId, module) {
const nativeRequire = global.nativeRequire;
if (!module && nativeRequire) {
nativeRequire(moduleId);
module = modules[moduleId];
} }
var mod = modules[id]; if (!module) {
if (!mod) { throw unknownModuleError(moduleId);
var msg = 'Requiring unknown module "' + id + '"';
if (__DEV__) {
msg += '. If you are sure the module is there, try restarting the packager or running "npm install".';
}
throw new Error(msg);
} }
if (mod.hasError) { if (module.hasError) {
throw new Error( throw moduleThrewError(moduleId);
'Requiring module "' + id + '" which threw an exception'
);
} }
// `require` calls int the require polyfill itself are not analyzed and // `require` calls int the require polyfill itself are not analyzed and
@ -77,49 +79,95 @@ function requireImpl(id) {
var {Systrace} = require; var {Systrace} = require;
} }
// We must optimistically mark module as initialized before running the
// factory to keep any require cycles inside the factory from causing an
// infinite require loop.
module.isInitialized = true;
const exports = module.exports = {};
const {factory} = module;
try { try {
// We must optimistically mark mod as initialized before running the factory to keep any
// require cycles inside the factory from causing an infinite require loop.
mod.isInitialized = true;
if (__DEV__) { if (__DEV__) {
Systrace.beginEvent('JS_require_' + id); Systrace.beginEvent('JS_require_' + moduleId);
}
const moduleObject = {exports};
if (__DEV__ && module.hot) {
moduleObject.hot = module.hot;
} }
// keep args in sync with with defineModuleCode in // keep args in sync with with defineModuleCode in
// packager/react-packager/src/Resolver/index.js // packager/react-packager/src/Resolver/index.js
mod.factory.call(global, global, require, mod.module, mod.module.exports); factory(global, require, moduleObject, exports);
mod.factory = undefined; module.factory = undefined;
if (__DEV__) { if (__DEV__) {
Systrace.endEvent(); Systrace.endEvent();
} }
return (module.exports = moduleObject.exports);
} catch (e) { } catch (e) {
mod.hasError = true; module.hasError = true;
mod.isInitialized = false; module.isInitialized = false;
module.exports = undefined;
throw e; throw e;
} }
}
return mod.module.exports; function unknownModuleError(id) {
let message = 'Requiring unknown module "' + id + '".';
if (__DEV__) {
message +=
'If you are sure the module is there, try restarting the packager or running "npm install".';
}
return Error(message);
}
function moduleThrewError(id) {
return Error('Requiring module "' + id + '", which threw an exception.');
} }
if (__DEV__) { if (__DEV__) {
require.Systrace = { beginEvent: () => {}, endEvent: () => {} }; require.Systrace = { beginEvent: () => {}, endEvent: () => {} };
}
global.__d = define; // HOT MODULE RELOADING
global.require = require; var createHotReloadingObject = function() {
const hot = {
acceptCallback: null,
accept: callback => { hot.acceptCallback = callback; },
};
return hot;
};
if (__DEV__) { // HMR const acceptAll = function(dependentModules, inverseDependencies) {
function accept(id, factory, inverseDependencies) { if (!dependentModules || dependentModules.length === 0) {
var mod = modules[id]; return true;
}
const notAccepted = dependentModules.filter(
module => !accept(module, /*factory*/ undefined, inverseDependencies));
const parents = [];
for (let i = 0; i < notAccepted.length; i++) {
// if the module has no parents then the change cannot be hot loaded
if (inverseDependencies[notAccepted[i]].length === 0) {
return false;
}
parents.pushAll(inverseDependencies[notAccepted[i]]);
}
return acceptAll(parents, inverseDependencies);
};
const accept = function(id, factory, inverseDependencies) {
const mod = modules[id];
if (!mod) { if (!mod) {
define(id, factory); define(id, factory);
return true; // new modules don't need to be accepted return true; // new modules don't need to be accepted
} }
if (!mod.module.hot) { const {hot} = mod;
if (!hot) {
console.warn( console.warn(
'Cannot accept module because Hot Module Replacement ' + 'Cannot accept module because Hot Module Replacement ' +
'API was not installed.' 'API was not installed.'
@ -134,8 +182,8 @@ if (__DEV__) { // HMR
mod.isInitialized = false; mod.isInitialized = false;
require(id); require(id);
if (mod.module.hot.acceptCallback) { if (hot.acceptCallback) {
mod.module.hot.acceptCallback(); hot.acceptCallback();
return true; return true;
} else { } else {
// need to have inverseDependencies to bubble up accept // need to have inverseDependencies to bubble up accept
@ -146,29 +194,7 @@ if (__DEV__) { // HMR
// accept parent modules recursively up until all siblings are accepted // accept parent modules recursively up until all siblings are accepted
return acceptAll(inverseDependencies[id], inverseDependencies); return acceptAll(inverseDependencies[id], inverseDependencies);
} }
} };
function acceptAll(modules, inverseDependencies) {
if (!modules || modules.length === 0) {
return true;
}
var notAccepted = modules.filter(function(module) {
return !accept(module, /*factory*/ undefined, inverseDependencies);
});
var parents = [];
for (var i = 0; i < notAccepted.length; i++) {
// if this the module has no parents then the change cannot be hot loaded
if (inverseDependencies[notAccepted[i]].length === 0) {
return false;
}
parents.pushAll(inverseDependencies[notAccepted[i]]);
}
return acceptAll(parents, inverseDependencies);
}
global.__accept = accept; global.__accept = accept;
} }