Skip to content

Commit 6cabcf3

Browse files
fix(debug): profile the taps of hook maps in ProfilingPlugin
1 parent d201a1c commit 6cabcf3

3 files changed

Lines changed: 75 additions & 6 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Profile hook map taps, such as resolver and parser hooks, in `ProfilingPlugin`.

‎lib/debug/ProfilingPlugin.js‎

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ class ProfilingPlugin {
303303
const hook =
304304
compiler.hooks[/** @type {keyof Compiler["hooks"]} */ (hookName)];
305305
if (hook) {
306-
hook.intercept(makeInterceptorFor("Compiler", tracer)(hookName));
306+
interceptTaps(hook, makeInterceptorFor("Compiler", tracer)(hookName));
307307
}
308308
}
309309

@@ -314,10 +314,7 @@ class ProfilingPlugin {
314314
(hookName)
315315
];
316316
if (hook) {
317-
hook.intercept(
318-
/** @type {EXPECTED_ANY} */
319-
(makeInterceptorFor("Resolver", tracer)(hookName))
320-
);
317+
interceptTaps(hook, makeInterceptorFor("Resolver", tracer)(hookName));
321318
}
322319
}
323320

@@ -411,6 +408,30 @@ class ProfilingPlugin {
411408

412409
/** @typedef {Record<string, Hook<EXPECTED_ANY, EXPECTED_ANY> | FakeHook<EXPECTED_ANY> | HookMap<EXPECTED_ANY>>} Hooks */
413410

411+
/**
412+
* Intercepts the taps of a hook or, for a hook map, of every hook it creates later.
413+
* @param {Hooks[string]} hook the hook or hook map
414+
* @param {HookInterceptor<EXPECTED_ANY, EXPECTED_ANY>} interceptor what intercepts each tap
415+
* @returns {void}
416+
*/
417+
const interceptTaps = (hook, interceptor) => {
418+
if ("for" in hook) {
419+
hook.intercept({
420+
/**
421+
* @param {string} key the key the hook is created for
422+
* @param {Hook<EXPECTED_ANY, EXPECTED_ANY>} created the hook created
423+
* @returns {Hook<EXPECTED_ANY, EXPECTED_ANY>} the same hook, intercepted
424+
*/
425+
factory: (key, created) => {
426+
created.intercept(interceptor);
427+
return created;
428+
}
429+
});
430+
} else {
431+
hook.intercept(interceptor);
432+
}
433+
};
434+
414435
/**
415436
* Intercept all hooks for.
416437
* @param {EXPECTED_OBJECT & { hooks?: Hooks }} instance instance
@@ -423,7 +444,7 @@ const interceptAllHooksFor = (instance, tracer, logLabel) => {
423444
for (const hookName of Object.keys(hooks)) {
424445
const hook = hooks[hookName];
425446
if (hook && !hook._fakeHook) {
426-
hook.intercept(makeInterceptorFor(logLabel, tracer)(hookName));
447+
interceptTaps(hook, makeInterceptorFor(logLabel, tracer)(hookName));
427448
}
428449
}
429450
}

‎test/unitCases/ProfilingPluginCompilation.unittest.js‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,47 @@ describe("Profiling Plugin", () => {
8989
});
9090
});
9191
});
92+
93+
itSkipBun("should profile the taps of hook maps", (done) => {
94+
const webpack = require("../../");
95+
96+
const finalPath = path.join(testDirectory, "js/profilingHookMaps.json");
97+
const compiler = webpack({
98+
context: testDirectory,
99+
entry: "./fixtures/a.js",
100+
output: {
101+
path: path.join(testDirectory, "js/profilingHookMapsOut")
102+
},
103+
plugins: [
104+
new webpack.debug.ProfilingPlugin({ outputPath: finalPath }),
105+
{
106+
apply(compiler) {
107+
compiler.resolverFactory.hooks.resolveOptions
108+
.for("normal")
109+
.tap("ResolverHookMapProbePlugin", (options) => options);
110+
compiler.hooks.compilation.tap(
111+
"ParserHookMapProbePlugin",
112+
(compilation, { normalModuleFactory }) => {
113+
normalModuleFactory.hooks.parser
114+
.for("javascript/auto")
115+
.tap("ParserHookMapProbePlugin", () => {});
116+
}
117+
);
118+
}
119+
}
120+
]
121+
});
122+
compiler.run((err) => {
123+
if (err) return done(err);
124+
compiler.close((closeErr) => {
125+
if (closeErr) return done(closeErr);
126+
/** @type {{ name: string }[]} */
127+
const data = JSON.parse(fs.readFileSync(finalPath, "utf8"));
128+
const names = new Set(data.map((entry) => entry.name));
129+
expect(names.has("ResolverHookMapProbePlugin")).toBe(true);
130+
expect(names.has("ParserHookMapProbePlugin")).toBe(true);
131+
done();
132+
});
133+
});
134+
});
92135
});

0 commit comments

Comments
 (0)