MCPcopy
hub / github.com/webpack/webpack / apply

Method apply

lib/NormalModuleReplacementPlugin.js:36–74  ·  view source on GitHub ↗

* Applies the plugin by registering its hooks on the compiler. * @param {Compiler} compiler the compiler instance * @returns {void}

(compiler)

Source from the content-addressed store, hash-verified

34 * @returns {void}
35 */
36 apply(compiler) {
37 const resourceRegExp = this.resourceRegExp;
38 const newResource = this.newResource;
39 compiler.hooks.normalModuleFactory.tap(PLUGIN_NAME, (nmf) => {
40 nmf.hooks.beforeResolve.tap(PLUGIN_NAME, (result) => {
41 if (resourceRegExp.test(result.request)) {
42 if (typeof newResource === "function") {
43 newResource(result);
44 } else {
45 result.request = newResource;
46 }
47 }
48 });
49 nmf.hooks.afterResolve.tap(PLUGIN_NAME, (result) => {
50 const createData = result.createData;
51 if (resourceRegExp.test(/** @type {string} */ (createData.resource))) {
52 if (typeof newResource === "function") {
53 newResource(result);
54 } else {
55 const fs =
56 /** @type {InputFileSystem} */
57 (compiler.inputFileSystem);
58 if (
59 newResource.startsWith("/") ||
60 (newResource.length > 1 && newResource[1] === ":")
61 ) {
62 createData.resource = newResource;
63 } else {
64 createData.resource = join(
65 fs,
66 dirname(fs, /** @type {string} */ (createData.resource)),
67 newResource
68 );
69 }
70 }
71 }
72 });
73 });
74 }
75}
76
77module.exports = NormalModuleReplacementPlugin;

Callers

nothing calls this directly

Calls 4

joinFunction · 0.85
dirnameFunction · 0.85
tapMethod · 0.80
testMethod · 0.45

Tested by

no test coverage detected