| 
									
										
										
										
											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 */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ArrayPushCallbackChunkFormatPlugin { | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Apply the plugin | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							|  |  |  | 		compiler.hooks.thisCompilation.tap( | 
					
						
							|  |  |  | 			"ArrayPushCallbackChunkFormatPlugin", | 
					
						
							|  |  |  | 			compilation => { | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 				compilation.hooks.additionalChunkRuntimeRequirements.tap( | 
					
						
							|  |  |  | 					"ArrayPushCallbackChunkFormatPlugin", | 
					
						
							| 
									
										
										
										
											2021-04-09 21:50:25 +08:00
										 |  |  | 					(chunk, set, { chunkGraph }) => { | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 						if (chunk.hasRuntime()) return; | 
					
						
							| 
									
										
										
										
											2021-04-09 21:50:25 +08:00
										 |  |  | 						if (chunkGraph.getNumberOfEntryModules(chunk) > 0) { | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 							set.add(RuntimeGlobals.onChunksLoaded); | 
					
						
							| 
									
										
										
										
											2024-07-09 20:02:25 +08:00
										 |  |  | 							set.add(RuntimeGlobals.exports); | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 							set.add(RuntimeGlobals.require); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						set.add(RuntimeGlobals.chunkCallback); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				); | 
					
						
							| 
									
										
										
										
											2020-08-25 02:26:31 +08:00
										 |  |  | 				const hooks = getCompilationHooks(compilation); | 
					
						
							|  |  |  | 				hooks.renderChunk.tap( | 
					
						
							|  |  |  | 					"ArrayPushCallbackChunkFormatPlugin", | 
					
						
							|  |  |  | 					(modules, renderContext) => { | 
					
						
							|  |  |  | 						const { chunk, chunkGraph, runtimeTemplate } = renderContext; | 
					
						
							|  |  |  | 						const hotUpdateChunk = | 
					
						
							|  |  |  | 							chunk instanceof HotUpdateChunk ? chunk : null; | 
					
						
							| 
									
										
										
										
											2021-11-24 01:40:03 +08:00
										 |  |  | 						const globalObject = runtimeTemplate.globalObject; | 
					
						
							| 
									
										
										
										
											2020-08-25 02:26:31 +08:00
										 |  |  | 						const source = new ConcatSource(); | 
					
						
							| 
									
										
										
										
											2021-05-11 15:31:46 +08:00
										 |  |  | 						const runtimeModules = | 
					
						
							|  |  |  | 							chunkGraph.getChunkRuntimeModulesInOrder(chunk); | 
					
						
							| 
									
										
										
										
											2020-08-25 02:26:31 +08:00
										 |  |  | 						if (hotUpdateChunk) { | 
					
						
							| 
									
										
										
										
											2020-08-25 15:16:49 +08:00
										 |  |  | 							const hotUpdateGlobal = | 
					
						
							|  |  |  | 								runtimeTemplate.outputOptions.hotUpdateGlobal; | 
					
						
							|  |  |  | 							source.add( | 
					
						
							|  |  |  | 								`${globalObject}[${JSON.stringify(hotUpdateGlobal)}](` | 
					
						
							|  |  |  | 							); | 
					
						
							| 
									
										
										
										
											2020-08-25 02:26:31 +08:00
										 |  |  | 							source.add(`${JSON.stringify(chunk.id)},`); | 
					
						
							|  |  |  | 							source.add(modules); | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 							if (runtimeModules.length > 0) { | 
					
						
							| 
									
										
										
										
											2020-08-25 02:26:31 +08:00
										 |  |  | 								source.add(",\n"); | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 								const runtimePart = Template.renderChunkRuntimeModules( | 
					
						
							|  |  |  | 									runtimeModules, | 
					
						
							|  |  |  | 									renderContext | 
					
						
							|  |  |  | 								); | 
					
						
							| 
									
										
										
										
											2020-08-25 02:26:31 +08:00
										 |  |  | 								source.add(runtimePart); | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 							source.add(")"); | 
					
						
							|  |  |  | 						} else { | 
					
						
							| 
									
										
										
										
											2020-08-25 15:16:49 +08:00
										 |  |  | 							const chunkLoadingGlobal = | 
					
						
							|  |  |  | 								runtimeTemplate.outputOptions.chunkLoadingGlobal; | 
					
						
							| 
									
										
										
										
											2020-08-25 02:26:31 +08:00
										 |  |  | 							source.add( | 
					
						
							|  |  |  | 								`(${globalObject}[${JSON.stringify( | 
					
						
							| 
									
										
										
										
											2020-08-25 15:16:49 +08:00
										 |  |  | 									chunkLoadingGlobal | 
					
						
							| 
									
										
										
										
											2020-08-25 02:26:31 +08:00
										 |  |  | 								)}] = ${globalObject}[${JSON.stringify( | 
					
						
							| 
									
										
										
										
											2020-08-25 15:16:49 +08:00
										 |  |  | 									chunkLoadingGlobal | 
					
						
							| 
									
										
										
										
											2020-08-25 02:26:31 +08:00
										 |  |  | 								)}] || []).push([`
 | 
					
						
							|  |  |  | 							); | 
					
						
							|  |  |  | 							source.add(`${JSON.stringify(chunk.ids)},`); | 
					
						
							|  |  |  | 							source.add(modules); | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 							const entries = Array.from( | 
					
						
							|  |  |  | 								chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk) | 
					
						
							| 
									
										
										
										
											2020-08-25 02:26:31 +08:00
										 |  |  | 							); | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 							if (runtimeModules.length > 0 || entries.length > 0) { | 
					
						
							|  |  |  | 								const runtime = new ConcatSource( | 
					
						
							| 
									
										
										
										
											2024-07-31 10:39:30 +08:00
										 |  |  | 									`${ | 
					
						
							|  |  |  | 										runtimeTemplate.supportsArrowFunction() | 
					
						
							|  |  |  | 											? `${RuntimeGlobals.require} =>` | 
					
						
							|  |  |  | 											: `function(${RuntimeGlobals.require})` | 
					
						
							|  |  |  | 									} { // webpackRuntimeModules\n`
 | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 								); | 
					
						
							|  |  |  | 								if (runtimeModules.length > 0) { | 
					
						
							|  |  |  | 									runtime.add( | 
					
						
							| 
									
										
										
										
											2021-03-17 00:07:10 +08:00
										 |  |  | 										Template.renderRuntimeModules(runtimeModules, { | 
					
						
							|  |  |  | 											...renderContext, | 
					
						
							| 
									
										
										
										
											2021-07-22 22:09:09 +08:00
										 |  |  | 											codeGenerationResults: compilation.codeGenerationResults | 
					
						
							| 
									
										
										
										
											2021-03-17 00:07:10 +08:00
										 |  |  | 										}) | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 									); | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 								if (entries.length > 0) { | 
					
						
							|  |  |  | 									const startupSource = new RawSource( | 
					
						
							|  |  |  | 										generateEntryStartup( | 
					
						
							|  |  |  | 											chunkGraph, | 
					
						
							|  |  |  | 											runtimeTemplate, | 
					
						
							|  |  |  | 											entries, | 
					
						
							|  |  |  | 											chunk, | 
					
						
							|  |  |  | 											true | 
					
						
							|  |  |  | 										) | 
					
						
							|  |  |  | 									); | 
					
						
							|  |  |  | 									runtime.add( | 
					
						
							|  |  |  | 										hooks.renderStartup.call( | 
					
						
							|  |  |  | 											startupSource, | 
					
						
							|  |  |  | 											entries[entries.length - 1][0], | 
					
						
							|  |  |  | 											{ | 
					
						
							|  |  |  | 												...renderContext, | 
					
						
							|  |  |  | 												inlined: false | 
					
						
							|  |  |  | 											} | 
					
						
							|  |  |  | 										) | 
					
						
							|  |  |  | 									); | 
					
						
							| 
									
										
										
										
											2021-04-19 19:04:04 +08:00
										 |  |  | 									if ( | 
					
						
							|  |  |  | 										chunkGraph | 
					
						
							|  |  |  | 											.getChunkRuntimeRequirements(chunk) | 
					
						
							|  |  |  | 											.has(RuntimeGlobals.returnExportsFromRuntime) | 
					
						
							|  |  |  | 									) { | 
					
						
							| 
									
										
										
										
											2023-05-26 23:34:26 +08:00
										 |  |  | 										runtime.add(`return ${RuntimeGlobals.exports};\n`); | 
					
						
							| 
									
										
										
										
											2021-04-19 19:04:04 +08:00
										 |  |  | 									} | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 								} | 
					
						
							|  |  |  | 								runtime.add("}\n"); | 
					
						
							| 
									
										
										
										
											2020-08-25 02:26:31 +08:00
										 |  |  | 								source.add(",\n"); | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 								source.add(new PrefixSource("/******/ ", runtime)); | 
					
						
							| 
									
										
										
										
											2020-08-25 02:26:31 +08:00
										 |  |  | 							} | 
					
						
							|  |  |  | 							source.add("])"); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						return source; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 				hooks.chunkHash.tap( | 
					
						
							|  |  |  | 					"ArrayPushCallbackChunkFormatPlugin", | 
					
						
							|  |  |  | 					(chunk, hash, { chunkGraph, runtimeTemplate }) => { | 
					
						
							|  |  |  | 						if (chunk.hasRuntime()) return; | 
					
						
							| 
									
										
										
										
											2021-09-26 08:15:41 +08:00
										 |  |  | 						hash.update( | 
					
						
							| 
									
										
										
										
											2021-11-24 01:40:03 +08:00
										 |  |  | 							`ArrayPushCallbackChunkFormatPlugin1${runtimeTemplate.outputOptions.chunkLoadingGlobal}${runtimeTemplate.outputOptions.hotUpdateGlobal}${runtimeTemplate.globalObject}` | 
					
						
							| 
									
										
										
										
											2021-09-26 08:15:41 +08:00
										 |  |  | 						); | 
					
						
							| 
									
										
										
										
											2021-03-19 23:07:15 +08:00
										 |  |  | 						const entries = Array.from( | 
					
						
							|  |  |  | 							chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk) | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 						updateHashForEntryStartup(hash, chunkGraph, entries, chunk); | 
					
						
							| 
									
										
										
										
											2020-08-25 02:26:31 +08:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = ArrayPushCallbackChunkFormatPlugin; |