| 
									
										
										
										
											2021-06-23 19:58:40 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-25 20:21:40 +08:00
										 |  |  | const { ConcatSource } = require("webpack-sources"); | 
					
						
							| 
									
										
										
										
											2021-06-23 19:58:40 +08:00
										 |  |  | const { RuntimeGlobals } = require(".."); | 
					
						
							|  |  |  | const HotUpdateChunk = require("../HotUpdateChunk"); | 
					
						
							|  |  |  | const Template = require("../Template"); | 
					
						
							| 
									
										
										
										
											2021-11-25 20:21:40 +08:00
										 |  |  | const { getAllChunks } = require("../javascript/ChunkHelpers"); | 
					
						
							| 
									
										
										
										
											2021-06-23 19:58:40 +08:00
										 |  |  | const { | 
					
						
							| 
									
										
										
										
											2021-07-08 15:13:42 +08:00
										 |  |  | 	getCompilationHooks, | 
					
						
							|  |  |  | 	getChunkFilenameTemplate | 
					
						
							| 
									
										
										
										
											2021-06-23 19:58:40 +08:00
										 |  |  | } = require("../javascript/JavascriptModulesPlugin"); | 
					
						
							| 
									
										
										
										
											2021-11-25 20:21:40 +08:00
										 |  |  | const { updateHashForEntryStartup } = require("../javascript/StartupHelpers"); | 
					
						
							| 
									
										
										
										
											2021-06-23 19:58:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ModuleChunkFormatPlugin { | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Apply the plugin | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							|  |  |  | 		compiler.hooks.thisCompilation.tap( | 
					
						
							|  |  |  | 			"ModuleChunkFormatPlugin", | 
					
						
							|  |  |  | 			compilation => { | 
					
						
							|  |  |  | 				compilation.hooks.additionalChunkRuntimeRequirements.tap( | 
					
						
							|  |  |  | 					"ModuleChunkFormatPlugin", | 
					
						
							|  |  |  | 					(chunk, set) => { | 
					
						
							|  |  |  | 						if (chunk.hasRuntime()) return; | 
					
						
							|  |  |  | 						if (compilation.chunkGraph.getNumberOfEntryModules(chunk) > 0) { | 
					
						
							|  |  |  | 							set.add(RuntimeGlobals.require); | 
					
						
							| 
									
										
										
										
											2021-07-08 15:13:42 +08:00
										 |  |  | 							set.add(RuntimeGlobals.startupEntrypoint); | 
					
						
							|  |  |  | 							set.add(RuntimeGlobals.externalInstallChunk); | 
					
						
							| 
									
										
										
										
											2021-06-23 19:58:40 +08:00
										 |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 				const hooks = getCompilationHooks(compilation); | 
					
						
							|  |  |  | 				hooks.renderChunk.tap( | 
					
						
							|  |  |  | 					"ModuleChunkFormatPlugin", | 
					
						
							|  |  |  | 					(modules, renderContext) => { | 
					
						
							| 
									
										
										
										
											2021-07-08 15:13:42 +08:00
										 |  |  | 						const { chunk, chunkGraph, runtimeTemplate } = renderContext; | 
					
						
							| 
									
										
										
										
											2021-06-23 19:58:40 +08:00
										 |  |  | 						const hotUpdateChunk = | 
					
						
							|  |  |  | 							chunk instanceof HotUpdateChunk ? chunk : null; | 
					
						
							|  |  |  | 						const source = new ConcatSource(); | 
					
						
							|  |  |  | 						if (hotUpdateChunk) { | 
					
						
							|  |  |  | 							throw new Error( | 
					
						
							|  |  |  | 								"HMR is not implemented for module chunk format yet" | 
					
						
							|  |  |  | 							); | 
					
						
							|  |  |  | 						} else { | 
					
						
							|  |  |  | 							source.add(`export const id = ${JSON.stringify(chunk.id)};\n`); | 
					
						
							|  |  |  | 							source.add(`export const ids = ${JSON.stringify(chunk.ids)};\n`); | 
					
						
							|  |  |  | 							source.add(`export const modules = `); | 
					
						
							|  |  |  | 							source.add(modules); | 
					
						
							|  |  |  | 							source.add(`;\n`); | 
					
						
							|  |  |  | 							const runtimeModules = | 
					
						
							|  |  |  | 								chunkGraph.getChunkRuntimeModulesInOrder(chunk); | 
					
						
							|  |  |  | 							if (runtimeModules.length > 0) { | 
					
						
							|  |  |  | 								source.add("export const runtime =\n"); | 
					
						
							|  |  |  | 								source.add( | 
					
						
							|  |  |  | 									Template.renderChunkRuntimeModules( | 
					
						
							|  |  |  | 										runtimeModules, | 
					
						
							|  |  |  | 										renderContext | 
					
						
							|  |  |  | 									) | 
					
						
							|  |  |  | 								); | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 							const entries = Array.from( | 
					
						
							|  |  |  | 								chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk) | 
					
						
							|  |  |  | 							); | 
					
						
							|  |  |  | 							if (entries.length > 0) { | 
					
						
							| 
									
										
										
										
											2021-07-08 15:13:42 +08:00
										 |  |  | 								const runtimeChunk = entries[0][1].getRuntimeChunk(); | 
					
						
							|  |  |  | 								const currentOutputName = compilation | 
					
						
							|  |  |  | 									.getPath( | 
					
						
							|  |  |  | 										getChunkFilenameTemplate(chunk, compilation.outputOptions), | 
					
						
							|  |  |  | 										{ | 
					
						
							|  |  |  | 											chunk, | 
					
						
							|  |  |  | 											contentHashType: "javascript" | 
					
						
							|  |  |  | 										} | 
					
						
							|  |  |  | 									) | 
					
						
							|  |  |  | 									.split("/"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 								// remove filename, we only need the directory
 | 
					
						
							| 
									
										
										
										
											2021-11-25 20:21:40 +08:00
										 |  |  | 								currentOutputName.pop(); | 
					
						
							| 
									
										
										
										
											2021-07-08 15:13:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-25 20:21:40 +08:00
										 |  |  | 								const getRelativePath = chunk => { | 
					
						
							|  |  |  | 									const baseOutputName = currentOutputName.slice(); | 
					
						
							|  |  |  | 									const chunkOutputName = compilation | 
					
						
							|  |  |  | 										.getPath( | 
					
						
							|  |  |  | 											getChunkFilenameTemplate( | 
					
						
							|  |  |  | 												chunk, | 
					
						
							|  |  |  | 												compilation.outputOptions | 
					
						
							|  |  |  | 											), | 
					
						
							|  |  |  | 											{ | 
					
						
							|  |  |  | 												chunk: chunk, | 
					
						
							|  |  |  | 												contentHashType: "javascript" | 
					
						
							|  |  |  | 											} | 
					
						
							|  |  |  | 										) | 
					
						
							|  |  |  | 										.split("/"); | 
					
						
							| 
									
										
										
										
											2021-07-08 15:13:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-25 20:21:40 +08:00
										 |  |  | 									// remove common parts
 | 
					
						
							|  |  |  | 									while ( | 
					
						
							|  |  |  | 										baseOutputName.length > 0 && | 
					
						
							|  |  |  | 										chunkOutputName.length > 0 && | 
					
						
							|  |  |  | 										baseOutputName[0] === chunkOutputName[0] | 
					
						
							|  |  |  | 									) { | 
					
						
							|  |  |  | 										baseOutputName.shift(); | 
					
						
							|  |  |  | 										chunkOutputName.shift(); | 
					
						
							|  |  |  | 									} | 
					
						
							|  |  |  | 									// create final path
 | 
					
						
							|  |  |  | 									return ( | 
					
						
							|  |  |  | 										(baseOutputName.length > 0 | 
					
						
							|  |  |  | 											? "../".repeat(baseOutputName.length) | 
					
						
							|  |  |  | 											: "./") + chunkOutputName.join("/") | 
					
						
							|  |  |  | 									); | 
					
						
							|  |  |  | 								}; | 
					
						
							| 
									
										
										
										
											2021-07-08 15:13:42 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 								const entrySource = new ConcatSource(); | 
					
						
							|  |  |  | 								entrySource.add(source); | 
					
						
							|  |  |  | 								entrySource.add(";\n\n// load runtime\n"); | 
					
						
							|  |  |  | 								entrySource.add( | 
					
						
							|  |  |  | 									`import __webpack_require__ from ${JSON.stringify( | 
					
						
							| 
									
										
										
										
											2021-11-25 20:21:40 +08:00
										 |  |  | 										getRelativePath(runtimeChunk) | 
					
						
							| 
									
										
										
										
											2021-07-08 15:13:42 +08:00
										 |  |  | 									)};\n`
 | 
					
						
							|  |  |  | 								); | 
					
						
							| 
									
										
										
										
											2021-11-25 20:21:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 								const startupSource = new ConcatSource(); | 
					
						
							|  |  |  | 								startupSource.add( | 
					
						
							|  |  |  | 									`var __webpack_exec__ = ${runtimeTemplate.returningFunction( | 
					
						
							|  |  |  | 										`__webpack_require__(${RuntimeGlobals.entryModuleId} = moduleId)`, | 
					
						
							|  |  |  | 										"moduleId" | 
					
						
							|  |  |  | 									)}\n`
 | 
					
						
							| 
									
										
										
										
											2021-07-08 15:13:42 +08:00
										 |  |  | 								); | 
					
						
							| 
									
										
										
										
											2021-11-25 20:21:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 								const loadedChunks = new Set(); | 
					
						
							|  |  |  | 								let index = 0; | 
					
						
							|  |  |  | 								for (let i = 0; i < entries.length; i++) { | 
					
						
							|  |  |  | 									const [module, entrypoint] = entries[i]; | 
					
						
							|  |  |  | 									const final = i + 1 === entries.length; | 
					
						
							|  |  |  | 									const moduleId = chunkGraph.getModuleId(module); | 
					
						
							|  |  |  | 									const chunks = getAllChunks( | 
					
						
							|  |  |  | 										entrypoint, | 
					
						
							|  |  |  | 										runtimeChunk, | 
					
						
							|  |  |  | 										undefined | 
					
						
							|  |  |  | 									); | 
					
						
							|  |  |  | 									for (const chunk of chunks) { | 
					
						
							|  |  |  | 										if (loadedChunks.has(chunk)) continue; | 
					
						
							|  |  |  | 										loadedChunks.add(chunk); | 
					
						
							|  |  |  | 										startupSource.add( | 
					
						
							|  |  |  | 											`import * as __webpack_chunk_${index}__ from ${JSON.stringify( | 
					
						
							|  |  |  | 												getRelativePath(chunk) | 
					
						
							|  |  |  | 											)};\n`
 | 
					
						
							|  |  |  | 										); | 
					
						
							|  |  |  | 										startupSource.add( | 
					
						
							|  |  |  | 											`${RuntimeGlobals.externalInstallChunk}(__webpack_chunk_${index}__);\n` | 
					
						
							|  |  |  | 										); | 
					
						
							|  |  |  | 										index++; | 
					
						
							|  |  |  | 									} | 
					
						
							|  |  |  | 									startupSource.add( | 
					
						
							|  |  |  | 										`${ | 
					
						
							|  |  |  | 											final ? "var __webpack_exports__ = " : "" | 
					
						
							|  |  |  | 										}__webpack_exec__(${JSON.stringify(moduleId)});\n`
 | 
					
						
							|  |  |  | 									); | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-08 15:13:42 +08:00
										 |  |  | 								entrySource.add( | 
					
						
							|  |  |  | 									hooks.renderStartup.call( | 
					
						
							|  |  |  | 										startupSource, | 
					
						
							|  |  |  | 										entries[entries.length - 1][0], | 
					
						
							|  |  |  | 										{ | 
					
						
							|  |  |  | 											...renderContext, | 
					
						
							|  |  |  | 											inlined: false | 
					
						
							|  |  |  | 										} | 
					
						
							|  |  |  | 									) | 
					
						
							| 
									
										
										
										
											2021-06-23 19:58:40 +08:00
										 |  |  | 								); | 
					
						
							| 
									
										
										
										
											2021-07-08 15:13:42 +08:00
										 |  |  | 								return entrySource; | 
					
						
							| 
									
										
										
										
											2021-06-23 19:58:40 +08:00
										 |  |  | 							} | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						return source; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 				hooks.chunkHash.tap( | 
					
						
							|  |  |  | 					"ModuleChunkFormatPlugin", | 
					
						
							|  |  |  | 					(chunk, hash, { chunkGraph, runtimeTemplate }) => { | 
					
						
							|  |  |  | 						if (chunk.hasRuntime()) return; | 
					
						
							|  |  |  | 						hash.update("ModuleChunkFormatPlugin"); | 
					
						
							|  |  |  | 						hash.update("1"); | 
					
						
							| 
									
										
										
										
											2021-07-08 15:13:42 +08:00
										 |  |  | 						const entries = Array.from( | 
					
						
							|  |  |  | 							chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk) | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 						updateHashForEntryStartup(hash, chunkGraph, entries, chunk); | 
					
						
							| 
									
										
										
										
											2021-06-23 19:58:40 +08:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = ModuleChunkFormatPlugin; |