MCPcopy
hub / github.com/webpack/webpack / testPlugin

Function testPlugin

test/configCases/rebuild/finishModules/webpack.config.js:13–62  ·  view source on GitHub ↗
(compiler)

Source from the content-addressed store, hash-verified

11 * @param {import("../../../../").Compiler} compiler the compiler
12 */
13const testPlugin = (compiler) => {
14 compiler.hooks.compilation.tap("TestPlugin", (compilation) => {
15 let shouldReplace = false;
16 NormalModule.getCompilationHooks(compilation).loader.tap(
17 "TestPlugin",
18 (loaderContext) => {
19 /** @type {EXPECTED_ANY} */
20 (loaderContext).shouldReplace = shouldReplace;
21 }
22 );
23 compilation.hooks.finishModules.tapAsync(
24 "TestPlugin",
25 (modules, callback) => {
26 const src = resolve(join(__dirname, "other-file.js"));
27
28 /**
29 * @param {Module} m test
30 * @returns {boolean | string} test
31 */
32 function matcher(m) {
33 return (
34 /** @type {NormalModule} */ (m).resource &&
35 /** @type {NormalModule} */ (m).resource === src
36 );
37 }
38
39 const module = [...modules].find(matcher);
40
41 if (!module) {
42 throw new Error("something went wrong");
43 }
44
45 // Check if already build the updated version
46 // this will happen when using caching
47 if (
48 /** @type {NonNullable<Module["buildInfo"]>} */
49 (module.buildInfo)._isReplaced
50 ) {
51 return callback();
52 }
53
54 shouldReplace = true;
55 compilation.rebuildModule(module, (err) => {
56 shouldReplace = false;
57 callback(err);
58 });
59 }
60 );
61 });
62};
63
64/** @type {import("../../../../").Configuration} */
65module.exports = {

Callers

nothing calls this directly

Calls 6

resolveFunction · 0.85
joinFunction · 0.85
tapMethod · 0.80
rebuildModuleMethod · 0.80
callbackFunction · 0.50
getCompilationHooksMethod · 0.45

Tested by

no test coverage detected