| 
									
										
										
										
											2020-08-25 16:30:56 +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, RawSource } = require("webpack-sources"); | 
					
						
							| 
									
										
										
										
											2020-08-25 16:30:56 +08:00
										 |  |  | const RuntimeGlobals = require("../RuntimeGlobals"); | 
					
						
							|  |  |  | const Template = require("../Template"); | 
					
						
							|  |  |  | const { | 
					
						
							|  |  |  | 	getChunkFilenameTemplate, | 
					
						
							|  |  |  | 	getCompilationHooks | 
					
						
							|  |  |  | } = require("./JavascriptModulesPlugin"); | 
					
						
							| 
									
										
										
										
											2021-03-19 23:07:15 +08:00
										 |  |  | const { | 
					
						
							|  |  |  | 	generateEntryStartup, | 
					
						
							|  |  |  | 	updateHashForEntryStartup | 
					
						
							|  |  |  | } = require("./StartupHelpers"); | 
					
						
							| 
									
										
										
										
											2020-08-25 16:30:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class CommonJsChunkFormatPlugin { | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Apply the plugin | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							|  |  |  | 		compiler.hooks.thisCompilation.tap( | 
					
						
							|  |  |  | 			"CommonJsChunkFormatPlugin", | 
					
						
							|  |  |  | 			compilation => { | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 				compilation.hooks.additionalChunkRuntimeRequirements.tap( | 
					
						
							|  |  |  | 					"CommonJsChunkLoadingPlugin", | 
					
						
							| 
									
										
										
										
											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.require); | 
					
						
							|  |  |  | 							set.add(RuntimeGlobals.startupEntrypoint); | 
					
						
							|  |  |  | 							set.add(RuntimeGlobals.externalInstallChunk); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				); | 
					
						
							| 
									
										
										
										
											2020-08-25 16:30:56 +08:00
										 |  |  | 				const hooks = getCompilationHooks(compilation); | 
					
						
							|  |  |  | 				hooks.renderChunk.tap( | 
					
						
							|  |  |  | 					"CommonJsChunkFormatPlugin", | 
					
						
							|  |  |  | 					(modules, renderContext) => { | 
					
						
							|  |  |  | 						const { chunk, chunkGraph, runtimeTemplate } = renderContext; | 
					
						
							|  |  |  | 						const source = new ConcatSource(); | 
					
						
							|  |  |  | 						source.add(`exports.id = ${JSON.stringify(chunk.id)};\n`); | 
					
						
							|  |  |  | 						source.add(`exports.ids = ${JSON.stringify(chunk.ids)};\n`); | 
					
						
							|  |  |  | 						source.add(`exports.modules = `); | 
					
						
							|  |  |  | 						source.add(modules); | 
					
						
							|  |  |  | 						source.add(";\n"); | 
					
						
							|  |  |  | 						const runtimeModules = chunkGraph.getChunkRuntimeModulesInOrder( | 
					
						
							|  |  |  | 							chunk | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 						if (runtimeModules.length > 0) { | 
					
						
							|  |  |  | 							source.add("exports.runtime =\n"); | 
					
						
							|  |  |  | 							source.add( | 
					
						
							|  |  |  | 								Template.renderChunkRuntimeModules( | 
					
						
							|  |  |  | 									runtimeModules, | 
					
						
							|  |  |  | 									renderContext | 
					
						
							|  |  |  | 								) | 
					
						
							|  |  |  | 							); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						const entries = Array.from( | 
					
						
							|  |  |  | 							chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk) | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 						if (entries.length > 0) { | 
					
						
							|  |  |  | 							const runtimeChunk = entries[0][1].getRuntimeChunk(); | 
					
						
							|  |  |  | 							const currentOutputName = compilation | 
					
						
							|  |  |  | 								.getPath( | 
					
						
							|  |  |  | 									getChunkFilenameTemplate(chunk, compilation.outputOptions), | 
					
						
							|  |  |  | 									{ | 
					
						
							|  |  |  | 										chunk, | 
					
						
							|  |  |  | 										contentHashType: "javascript" | 
					
						
							|  |  |  | 									} | 
					
						
							|  |  |  | 								) | 
					
						
							|  |  |  | 								.split("/"); | 
					
						
							|  |  |  | 							const runtimeOutputName = compilation | 
					
						
							|  |  |  | 								.getPath( | 
					
						
							|  |  |  | 									getChunkFilenameTemplate( | 
					
						
							|  |  |  | 										runtimeChunk, | 
					
						
							|  |  |  | 										compilation.outputOptions | 
					
						
							|  |  |  | 									), | 
					
						
							|  |  |  | 									{ | 
					
						
							|  |  |  | 										chunk: runtimeChunk, | 
					
						
							|  |  |  | 										contentHashType: "javascript" | 
					
						
							|  |  |  | 									} | 
					
						
							|  |  |  | 								) | 
					
						
							|  |  |  | 								.split("/"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 							// remove filename, we only need the directory
 | 
					
						
							|  |  |  | 							currentOutputName.pop(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 							// remove common parts
 | 
					
						
							|  |  |  | 							while ( | 
					
						
							|  |  |  | 								currentOutputName.length > 0 && | 
					
						
							|  |  |  | 								runtimeOutputName.length > 0 && | 
					
						
							|  |  |  | 								currentOutputName[0] === runtimeOutputName[0] | 
					
						
							|  |  |  | 							) { | 
					
						
							|  |  |  | 								currentOutputName.shift(); | 
					
						
							|  |  |  | 								runtimeOutputName.shift(); | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 							// create final path
 | 
					
						
							|  |  |  | 							const runtimePath = | 
					
						
							|  |  |  | 								(currentOutputName.length > 0 | 
					
						
							|  |  |  | 									? "../".repeat(currentOutputName.length) | 
					
						
							|  |  |  | 									: "./") + runtimeOutputName.join("/"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 							const entrySource = new ConcatSource(); | 
					
						
							|  |  |  | 							entrySource.add( | 
					
						
							|  |  |  | 								`(${ | 
					
						
							|  |  |  | 									runtimeTemplate.supportsArrowFunction() | 
					
						
							|  |  |  | 										? "() => " | 
					
						
							|  |  |  | 										: "function() " | 
					
						
							|  |  |  | 								}{\n`
 | 
					
						
							|  |  |  | 							); | 
					
						
							|  |  |  | 							entrySource.add("var exports = {};\n"); | 
					
						
							|  |  |  | 							entrySource.add(source); | 
					
						
							|  |  |  | 							entrySource.add(";\n\n// load runtime\n"); | 
					
						
							|  |  |  | 							entrySource.add( | 
					
						
							|  |  |  | 								`var __webpack_require__ = require(${JSON.stringify( | 
					
						
							|  |  |  | 									runtimePath | 
					
						
							|  |  |  | 								)});\n`
 | 
					
						
							|  |  |  | 							); | 
					
						
							|  |  |  | 							entrySource.add( | 
					
						
							|  |  |  | 								`${RuntimeGlobals.externalInstallChunk}(exports);\n` | 
					
						
							|  |  |  | 							); | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 							const startupSource = new RawSource( | 
					
						
							|  |  |  | 								generateEntryStartup( | 
					
						
							|  |  |  | 									chunkGraph, | 
					
						
							|  |  |  | 									runtimeTemplate, | 
					
						
							|  |  |  | 									entries, | 
					
						
							|  |  |  | 									chunk, | 
					
						
							|  |  |  | 									false | 
					
						
							|  |  |  | 								) | 
					
						
							|  |  |  | 							); | 
					
						
							| 
									
										
										
										
											2021-02-18 22:49:43 +08:00
										 |  |  | 							entrySource.add( | 
					
						
							|  |  |  | 								hooks.renderStartup.call( | 
					
						
							|  |  |  | 									startupSource, | 
					
						
							|  |  |  | 									entries[entries.length - 1][0], | 
					
						
							|  |  |  | 									{ | 
					
						
							|  |  |  | 										...renderContext, | 
					
						
							|  |  |  | 										inlined: false | 
					
						
							|  |  |  | 									} | 
					
						
							|  |  |  | 								) | 
					
						
							|  |  |  | 							); | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 							entrySource.add("\n})()"); | 
					
						
							| 
									
										
										
										
											2020-08-25 16:30:56 +08:00
										 |  |  | 							return entrySource; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						return source; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 				hooks.chunkHash.tap( | 
					
						
							|  |  |  | 					"CommonJsChunkFormatPlugin", | 
					
						
							|  |  |  | 					(chunk, hash, { chunkGraph }) => { | 
					
						
							|  |  |  | 						if (chunk.hasRuntime()) return; | 
					
						
							|  |  |  | 						hash.update("CommonJsChunkFormatPlugin"); | 
					
						
							|  |  |  | 						hash.update("1"); | 
					
						
							| 
									
										
										
										
											2021-03-19 23:07:15 +08:00
										 |  |  | 						const entries = Array.from( | 
					
						
							|  |  |  | 							chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk) | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 						updateHashForEntryStartup(hash, chunkGraph, entries, chunk); | 
					
						
							| 
									
										
										
										
											2020-08-25 16:30:56 +08:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = CommonJsChunkFormatPlugin; |