| 
									
										
										
										
											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); | 
					
						
							|  |  |  | 							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; | 
					
						
							|  |  |  | 						const globalObject = runtimeTemplate.outputOptions.globalObject; | 
					
						
							|  |  |  | 						const source = new ConcatSource(); | 
					
						
							|  |  |  | 						const runtimeModules = chunkGraph.getChunkRuntimeModulesInOrder( | 
					
						
							|  |  |  | 							chunk | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 						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) { | 
					
						
							| 
									
										
										
										
											2021-03-17 00:07:10 +08:00
										 |  |  | 								const strictBailout = hooks.strictRuntimeBailout.call( | 
					
						
							|  |  |  | 									renderContext | 
					
						
							|  |  |  | 								); | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 								const runtime = new ConcatSource( | 
					
						
							|  |  |  | 									(runtimeTemplate.supportsArrowFunction() | 
					
						
							|  |  |  | 										? "__webpack_require__ =>" | 
					
						
							|  |  |  | 										: "function(__webpack_require__)") + | 
					
						
							|  |  |  | 										" { // webpackRuntimeModules\n", | 
					
						
							| 
									
										
										
										
											2021-03-17 00:07:10 +08:00
										 |  |  | 									strictBailout | 
					
						
							|  |  |  | 										? `// runtime can't be in strict mode because ${strictBailout}.\n\n` | 
					
						
							|  |  |  | 										: '"use strict";\n\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, | 
					
						
							|  |  |  | 											codeGenerationResults: compilation.codeGenerationResults, | 
					
						
							|  |  |  | 											useStrict: !!strictBailout | 
					
						
							|  |  |  | 										}) | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 											} | 
					
						
							|  |  |  | 										) | 
					
						
							|  |  |  | 									); | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 								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; | 
					
						
							|  |  |  | 						hash.update("ArrayPushCallbackChunkFormatPlugin"); | 
					
						
							|  |  |  | 						hash.update("1"); | 
					
						
							| 
									
										
										
										
											2020-08-25 15:16:49 +08:00
										 |  |  | 						hash.update(`${runtimeTemplate.outputOptions.chunkLoadingGlobal}`); | 
					
						
							|  |  |  | 						hash.update(`${runtimeTemplate.outputOptions.hotUpdateGlobal}`); | 
					
						
							| 
									
										
										
										
											2020-08-25 02:26:31 +08:00
										 |  |  | 						hash.update(`${runtimeTemplate.outputOptions.globalObject}`); | 
					
						
							| 
									
										
										
										
											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; |