| 
									
										
										
										
											2020-08-25 02:26:31 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | const { ConcatSource, PrefixSource, RawSource } = require("webpack-sources"); | 
					
						
							|  |  |  | const { RuntimeGlobals } = require(".."); | 
					
						
							| 
									
										
										
										
											2020-08-25 02:26:31 +08:00
										 |  |  | const HotUpdateChunk = require("../HotUpdateChunk"); | 
					
						
							|  |  |  | const Template = require("../Template"); | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | const { getCompilationHooks } = require("./JavascriptModulesPlugin"); | 
					
						
							| 
									
										
										
										
											2021-03-19 23:07:15 +08:00
										 |  |  | const { | 
					
						
							|  |  |  | 	generateEntryStartup, | 
					
						
							|  |  |  | 	updateHashForEntryStartup | 
					
						
							|  |  |  | } = require("./StartupHelpers"); | 
					
						
							| 
									
										
										
										
											2020-08-25 02:26:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							| 
									
										
										
										
											2025-07-03 17:06:45 +08:00
										 |  |  | /** @typedef {import("../ChunkGraph").EntryModuleWithChunkGroup} EntryModuleWithChunkGroup */ | 
					
						
							| 
									
										
										
										
											2025-09-02 22:12:40 +08:00
										 |  |  | /** @typedef {import("../CodeGenerationResults")} CodeGenerationResults */ | 
					
						
							| 
									
										
										
										
											2020-08-25 02:26:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-04 02:20:37 +08:00
										 |  |  | const PLUGIN_NAME = "ArrayPushCallbackChunkFormatPlugin"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-25 02:26:31 +08:00
										 |  |  | class ArrayPushCallbackChunkFormatPlugin { | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Apply the plugin | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 		compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => { | 
					
						
							| 
									
										
										
										
											2025-06-04 02:20:37 +08:00
										 |  |  | 			compilation.hooks.additionalChunkRuntimeRequirements.tap( | 
					
						
							|  |  |  | 				PLUGIN_NAME, | 
					
						
							|  |  |  | 				(chunk, set, { chunkGraph }) => { | 
					
						
							|  |  |  | 					if (chunk.hasRuntime()) return; | 
					
						
							|  |  |  | 					if (chunkGraph.getNumberOfEntryModules(chunk) > 0) { | 
					
						
							|  |  |  | 						set.add(RuntimeGlobals.onChunksLoaded); | 
					
						
							|  |  |  | 						set.add(RuntimeGlobals.exports); | 
					
						
							|  |  |  | 						set.add(RuntimeGlobals.require); | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2025-06-04 02:20:37 +08:00
										 |  |  | 					set.add(RuntimeGlobals.chunkCallback); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 			const hooks = getCompilationHooks(compilation); | 
					
						
							|  |  |  | 			hooks.renderChunk.tap(PLUGIN_NAME, (modules, renderContext) => { | 
					
						
							|  |  |  | 				const { chunk, chunkGraph, runtimeTemplate } = renderContext; | 
					
						
							|  |  |  | 				const hotUpdateChunk = chunk instanceof HotUpdateChunk ? chunk : null; | 
					
						
							|  |  |  | 				const globalObject = runtimeTemplate.globalObject; | 
					
						
							|  |  |  | 				const source = new ConcatSource(); | 
					
						
							|  |  |  | 				const runtimeModules = chunkGraph.getChunkRuntimeModulesInOrder(chunk); | 
					
						
							|  |  |  | 				if (hotUpdateChunk) { | 
					
						
							|  |  |  | 					const hotUpdateGlobal = runtimeTemplate.outputOptions.hotUpdateGlobal; | 
					
						
							|  |  |  | 					source.add(`${globalObject}[${JSON.stringify(hotUpdateGlobal)}](`); | 
					
						
							|  |  |  | 					source.add(`${JSON.stringify(chunk.id)},`); | 
					
						
							|  |  |  | 					source.add(modules); | 
					
						
							|  |  |  | 					if (runtimeModules.length > 0) { | 
					
						
							|  |  |  | 						source.add(",\n"); | 
					
						
							|  |  |  | 						const runtimePart = Template.renderChunkRuntimeModules( | 
					
						
							|  |  |  | 							runtimeModules, | 
					
						
							|  |  |  | 							renderContext | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 						source.add(runtimePart); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					source.add(")"); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					const chunkLoadingGlobal = | 
					
						
							|  |  |  | 						runtimeTemplate.outputOptions.chunkLoadingGlobal; | 
					
						
							|  |  |  | 					source.add( | 
					
						
							|  |  |  | 						`(${globalObject}[${JSON.stringify( | 
					
						
							|  |  |  | 							chunkLoadingGlobal | 
					
						
							|  |  |  | 						)}] = ${globalObject}[${JSON.stringify( | 
					
						
							|  |  |  | 							chunkLoadingGlobal | 
					
						
							|  |  |  | 						)}] || []).push([`
 | 
					
						
							|  |  |  | 					); | 
					
						
							|  |  |  | 					source.add(`${JSON.stringify(chunk.ids)},`); | 
					
						
							|  |  |  | 					source.add(modules); | 
					
						
							| 
									
										
										
										
											2025-07-03 17:06:45 +08:00
										 |  |  | 					/** @type {EntryModuleWithChunkGroup[]} */ | 
					
						
							|  |  |  | 					const entries = [ | 
					
						
							|  |  |  | 						...chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk) | 
					
						
							|  |  |  | 					]; | 
					
						
							| 
									
										
										
										
											2025-06-04 02:20:37 +08:00
										 |  |  | 					if (runtimeModules.length > 0 || entries.length > 0) { | 
					
						
							|  |  |  | 						const runtime = new ConcatSource( | 
					
						
							|  |  |  | 							`${ | 
					
						
							|  |  |  | 								runtimeTemplate.supportsArrowFunction() | 
					
						
							|  |  |  | 									? `${RuntimeGlobals.require} =>` | 
					
						
							|  |  |  | 									: `function(${RuntimeGlobals.require})` | 
					
						
							|  |  |  | 							} { // webpackRuntimeModules\n`
 | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 						if (runtimeModules.length > 0) { | 
					
						
							|  |  |  | 							runtime.add( | 
					
						
							|  |  |  | 								Template.renderRuntimeModules(runtimeModules, { | 
					
						
							|  |  |  | 									...renderContext, | 
					
						
							| 
									
										
										
										
											2025-09-02 22:12:40 +08:00
										 |  |  | 									codeGenerationResults: | 
					
						
							|  |  |  | 										/** @type {CodeGenerationResults} */ | 
					
						
							|  |  |  | 										(compilation.codeGenerationResults) | 
					
						
							| 
									
										
										
										
											2025-06-04 02:20:37 +08:00
										 |  |  | 								}) | 
					
						
							| 
									
										
										
										
											2020-08-25 02:26:31 +08:00
										 |  |  | 							); | 
					
						
							| 
									
										
										
										
											2025-06-04 02:20:37 +08:00
										 |  |  | 						} | 
					
						
							|  |  |  | 						if (entries.length > 0) { | 
					
						
							|  |  |  | 							const startupSource = new RawSource( | 
					
						
							|  |  |  | 								generateEntryStartup( | 
					
						
							|  |  |  | 									chunkGraph, | 
					
						
							|  |  |  | 									runtimeTemplate, | 
					
						
							|  |  |  | 									entries, | 
					
						
							|  |  |  | 									chunk, | 
					
						
							|  |  |  | 									true | 
					
						
							|  |  |  | 								) | 
					
						
							| 
									
										
										
										
											2020-08-25 02:26:31 +08:00
										 |  |  | 							); | 
					
						
							| 
									
										
										
										
											2025-06-04 02:20:37 +08:00
										 |  |  | 							runtime.add( | 
					
						
							|  |  |  | 								hooks.renderStartup.call( | 
					
						
							|  |  |  | 									startupSource, | 
					
						
							|  |  |  | 									entries[entries.length - 1][0], | 
					
						
							|  |  |  | 									{ | 
					
						
							|  |  |  | 										...renderContext, | 
					
						
							|  |  |  | 										inlined: false | 
					
						
							| 
									
										
										
										
											2021-04-19 19:04:04 +08:00
										 |  |  | 									} | 
					
						
							| 
									
										
										
										
											2025-06-04 02:20:37 +08:00
										 |  |  | 								) | 
					
						
							|  |  |  | 							); | 
					
						
							|  |  |  | 							if ( | 
					
						
							|  |  |  | 								chunkGraph | 
					
						
							|  |  |  | 									.getChunkRuntimeRequirements(chunk) | 
					
						
							|  |  |  | 									.has(RuntimeGlobals.returnExportsFromRuntime) | 
					
						
							|  |  |  | 							) { | 
					
						
							|  |  |  | 								runtime.add(`return ${RuntimeGlobals.exports};\n`); | 
					
						
							| 
									
										
										
										
											2020-08-25 02:26:31 +08:00
										 |  |  | 							} | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2025-06-04 02:20:37 +08:00
										 |  |  | 						runtime.add("}\n"); | 
					
						
							|  |  |  | 						source.add(",\n"); | 
					
						
							|  |  |  | 						source.add(new PrefixSource("/******/ ", runtime)); | 
					
						
							| 
									
										
										
										
											2020-08-25 02:26:31 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2025-06-04 02:20:37 +08:00
										 |  |  | 					source.add("])"); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				return source; | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			hooks.chunkHash.tap( | 
					
						
							|  |  |  | 				PLUGIN_NAME, | 
					
						
							|  |  |  | 				(chunk, hash, { chunkGraph, runtimeTemplate }) => { | 
					
						
							|  |  |  | 					if (chunk.hasRuntime()) return; | 
					
						
							|  |  |  | 					hash.update( | 
					
						
							|  |  |  | 						`${PLUGIN_NAME}1${runtimeTemplate.outputOptions.chunkLoadingGlobal}${runtimeTemplate.outputOptions.hotUpdateGlobal}${runtimeTemplate.globalObject}` | 
					
						
							|  |  |  | 					); | 
					
						
							| 
									
										
										
										
											2025-07-03 17:06:45 +08:00
										 |  |  | 					/** @type {EntryModuleWithChunkGroup[]} */ | 
					
						
							|  |  |  | 					const entries = [ | 
					
						
							|  |  |  | 						...chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk) | 
					
						
							|  |  |  | 					]; | 
					
						
							| 
									
										
										
										
											2025-06-04 02:20:37 +08:00
										 |  |  | 					updateHashForEntryStartup(hash, chunkGraph, entries, chunk); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2020-08-25 02:26:31 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = ArrayPushCallbackChunkFormatPlugin; |