mirror of https://github.com/status-im/metro.git
Fix indentation of polyfills/require.js
Summary: Fixes the messed-up indentation of `polyfills/require.js`. Over half of the lines were indented with an odd number of spaces. Reviewed By: arcanis, bestander Differential Revision: D4737435 fbshipit-source-id: a5b9baf0a27f236a4d3d6b6c1c5a92f52859f62c
This commit is contained in:
parent
bbd4669180
commit
80faeb9034
|
@ -10,30 +10,30 @@
|
||||||
* @flow
|
* @flow
|
||||||
*/
|
*/
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
declare var __DEV__: boolean;
|
declare var __DEV__: boolean;
|
||||||
|
|
||||||
type DependencyMap = Array<ModuleID>;
|
type DependencyMap = Array<ModuleID>;
|
||||||
type Exports = any;
|
type Exports = any;
|
||||||
type FactoryFn = (
|
type FactoryFn = (
|
||||||
global: Object,
|
global: Object,
|
||||||
require: RequireFn,
|
require: RequireFn,
|
||||||
moduleObject: {exports: {}},
|
moduleObject: {exports: {}},
|
||||||
exports: {},
|
exports: {},
|
||||||
dependencyMap: ?DependencyMap,
|
dependencyMap: ?DependencyMap,
|
||||||
) => void;
|
) => void;
|
||||||
type HotModuleReloadingAcceptFn = Function;
|
type HotModuleReloadingAcceptFn = Function;
|
||||||
type HotModuleReloadingData = {|
|
type HotModuleReloadingData = {|
|
||||||
acceptCallback: ?HotModuleReloadingAcceptFn,
|
acceptCallback: ?HotModuleReloadingAcceptFn,
|
||||||
accept: (callback: HotModuleReloadingAcceptFn) => void,
|
accept: (callback: HotModuleReloadingAcceptFn) => void,
|
||||||
|};
|
|};
|
||||||
type Module = {
|
type Module = {
|
||||||
exports: Exports,
|
exports: Exports,
|
||||||
hot?: HotModuleReloadingData,
|
hot?: HotModuleReloadingData,
|
||||||
};
|
};
|
||||||
type ModuleID = number;
|
type ModuleID = number;
|
||||||
type ModuleDefinition = {|
|
type ModuleDefinition = {|
|
||||||
dependencyMap: ?DependencyMap,
|
dependencyMap: ?DependencyMap,
|
||||||
exports: Exports,
|
exports: Exports,
|
||||||
factory: FactoryFn,
|
factory: FactoryFn,
|
||||||
|
@ -42,247 +42,247 @@
|
||||||
isInitialized: boolean,
|
isInitialized: boolean,
|
||||||
verboseName?: string,
|
verboseName?: string,
|
||||||
|};
|
|};
|
||||||
type ModuleMap =
|
type ModuleMap =
|
||||||
{[key: ModuleID]: (ModuleDefinition)};
|
{[key: ModuleID]: (ModuleDefinition)};
|
||||||
type RequireFn = (id: ModuleID | VerboseModuleNameForDev) => Exports;
|
type RequireFn = (id: ModuleID | VerboseModuleNameForDev) => Exports;
|
||||||
type VerboseModuleNameForDev = string;
|
type VerboseModuleNameForDev = string;
|
||||||
|
|
||||||
global.require = require;
|
global.require = require;
|
||||||
global.__d = define;
|
global.__d = define;
|
||||||
|
|
||||||
const modules: ModuleMap = Object.create(null);
|
const modules: ModuleMap = Object.create(null);
|
||||||
if (__DEV__) {
|
if (__DEV__) {
|
||||||
var verboseNamesToModuleIds: {[key: string]: number} = Object.create(null);
|
var verboseNamesToModuleIds: {[key: string]: number} = Object.create(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
function define(
|
function define(
|
||||||
factory: FactoryFn,
|
factory: FactoryFn,
|
||||||
moduleId: number,
|
moduleId: number,
|
||||||
dependencyMap?: DependencyMap,
|
dependencyMap?: DependencyMap,
|
||||||
) {
|
) {
|
||||||
if (moduleId in modules) {
|
if (moduleId in modules) {
|
||||||
// prevent repeated calls to `global.nativeRequire` to overwrite modules
|
// prevent repeated calls to `global.nativeRequire` to overwrite modules
|
||||||
// that are already loaded
|
// that are already loaded
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
modules[moduleId] = {
|
modules[moduleId] = {
|
||||||
dependencyMap,
|
dependencyMap,
|
||||||
exports: undefined,
|
exports: undefined,
|
||||||
factory,
|
factory,
|
||||||
hasError: false,
|
hasError: false,
|
||||||
isInitialized: false,
|
isInitialized: false,
|
||||||
};
|
};
|
||||||
if (__DEV__) {
|
if (__DEV__) {
|
||||||
// HMR
|
// HMR
|
||||||
modules[moduleId].hot = createHotReloadingObject();
|
modules[moduleId].hot = createHotReloadingObject();
|
||||||
|
|
||||||
// DEBUGGABLE MODULES NAMES
|
// DEBUGGABLE MODULES NAMES
|
||||||
// we take `verboseName` from `arguments` to avoid an unused named parameter
|
// we take `verboseName` from `arguments` to avoid an unused named parameter
|
||||||
// in `define` in production.
|
// in `define` in production.
|
||||||
const verboseName: string | void = arguments[3];
|
const verboseName: string | void = arguments[3];
|
||||||
if (verboseName) {
|
if (verboseName) {
|
||||||
modules[moduleId].verboseName = verboseName;
|
modules[moduleId].verboseName = verboseName;
|
||||||
verboseNamesToModuleIds[verboseName] = moduleId;
|
verboseNamesToModuleIds[verboseName] = moduleId;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function require(moduleId: ModuleID | VerboseModuleNameForDev) {
|
function require(moduleId: ModuleID | VerboseModuleNameForDev) {
|
||||||
if (__DEV__ && typeof moduleId === 'string') {
|
if (__DEV__ && typeof moduleId === 'string') {
|
||||||
const verboseName = moduleId;
|
const verboseName = moduleId;
|
||||||
moduleId = verboseNamesToModuleIds[moduleId];
|
moduleId = verboseNamesToModuleIds[moduleId];
|
||||||
if (moduleId == null) {
|
if (moduleId == null) {
|
||||||
throw new Error(`Unknown named module: '${verboseName}'`);
|
throw new Error(`Unknown named module: '${verboseName}'`);
|
||||||
} else {
|
} else {
|
||||||
console.warn(
|
console.warn(
|
||||||
`Requiring module '${verboseName}' by name is only supported for ` +
|
`Requiring module '${verboseName}' by name is only supported for ` +
|
||||||
'debugging purposes and will BREAK IN PRODUCTION!'
|
'debugging purposes and will BREAK IN PRODUCTION!'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//$FlowFixMe: at this point we know that moduleId is a number
|
//$FlowFixMe: at this point we know that moduleId is a number
|
||||||
const moduleIdReallyIsNumber: number = moduleId;
|
const moduleIdReallyIsNumber: number = moduleId;
|
||||||
const module = modules[moduleIdReallyIsNumber];
|
const module = modules[moduleIdReallyIsNumber];
|
||||||
return module && module.isInitialized
|
return module && module.isInitialized
|
||||||
? module.exports
|
? module.exports
|
||||||
: guardedLoadModule(moduleIdReallyIsNumber, module);
|
: guardedLoadModule(moduleIdReallyIsNumber, module);
|
||||||
}
|
}
|
||||||
|
|
||||||
let inGuard = false;
|
let inGuard = false;
|
||||||
function guardedLoadModule(moduleId: ModuleID, module) {
|
function guardedLoadModule(moduleId: ModuleID, module) {
|
||||||
if (!inGuard && global.ErrorUtils) {
|
if (!inGuard && global.ErrorUtils) {
|
||||||
inGuard = true;
|
inGuard = true;
|
||||||
let returnValue;
|
let returnValue;
|
||||||
try {
|
try {
|
||||||
returnValue = loadModuleImplementation(moduleId, module);
|
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 {
|
} else {
|
||||||
return loadModuleImplementation(moduleId, module);
|
return loadModuleImplementation(moduleId, module);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadModuleImplementation(moduleId, module) {
|
function loadModuleImplementation(moduleId, module) {
|
||||||
const nativeRequire = global.nativeRequire;
|
const nativeRequire = global.nativeRequire;
|
||||||
if (!module && nativeRequire) {
|
if (!module && nativeRequire) {
|
||||||
nativeRequire(moduleId);
|
nativeRequire(moduleId);
|
||||||
module = modules[moduleId];
|
module = modules[moduleId];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!module) {
|
if (!module) {
|
||||||
throw unknownModuleError(moduleId);
|
throw unknownModuleError(moduleId);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (module.hasError) {
|
if (module.hasError) {
|
||||||
throw moduleThrewError(moduleId);
|
throw moduleThrewError(moduleId);
|
||||||
}
|
}
|
||||||
|
|
||||||
// `require` calls int the require polyfill itself are not analyzed and
|
// `require` calls int the require polyfill itself are not analyzed and
|
||||||
// replaced so that they use numeric module IDs.
|
// replaced so that they use numeric module IDs.
|
||||||
// The systrace module will expose itself on the require function so that
|
// The systrace module will expose itself on the require function so that
|
||||||
// it can be used here.
|
// it can be used here.
|
||||||
// TODO(davidaurelio) Scan polyfills for dependencies, too (t9759686)
|
// TODO(davidaurelio) Scan polyfills for dependencies, too (t9759686)
|
||||||
if (__DEV__) {
|
if (__DEV__) {
|
||||||
var {Systrace} = require;
|
var {Systrace} = require;
|
||||||
}
|
}
|
||||||
|
|
||||||
// We must optimistically mark module as initialized before running the
|
// We must optimistically mark module as initialized before running the
|
||||||
// factory to keep any require cycles inside the factory from causing an
|
// factory to keep any require cycles inside the factory from causing an
|
||||||
// infinite require loop.
|
// infinite require loop.
|
||||||
module.isInitialized = true;
|
module.isInitialized = true;
|
||||||
const exports = module.exports = {};
|
const exports = module.exports = {};
|
||||||
const {factory, dependencyMap} = module;
|
const {factory, dependencyMap} = module;
|
||||||
try {
|
try {
|
||||||
if (__DEV__) {
|
if (__DEV__) {
|
||||||
// $FlowFixMe: we know that __DEV__ is const and `Systrace` exists
|
// $FlowFixMe: we know that __DEV__ is const and `Systrace` exists
|
||||||
Systrace.beginEvent('JS_require_' + (module.verboseName || moduleId));
|
Systrace.beginEvent('JS_require_' + (module.verboseName || moduleId));
|
||||||
}
|
}
|
||||||
|
|
||||||
const moduleObject: Module = {exports};
|
const moduleObject: Module = {exports};
|
||||||
if (__DEV__ && module.hot) {
|
if (__DEV__ && module.hot) {
|
||||||
moduleObject.hot = module.hot;
|
moduleObject.hot = module.hot;
|
||||||
}
|
}
|
||||||
|
|
||||||
// keep args in sync with with defineModuleCode in
|
// keep args in sync with with defineModuleCode in
|
||||||
// packager/src//Resolver/index.js
|
// packager/src//Resolver/index.js
|
||||||
// and packager/src//ModuleGraph/worker.js
|
// and packager/src//ModuleGraph/worker.js
|
||||||
factory(global, require, moduleObject, exports, dependencyMap);
|
factory(global, require, moduleObject, exports, dependencyMap);
|
||||||
|
|
||||||
// avoid removing factory in DEV mode as it breaks HMR
|
// avoid removing factory in DEV mode as it breaks HMR
|
||||||
if (!__DEV__) {
|
if (!__DEV__) {
|
||||||
// $FlowFixMe: This is only sound because we never access `factory` again
|
// $FlowFixMe: This is only sound because we never access `factory` again
|
||||||
module.factory = undefined;
|
module.factory = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (__DEV__) {
|
if (__DEV__) {
|
||||||
// $FlowFixMe: we know that __DEV__ is const and `Systrace` exists
|
// $FlowFixMe: we know that __DEV__ is const and `Systrace` exists
|
||||||
Systrace.endEvent();
|
Systrace.endEvent();
|
||||||
}
|
}
|
||||||
return (module.exports = moduleObject.exports);
|
return (module.exports = moduleObject.exports);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
module.hasError = true;
|
module.hasError = true;
|
||||||
module.isInitialized = false;
|
module.isInitialized = false;
|
||||||
module.exports = undefined;
|
module.exports = undefined;
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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. ' +
|
||||||
'You may also want to run `npm install`, or `yarn` (depending on your environment).';
|
'You may also want to run `npm install`, or `yarn` (depending on your environment).';
|
||||||
}
|
}
|
||||||
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.');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (__DEV__) {
|
if (__DEV__) {
|
||||||
require.Systrace = {beginEvent: () => {}, endEvent: () => {}};
|
require.Systrace = {beginEvent: () => {}, endEvent: () => {}};
|
||||||
|
|
||||||
// HOT MODULE RELOADING
|
// HOT MODULE RELOADING
|
||||||
var createHotReloadingObject = function() {
|
var createHotReloadingObject = function() {
|
||||||
const hot: HotModuleReloadingData = {
|
const hot: HotModuleReloadingData = {
|
||||||
acceptCallback: null,
|
acceptCallback: null,
|
||||||
accept: callback => { hot.acceptCallback = callback; },
|
accept: callback => { hot.acceptCallback = callback; },
|
||||||
};
|
};
|
||||||
return hot;
|
return hot;
|
||||||
};
|
};
|
||||||
|
|
||||||
const acceptAll = function(
|
const acceptAll = function(
|
||||||
dependentModules,
|
dependentModules,
|
||||||
inverseDependencies,
|
inverseDependencies,
|
||||||
) {
|
) {
|
||||||
if (!dependentModules || dependentModules.length === 0) {
|
if (!dependentModules || dependentModules.length === 0) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const notAccepted = dependentModules.filter(
|
const notAccepted = dependentModules.filter(
|
||||||
module => !accept(module, /*factory*/ undefined, inverseDependencies));
|
module => !accept(module, /*factory*/ undefined, inverseDependencies));
|
||||||
|
|
||||||
const parents = [];
|
const parents = [];
|
||||||
for (let i = 0; i < notAccepted.length; i++) {
|
for (let i = 0; i < notAccepted.length; i++) {
|
||||||
// if the module has no parents then the change cannot be hot loaded
|
// if the module has no parents then the change cannot be hot loaded
|
||||||
if (inverseDependencies[notAccepted[i]].length === 0) {
|
if (inverseDependencies[notAccepted[i]].length === 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
parents.push(...inverseDependencies[notAccepted[i]]);
|
parents.push(...inverseDependencies[notAccepted[i]]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return acceptAll(parents, inverseDependencies);
|
return acceptAll(parents, inverseDependencies);
|
||||||
};
|
};
|
||||||
|
|
||||||
const accept = function(
|
const accept = function(
|
||||||
id: ModuleID,
|
id: ModuleID,
|
||||||
factory?: FactoryFn,
|
factory?: FactoryFn,
|
||||||
inverseDependencies: {[key: ModuleID]: Array<ModuleID>},
|
inverseDependencies: {[key: ModuleID]: Array<ModuleID>},
|
||||||
) {
|
) {
|
||||||
const mod = modules[id];
|
const mod = modules[id];
|
||||||
|
|
||||||
if (!mod && factory) { // new modules need a factory
|
if (!mod && factory) { // new modules need a factory
|
||||||
define(factory, id);
|
define(factory, id);
|
||||||
return true; // new modules don't need to be accepted
|
return true; // new modules don't need to be accepted
|
||||||
}
|
}
|
||||||
|
|
||||||
const {hot} = mod;
|
const {hot} = mod;
|
||||||
if (!hot) {
|
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.'
|
||||||
);
|
);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// replace and initialize factory
|
// replace and initialize factory
|
||||||
if (factory) {
|
if (factory) {
|
||||||
mod.factory = factory;
|
mod.factory = factory;
|
||||||
}
|
}
|
||||||
mod.hasError = false;
|
mod.hasError = false;
|
||||||
mod.isInitialized = false;
|
mod.isInitialized = false;
|
||||||
require(id);
|
require(id);
|
||||||
|
|
||||||
if (hot.acceptCallback) {
|
if (hot.acceptCallback) {
|
||||||
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
|
||||||
if (!inverseDependencies) {
|
if (!inverseDependencies) {
|
||||||
throw new Error('Undefined `inverseDependencies`');
|
throw new Error('Undefined `inverseDependencies`');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
global.__accept = accept;
|
global.__accept = accept;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue