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

Method apply

lib/library/ModuleLibraryPlugin.js:80–191  ·  lib/library/ModuleLibraryPlugin.js::ModuleLibraryPlugin.apply

* 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

78 * @returns {void}
79 */
80 apply(compiler) {
81 super.apply(compiler);
82
83 compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
84 const { onDemandExportsGeneration } =
85 ConcatenatedModule.getCompilationHooks(compilation);
86 const javascriptHooks =
87 JavascriptModulesPlugin.getCompilationHooks(compilation);
88 onDemandExportsGeneration.tap(
89 PLUGIN_NAME,
90 (module, runtimes, source, finalName) => {
91 /** @type {BuildMeta} */
92 const buildMeta = module.buildMeta || (module.buildMeta = {});
93
94 /** @type {BuildMeta[class="st">"exportsSourceByRuntime"]} */
95 const exportsSourceByRuntime =
96 buildMeta.exportsSourceByRuntime ||
97 (buildMeta.exportsSourceByRuntime = new Map());
98
99 /** @type {BuildMeta[class="st">"exportsFinalNameByRuntime"]} */
100 const exportsFinalNameByRuntime =
101 buildMeta.exportsFinalNameByRuntime ||
102 (buildMeta.exportsFinalNameByRuntime = new Map());
103
104 for (const runtime of runtimes) {
105 const key = getRuntimeKey(runtime);
106 exportsSourceByRuntime.set(key, source);
107 exportsFinalNameByRuntime.set(key, finalName);
108 }
109
110 return true;
111 }
112 );
113
114 class="cm">// `ModuleLibraryPlugin` stashes the on-demand exports source via
115 class="cm">// `onDemandExportsGeneration` and only re-emits it when the
116 class="cm">// module is wrapped in an IIFE/factory. When the entry is
117 class="cm">// inlined directly, the stashed source — and the
118 class="cm">// `definePropertyGetters` / `requireScope` runtime helpers it
119 class="cm">// pulled in — never make it into the output. Drop those helpers
120 class="cm">// from the chunk's set so the bundle stays clean.
121 compilation.hooks.additionalChunkRuntimeRequirements.tap(
122 PLUGIN_NAME,
123 (chunk, set, { chunkGraph, codeGenerationResults }) => {
124 if (!set.has(RuntimeGlobals.definePropertyGetters)) return;
125 if (chunkGraph.getNumberOfEntryModules(chunk) !== 1) return;
126 if (chunkGraph.hasChunkEntryDependentChunks(chunk)) return;
127 if (
128 set.has(RuntimeGlobals.moduleFactories) ||
129 set.has(RuntimeGlobals.moduleCache) ||
130 set.has(RuntimeGlobals.interceptModuleExecution) ||
131 set.has(RuntimeGlobals.module) ||
132 set.has(RuntimeGlobals.thisAsExports)
133 ) {
134 return;
135 }
136 if (javascriptHooks.inlineInRuntimeBailout.isUsed()) return;
137

Callers

nothing calls this directly

Calls 15

getRuntimeKeyFunction · 0.85
tapMethod · 0.80
isUsedMethod · 0.80
sourceMethod · 0.80
applyMethod · 0.65
getCompilationHooksMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected