* Applies the plugin by registering its hooks on the compiler. * @param {Compiler} compiler the compiler instance * @returns {void}
(compiler)
| 101 | * @returns {void} |
| 102 | */ |
| 103 | apply(compiler) { |
| 104 | compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => { |
| 105 | compilation.hooks.additionalChunkRuntimeRequirements.tap( |
| 106 | PLUGIN_NAME, |
| 107 | (chunk, set) => { |
| 108 | if (chunk.hasRuntime()) return; |
| 109 | if (compilation.chunkGraph.getNumberOfEntryModules(chunk) > 0) { |
| 110 | set.add(RuntimeGlobals.require); |
| 111 | set.add(RuntimeGlobals.externalInstallChunk); |
| 112 | } |
| 113 | } |
| 114 | ); |
| 115 | const hooks = getCompilationHooks(compilation); |
| 116 | /** |
| 117 | * With dependent chunks. |
| 118 | * @param {Iterable<Chunk>} chunks the chunks to render |
| 119 | * @param {ChunkGraph} chunkGraph the chunk graph |
| 120 | * @param {Chunk=} runtimeChunk the runtime chunk |
| 121 | * @returns {Source | undefined} the source |
| 122 | */ |
| 123 | const withDependentChunks = (chunks, chunkGraph, runtimeChunk) => { |
| 124 | if (/** @type {Set<Chunk>} */ (chunks).size > 0) { |
| 125 | const source = new ConcatSource(); |
| 126 | let index = 0; |
| 127 | |
| 128 | for (const chunk of chunks) { |
| 129 | index++; |
| 130 | |
| 131 | if (!chunkHasJs(chunk, chunkGraph)) { |
| 132 | continue; |
| 133 | } |
| 134 | const namedImport = getChunkNamedImport(index); |
| 135 | source.add( |
| 136 | renderChunkImport( |
| 137 | compilation, |
| 138 | chunk, |
| 139 | namedImport, |
| 140 | runtimeChunk || chunk |
| 141 | ) |
| 142 | ); |
| 143 | source.add( |
| 144 | `${RuntimeGlobals.externalInstallChunk}(${namedImport});\n` |
| 145 | ); |
| 146 | } |
| 147 | return source; |
| 148 | } |
| 149 | }; |
| 150 | hooks.renderStartup.tap( |
| 151 | PLUGIN_NAME, |
| 152 | (modules, _lastModule, renderContext) => { |
| 153 | const { chunk, chunkGraph } = renderContext; |
| 154 | if ( |
| 155 | chunkGraph.getNumberOfEntryModules(chunk) > 0 && |
| 156 | chunk.hasRuntime() |
| 157 | ) { |
| 158 | const entryDependentChunks = |
| 159 | chunkGraph.getChunkEntryDependentChunksIterable(chunk); |
| 160 | const sourceWithDependentChunks = withDependentChunks( |
nothing calls this directly
no test coverage detected