mirror of https://github.com/status-im/metro.git
add new hooks to print require paths
Reviewed By: jingc Differential Revision: D7545569 fbshipit-source-id: 57fa6fef45805c74682dd9897570a89c82ce0b23
This commit is contained in:
parent
6ab9e7d09c
commit
11cc6a7f65
|
@ -20,6 +20,11 @@ export type Options = {
|
||||||
+dev: boolean,
|
+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) {
|
function wrapModule(module: DependencyEdge, options: Options) {
|
||||||
if (module.output.type === 'script') {
|
if (module.output.type === 'script') {
|
||||||
return module.output.code;
|
return module.output.code;
|
||||||
|
@ -35,8 +40,11 @@ function wrapModule(module: DependencyEdge, options: Options) {
|
||||||
// requires by name when debugging).
|
// requires by name when debugging).
|
||||||
// TODO (t26853986): Switch this to use the relative file path (once we have
|
// TODO (t26853986): Switch this to use the relative file path (once we have
|
||||||
// as single project root).
|
// as single project root).
|
||||||
if (options.dev) {
|
if (PASS_MODULE_PATHS_TO_DEFINE || options.dev) {
|
||||||
params.push(path.basename(module.path));
|
params.push(path.basename(module.path));
|
||||||
|
if (PASS_MODULE_PATHS_TO_DEFINE) {
|
||||||
|
params.push(module.path);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return addParamsToDefineCall(module.output.code, ...params);
|
return addParamsToDefineCall(module.output.code, ...params);
|
||||||
|
|
|
@ -5,6 +5,7 @@ exports[`basic_bundle bundles package with polyfills 1`] = `
|
||||||
(function (global) {
|
(function (global) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
var PRINT_REQUIRE_PATHS = false;
|
||||||
global.require = metroRequire;
|
global.require = metroRequire;
|
||||||
global.__d = define;
|
global.__d = define;
|
||||||
var modules = Object.create(null);
|
var modules = Object.create(null);
|
||||||
|
@ -21,6 +22,16 @@ exports[`basic_bundle bundles package with polyfills 1`] = `
|
||||||
hasError: false,
|
hasError: false,
|
||||||
isInitialized: 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) {
|
function metroRequire(moduleId) {
|
||||||
|
@ -96,6 +107,10 @@ exports[`basic_bundle bundles package with polyfills 1`] = `
|
||||||
dependencyMap = _module.dependencyMap;
|
dependencyMap = _module.dependencyMap;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
if (PRINT_REQUIRE_PATHS) {
|
||||||
|
console.log(\\"require file path \\" + (module.path || 'unknown'));
|
||||||
|
}
|
||||||
|
|
||||||
var _moduleObject = {
|
var _moduleObject = {
|
||||||
exports: exports
|
exports: exports
|
||||||
};
|
};
|
||||||
|
@ -193,6 +208,7 @@ exports[`basic_bundle bundles package without polyfills 1`] = `
|
||||||
(function (global) {
|
(function (global) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
var PRINT_REQUIRE_PATHS = false;
|
||||||
global.require = metroRequire;
|
global.require = metroRequire;
|
||||||
global.__d = define;
|
global.__d = define;
|
||||||
var modules = Object.create(null);
|
var modules = Object.create(null);
|
||||||
|
@ -209,6 +225,16 @@ exports[`basic_bundle bundles package without polyfills 1`] = `
|
||||||
hasError: false,
|
hasError: false,
|
||||||
isInitialized: 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) {
|
function metroRequire(moduleId) {
|
||||||
|
@ -284,6 +310,10 @@ exports[`basic_bundle bundles package without polyfills 1`] = `
|
||||||
dependencyMap = _module.dependencyMap;
|
dependencyMap = _module.dependencyMap;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
if (PRINT_REQUIRE_PATHS) {
|
||||||
|
console.log(\\"require file path \\" + (module.path || 'unknown'));
|
||||||
|
}
|
||||||
|
|
||||||
var _moduleObject = {
|
var _moduleObject = {
|
||||||
exports: exports
|
exports: exports
|
||||||
};
|
};
|
||||||
|
|
|
@ -45,12 +45,18 @@ type ModuleDefinition = {|
|
||||||
hot?: HotModuleReloadingData,
|
hot?: HotModuleReloadingData,
|
||||||
isInitialized: boolean,
|
isInitialized: boolean,
|
||||||
verboseName?: string,
|
verboseName?: string,
|
||||||
|
path?: string,
|
||||||
|};
|
|};
|
||||||
type ModuleMap = {[key: ModuleID]: ModuleDefinition, __proto__: null};
|
type ModuleMap = {[key: ModuleID]: ModuleDefinition, __proto__: null};
|
||||||
type PatchedModules = {[ModuleID]: boolean};
|
type PatchedModules = {[ModuleID]: boolean};
|
||||||
type RequireFn = (id: ModuleID | VerboseModuleNameForDev) => Exports;
|
type RequireFn = (id: ModuleID | VerboseModuleNameForDev) => Exports;
|
||||||
type VerboseModuleNameForDev = string;
|
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.require = metroRequire;
|
||||||
global.__d = define;
|
global.__d = define;
|
||||||
|
|
||||||
|
@ -95,6 +101,17 @@ function define(
|
||||||
hasError: false,
|
hasError: false,
|
||||||
isInitialized: 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__) {
|
if (__DEV__) {
|
||||||
// HMR
|
// HMR
|
||||||
modules[moduleId].hot = createHotReloadingObject();
|
modules[moduleId].hot = createHotReloadingObject();
|
||||||
|
@ -198,6 +215,9 @@ function loadModuleImplementation(moduleId, module) {
|
||||||
const exports = (module.exports = {});
|
const exports = (module.exports = {});
|
||||||
const {factory, dependencyMap} = module;
|
const {factory, dependencyMap} = module;
|
||||||
try {
|
try {
|
||||||
|
if (PRINT_REQUIRE_PATHS) {
|
||||||
|
console.log(`require file path ${module.path || 'unknown'}`); // eslint-disable-line no-console
|
||||||
|
}
|
||||||
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));
|
||||||
|
|
Loading…
Reference in New Issue