Skip to content

Commit f2e52a1

Browse files
committed
Patch facebook/metro#1130 to profile module init
1 parent f9944b5 commit f2e52a1

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

patches/metro+0.76.8.patch

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
diff --git a/node_modules/metro/src/ModuleGraph/worker/JsFileWrapping.js b/node_modules/metro/src/ModuleGraph/worker/JsFileWrapping.js
2+
index 48a1409..ef185c9 100644
3+
--- a/node_modules/metro/src/ModuleGraph/worker/JsFileWrapping.js
4+
+++ b/node_modules/metro/src/ModuleGraph/worker/JsFileWrapping.js
5+
@@ -70,14 +70,19 @@ function wrapModule(
6+
importDefaultName,
7+
importAllName,
8+
dependencyMapName,
9+
- globalPrefix
10+
+ globalPrefix,
11+
+ moduleFactoryName
12+
) {
13+
const params = buildParameters(
14+
importDefaultName,
15+
importAllName,
16+
dependencyMapName
17+
);
18+
- const factory = functionFromProgram(fileAst.program, params);
19+
+ const factory = functionFromProgram(
20+
+ fileAst.program,
21+
+ params,
22+
+ moduleFactoryName
23+
+ );
24+
const def = t.callExpression(t.identifier(`${globalPrefix}__d`), [factory]);
25+
const ast = t.file(t.program([t.expressionStatement(def)]));
26+
const requireName = renameRequires(ast);
27+
@@ -107,7 +112,16 @@ function wrapJson(source, globalPrefix) {
28+
"});",
29+
].join("\n");
30+
}
31+
-function functionFromProgram(program, parameters) {
32+
+const JS_INVALID_IDENT_RE = /[^a-zA-Z0-9$_]/g;
33+
+function functionFromProgram(program, parameters, moduleFactoryName) {
34+
+ let identifier;
35+
+ if (typeof moduleFactoryName === "string" && moduleFactoryName !== "") {
36+
+ // Keep the name readable so it shows up in profiler traces.
37+
+ // Add an unlikely suffix to avoid collisions with the module code.
38+
+ identifier = t.identifier(
39+
+ `${moduleFactoryName.replace(JS_INVALID_IDENT_RE, "_")}__module_factory__`
40+
+ );
41+
+ }
42+
return t.functionExpression(
43+
undefined,
44+
parameters.map(makeIdentifier),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
diff --git a/node_modules/metro-transform-worker/src/index.js b/node_modules/metro-transform-worker/src/index.js
2+
index 27d4cb3..fd71f47 100644
3+
--- a/node_modules/metro-transform-worker/src/index.js
4+
+++ b/node_modules/metro-transform-worker/src/index.js
5+
@@ -190,6 +190,10 @@ async function transformJS(file, { config, options, projectRoot }) {
6+
let dependencyMapName = "";
7+
let dependencies;
8+
let wrappedAst;
9+
+ const minify =
10+
+ options.minify &&
11+
+ options.unstable_transformProfile !== "hermes-canary" &&
12+
+ options.unstable_transformProfile !== "hermes-stable";
13+
14+
// If the module to transform is a script (meaning that is not part of the
15+
// dependency graph and it code will just be prepended to the bundle modules),
16+
@@ -229,19 +233,20 @@ async function transformJS(file, { config, options, projectRoot }) {
17+
if (config.unstable_disableModuleWrapping === true) {
18+
wrappedAst = ast;
19+
} else {
20+
+ let moduleFactoryName;
21+
+ if (options.dev && !minify) {
22+
+ moduleFactoryName = file.filename;
23+
+ }
24+
({ ast: wrappedAst } = JsFileWrapping.wrapModule(
25+
ast,
26+
importDefault,
27+
importAll,
28+
dependencyMapName,
29+
- config.globalPrefix
30+
+ config.globalPrefix,
31+
+ moduleFactoryName
32+
));
33+
}
34+
}
35+
- const minify =
36+
- options.minify &&
37+
- options.unstable_transformProfile !== "hermes-canary" &&
38+
- options.unstable_transformProfile !== "hermes-stable";
39+
const reserved = [];
40+
if (config.unstable_dependencyMapReservedName != null) {
41+
reserved.push(config.unstable_dependencyMapReservedName);

0 commit comments

Comments
 (0)