add new hooks to print require paths

Reviewed By: jingc

Differential Revision: D7545569

fbshipit-source-id: 57fa6fef45805c74682dd9897570a89c82ce0b23
This commit is contained in:
Spencer Ahrens 2018-04-10 12:35:07 -07:00 committed by Facebook Github Bot
parent 6ab9e7d09c
commit 11cc6a7f65
3 changed files with 59 additions and 1 deletions

View File

@ -20,6 +20,11 @@ export type Options = {
+dev: boolean,
};
// Used to include paths in production bundles for traces of performance tuned runs,
// e.g. to update fbandroid/apps/fb4a/compiled_react_native_modules.txt
// Make sure to set PRINT_REQUIRE_PATHS = true too, and restart Metro
const PASS_MODULE_PATHS_TO_DEFINE = false;
function wrapModule(module: DependencyEdge, options: Options) {
if (module.output.type === 'script') {
return module.output.code;
@ -35,8 +40,11 @@ function wrapModule(module: DependencyEdge, options: Options) {
// requires by name when debugging).
// TODO (t26853986): Switch this to use the relative file path (once we have
// as single project root).
if (options.dev) {
if (PASS_MODULE_PATHS_TO_DEFINE || options.dev) {
params.push(path.basename(module.path));
if (PASS_MODULE_PATHS_TO_DEFINE) {
params.push(module.path);
}
}
return addParamsToDefineCall(module.output.code, ...params);

View File

@ -5,6 +5,7 @@ exports[`basic_bundle bundles package with polyfills 1`] = `
(function (global) {
'use strict';
var PRINT_REQUIRE_PATHS = false;
global.require = metroRequire;
global.__d = define;
var modules = Object.create(null);
@ -21,6 +22,16 @@ exports[`basic_bundle bundles package with polyfills 1`] = `
hasError: false,
isInitialized: false
};
if (PRINT_REQUIRE_PATHS) {
var _path = arguments[4];
if (_path) {
modules[moduleId].path = _path;
} else {
throw new Error(\\"path not set on module with PRINT_REQUIRE_PATHS true. Make sure PASS_MODULE_PATHS_TO_DEFINE is true and restart Metro or rebuild bundle\\");
}
}
}
function metroRequire(moduleId) {
@ -96,6 +107,10 @@ exports[`basic_bundle bundles package with polyfills 1`] = `
dependencyMap = _module.dependencyMap;
try {
if (PRINT_REQUIRE_PATHS) {
console.log(\\"require file path \\" + (module.path || 'unknown'));
}
var _moduleObject = {
exports: exports
};
@ -193,6 +208,7 @@ exports[`basic_bundle bundles package without polyfills 1`] = `
(function (global) {
'use strict';
var PRINT_REQUIRE_PATHS = false;
global.require = metroRequire;
global.__d = define;
var modules = Object.create(null);
@ -209,6 +225,16 @@ exports[`basic_bundle bundles package without polyfills 1`] = `
hasError: false,
isInitialized: false
};
if (PRINT_REQUIRE_PATHS) {
var _path = arguments[4];
if (_path) {
modules[moduleId].path = _path;
} else {
throw new Error(\\"path not set on module with PRINT_REQUIRE_PATHS true. Make sure PASS_MODULE_PATHS_TO_DEFINE is true and restart Metro or rebuild bundle\\");
}
}
}
function metroRequire(moduleId) {
@ -284,6 +310,10 @@ exports[`basic_bundle bundles package without polyfills 1`] = `
dependencyMap = _module.dependencyMap;
try {
if (PRINT_REQUIRE_PATHS) {
console.log(\\"require file path \\" + (module.path || 'unknown'));
}
var _moduleObject = {
exports: exports
};

View File

@ -45,12 +45,18 @@ type ModuleDefinition = {|
hot?: HotModuleReloadingData,
isInitialized: boolean,
verboseName?: string,
path?: string,
|};
type ModuleMap = {[key: ModuleID]: ModuleDefinition, __proto__: null};
type PatchedModules = {[ModuleID]: boolean};
type RequireFn = (id: ModuleID | VerboseModuleNameForDev) => Exports;
type VerboseModuleNameForDev = string;
// Used to include paths in production bundles for traces of performance tuned runs
// e.g. to update fbandroid/apps/fb4a/compiled_react_native_modules.txt
// Make sure to set PASS_MODULE_PATHS_TO_DEFINE = true too, and restart Metro.
const PRINT_REQUIRE_PATHS = false;
global.require = metroRequire;
global.__d = define;
@ -95,6 +101,17 @@ function define(
hasError: false,
isInitialized: false,
};
if (PRINT_REQUIRE_PATHS) {
const path: string | void = arguments[4];
if (path) {
modules[moduleId].path = path;
} else {
throw new Error(
'path not set on module with PRINT_REQUIRE_PATHS true. Make sure ' +
'PASS_MODULE_PATHS_TO_DEFINE is true and restart Metro or rebuild bundle',
);
}
}
if (__DEV__) {
// HMR
modules[moduleId].hot = createHotReloadingObject();
@ -198,6 +215,9 @@ function loadModuleImplementation(moduleId, module) {
const exports = (module.exports = {});
const {factory, dependencyMap} = module;
try {
if (PRINT_REQUIRE_PATHS) {
console.log(`require file path ${module.path || 'unknown'}`); // eslint-disable-line no-console
}
if (__DEV__) {
// $FlowFixMe: we know that __DEV__ is const and `Systrace` exists
Systrace.beginEvent('JS_require_' + (module.verboseName || moduleId));