mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Summary: @public Fixes #773, #1055 The resolver was getting a bit unwieldy because a lot has changed since the initial writing (porting node-haste). This also splits up a large complex file into the following: * Makes use of classes: Module, AssetModule, Package, and AssetModule_DEPRECATED (`image!` modules) * DependencyGraph is lazy for everything that isn't haste modules and packages (need to read ahead of time) * Lazy makes it fast, easier to reason about, and easier to add new loaders * Has a centralized filesystem wrapper: fast-fs (ffs) * ffs is async and lazy for any read operation and sync for directory/file lookup which makes it fast * we can easily drop in different adapters for ffs to be able to build up the tree: watchman, git ls-files, etc * use es6 for classes and easier to read promise-based code Follow up diffs will include: * Using new types (Module, AssetModule etc) in the rest of the codebase (currently we convert to plain object which is a bit of a hack) * using watchman to build up the fs * some caching at the object creation level (we are recreating Modules and Packages many times, we can cache them) * A plugin system for loaders (e.g. @tadeuzagallo wants to add a native module loader) Test Plan: * ./runJestTests.sh react-packager * ./runJestTests.sh PackagerIntegration * Export open source and run the e2e test * reset cache * ./fbrnios.sh run and click around
92 lines
2.6 KiB
JavaScript
92 lines
2.6 KiB
JavaScript
/**
|
|
* Copyright (c) 2015-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.
|
|
*
|
|
* The particular require runtime that we are using looks for a global
|
|
* `ErrorUtils` object and if it exists, then it requires modules with the
|
|
* error handler specified via ErrorUtils.setGlobalHandler by calling the
|
|
* require function with applyWithGuard. Since the require module is loaded
|
|
* before any of the modules, this ErrorUtils must be defined (and the handler
|
|
* set) globally before requiring anything.
|
|
*/
|
|
/* eslint global-strict:0 */
|
|
(function(global) {
|
|
var ErrorUtils = {
|
|
_inGuard: 0,
|
|
_globalHandler: null,
|
|
setGlobalHandler: function(fun) {
|
|
ErrorUtils._globalHandler = fun;
|
|
},
|
|
reportError: function(error) {
|
|
ErrorUtils._globalHandler && ErrorUtils._globalHandler(error);
|
|
},
|
|
reportFatalError: function(error) {
|
|
ErrorUtils._globalHandler && ErrorUtils._globalHandler(error, true);
|
|
},
|
|
applyWithGuard: function(fun, context, args) {
|
|
try {
|
|
ErrorUtils._inGuard++;
|
|
return fun.apply(context, args);
|
|
} catch (e) {
|
|
ErrorUtils.reportError(e);
|
|
} finally {
|
|
ErrorUtils._inGuard--;
|
|
}
|
|
},
|
|
applyWithGuardIfNeeded: function(fun, context, args) {
|
|
if (ErrorUtils.inGuard()) {
|
|
return fun.apply(context, args);
|
|
} else {
|
|
ErrorUtils.applyWithGuard(fun, context, args);
|
|
}
|
|
},
|
|
inGuard: function() {
|
|
return ErrorUtils._inGuard;
|
|
},
|
|
guard: function(fun, name, context) {
|
|
if (typeof fun !== 'function') {
|
|
console.warn('A function must be passed to ErrorUtils.guard, got ', fun);
|
|
return null;
|
|
}
|
|
name = name || fun.name || '<generated guard>';
|
|
function guarded() {
|
|
return (
|
|
ErrorUtils.applyWithGuard(
|
|
fun,
|
|
context || this,
|
|
arguments,
|
|
null,
|
|
name
|
|
)
|
|
);
|
|
}
|
|
|
|
return guarded;
|
|
}
|
|
};
|
|
global.ErrorUtils = ErrorUtils;
|
|
|
|
/**
|
|
* This is the error handler that is called when we encounter an exception
|
|
* when loading a module.
|
|
*/
|
|
function setupErrorGuard() {
|
|
var onError = function(e) {
|
|
global.console.error(
|
|
'Error: ' +
|
|
'\n stack: ' + e.stack +
|
|
'\n line: ' + e.line +
|
|
'\n message: ' + e.message,
|
|
e
|
|
);
|
|
};
|
|
global.ErrorUtils.setGlobalHandler(onError);
|
|
}
|
|
|
|
setupErrorGuard();
|
|
})(this);
|