* Applies the plugin by registering its hooks on the compiler. * @param {Compiler} compiler the compiler instance * @returns {void}
(compiler)
| 147 | * @returns {void} |
| 148 | */ |
| 149 | apply(compiler) { |
| 150 | compiler.hooks.compilation.tap( |
| 151 | PLUGIN_NAME, |
| 152 | (compilation, { normalModuleFactory }) => { |
| 153 | const moduleOutput = compilation.options.output.module; |
| 154 | const nodeTarget = compiler.platform.node; |
| 155 | const nodeEsm = moduleOutput && nodeTarget; |
| 156 | |
| 157 | const REPLACEMENTS = getReplacements(); |
| 158 | if (nodeEsm) { |
| 159 | REPLACEMENTS.__non_webpack_require__.expr = |
| 160 | "__WEBPACK_EXTERNAL_createRequire_require"; |
| 161 | } |
| 162 | |
| 163 | compilation.dependencyTemplates.set( |
| 164 | ConstDependency, |
| 165 | new ConstDependency.Template() |
| 166 | ); |
| 167 | compilation.dependencyTemplates.set( |
| 168 | ModuleInitFragmentDependency, |
| 169 | new ModuleInitFragmentDependency.Template() |
| 170 | ); |
| 171 | |
| 172 | compilation.hooks.runtimeRequirementInTree |
| 173 | .for(RuntimeGlobals.chunkName) |
| 174 | .tap(PLUGIN_NAME, (chunk) => { |
| 175 | compilation.addRuntimeModule( |
| 176 | chunk, |
| 177 | new ChunkNameRuntimeModule(/** @type {string} */ (chunk.name)) |
| 178 | ); |
| 179 | return true; |
| 180 | }); |
| 181 | |
| 182 | compilation.hooks.runtimeRequirementInTree |
| 183 | .for(RuntimeGlobals.getFullHash) |
| 184 | .tap(PLUGIN_NAME, (chunk, _set) => { |
| 185 | compilation.addRuntimeModule(chunk, new GetFullHashRuntimeModule()); |
| 186 | return true; |
| 187 | }); |
| 188 | |
| 189 | const hooks = JavascriptModulesPlugin.getCompilationHooks(compilation); |
| 190 | |
| 191 | hooks.renderModuleContent.tap( |
| 192 | PLUGIN_NAME, |
| 193 | (source, module, renderContext) => { |
| 194 | if ( |
| 195 | /** @type {JavascriptModuleBuildInfo} */ (module.buildInfo) |
| 196 | .needCreateRequire |
| 197 | ) { |
| 198 | const chunkInitFragments = [ |
| 199 | getExternalModuleNodeCommonjsInitFragment( |
| 200 | renderContext.runtimeTemplate |
| 201 | ) |
| 202 | ]; |
| 203 | |
| 204 | renderContext.chunkInitFragments.push(...chunkInitFragments); |
| 205 | } |
| 206 |
nothing calls this directly
no test coverage detected