| 
									
										
										
										
											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"); | 
					
						
							| 
									
										
										
										
											2024-07-11 23:51:59 +08:00
										 |  |  | const { getUndoPath } = require("../util/identifier"); | 
					
						
							| 
									
										
										
										
											2025-08-11 19:53:57 +08:00
										 |  |  | const { | 
					
						
							|  |  |  | 	createChunkHashHandler, | 
					
						
							|  |  |  | 	getChunkInfo | 
					
						
							|  |  |  | } = require("./ChunkFormatHelpers"); | 
					
						
							| 
									
										
										
										
											2020-08-25 16:30:56 +08:00
										 |  |  | const { | 
					
						
							|  |  |  | 	getChunkFilenameTemplate, | 
					
						
							|  |  |  | 	getCompilationHooks | 
					
						
							|  |  |  | } = require("./JavascriptModulesPlugin"); | 
					
						
							| 
									
										
										
										
											2025-08-11 19:53:57 +08:00
										 |  |  | const { generateEntryStartup } = require("./StartupHelpers"); | 
					
						
							| 
									
										
										
										
											2020-08-25 16:30:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-13 01:24:59 +08:00
										 |  |  | /** @typedef {import("../Chunk")} Chunk */ | 
					
						
							| 
									
										
										
										
											2020-08-25 16:30:56 +08:00
										 |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-04 02:20:37 +08:00
										 |  |  | const PLUGIN_NAME = "CommonJsChunkFormatPlugin"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-25 16:30:56 +08:00
										 |  |  | class CommonJsChunkFormatPlugin { | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * 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.require); | 
					
						
							|  |  |  | 						set.add(RuntimeGlobals.startupEntrypoint); | 
					
						
							|  |  |  | 						set.add(RuntimeGlobals.externalInstallChunk); | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2025-06-04 02:20:37 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 			const hooks = getCompilationHooks(compilation); | 
					
						
							|  |  |  | 			hooks.renderChunk.tap(PLUGIN_NAME, (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) | 
					
						
							|  |  |  | 					); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2025-08-11 19:53:57 +08:00
										 |  |  | 				const { entries, runtimeChunk } = getChunkInfo(chunk, chunkGraph); | 
					
						
							|  |  |  | 				if (runtimeChunk) { | 
					
						
							| 
									
										
										
										
											2025-06-04 02:20:37 +08:00
										 |  |  | 					const currentOutputName = compilation | 
					
						
							|  |  |  | 						.getPath( | 
					
						
							|  |  |  | 							getChunkFilenameTemplate(chunk, compilation.outputOptions), | 
					
						
							|  |  |  | 							{ | 
					
						
							|  |  |  | 								chunk, | 
					
						
							|  |  |  | 								contentHashType: "javascript" | 
					
						
							| 
									
										
										
										
											2020-08-25 16:30:56 +08:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2025-06-04 02:20:37 +08:00
										 |  |  | 						) | 
					
						
							|  |  |  | 						.replace(/^\/+/g, "") | 
					
						
							|  |  |  | 						.split("/"); | 
					
						
							|  |  |  | 					const runtimeOutputName = compilation | 
					
						
							|  |  |  | 						.getPath( | 
					
						
							|  |  |  | 							getChunkFilenameTemplate( | 
					
						
							|  |  |  | 								/** @type {Chunk} */ | 
					
						
							|  |  |  | 								(runtimeChunk), | 
					
						
							|  |  |  | 								compilation.outputOptions | 
					
						
							|  |  |  | 							), | 
					
						
							|  |  |  | 							{ | 
					
						
							|  |  |  | 								chunk: /** @type {Chunk} */ (runtimeChunk), | 
					
						
							|  |  |  | 								contentHashType: "javascript" | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						) | 
					
						
							|  |  |  | 						.replace(/^\/+/g, "") | 
					
						
							|  |  |  | 						.split("/"); | 
					
						
							| 
									
										
										
										
											2020-08-25 16:30:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-04 02:20:37 +08:00
										 |  |  | 					// remove common parts
 | 
					
						
							|  |  |  | 					while ( | 
					
						
							|  |  |  | 						currentOutputName.length > 1 && | 
					
						
							|  |  |  | 						runtimeOutputName.length > 1 && | 
					
						
							|  |  |  | 						currentOutputName[0] === runtimeOutputName[0] | 
					
						
							|  |  |  | 					) { | 
					
						
							|  |  |  | 						currentOutputName.shift(); | 
					
						
							|  |  |  | 						runtimeOutputName.shift(); | 
					
						
							| 
									
										
										
										
											2020-08-25 16:30:56 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2025-06-04 02:20:37 +08:00
										 |  |  | 					const last = runtimeOutputName.join("/"); | 
					
						
							|  |  |  | 					// create final path
 | 
					
						
							|  |  |  | 					const runtimePath = | 
					
						
							|  |  |  | 						getUndoPath(currentOutputName.join("/"), last, true) + last; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					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 ${RuntimeGlobals.require} = require(${JSON.stringify( | 
					
						
							|  |  |  | 							runtimePath | 
					
						
							|  |  |  | 						)});\n`
 | 
					
						
							|  |  |  | 					); | 
					
						
							|  |  |  | 					entrySource.add(`${RuntimeGlobals.externalInstallChunk}(exports);\n`); | 
					
						
							|  |  |  | 					const startupSource = new RawSource( | 
					
						
							|  |  |  | 						generateEntryStartup( | 
					
						
							|  |  |  | 							chunkGraph, | 
					
						
							|  |  |  | 							runtimeTemplate, | 
					
						
							|  |  |  | 							entries, | 
					
						
							|  |  |  | 							chunk, | 
					
						
							|  |  |  | 							false | 
					
						
							|  |  |  | 						) | 
					
						
							|  |  |  | 					); | 
					
						
							|  |  |  | 					entrySource.add( | 
					
						
							|  |  |  | 						hooks.renderStartup.call( | 
					
						
							|  |  |  | 							startupSource, | 
					
						
							|  |  |  | 							entries[entries.length - 1][0], | 
					
						
							|  |  |  | 							{ | 
					
						
							|  |  |  | 								...renderContext, | 
					
						
							|  |  |  | 								inlined: false | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						) | 
					
						
							|  |  |  | 					); | 
					
						
							|  |  |  | 					entrySource.add("\n})()"); | 
					
						
							|  |  |  | 					return entrySource; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				return source; | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2025-08-11 19:53:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			hooks.chunkHash.tap(PLUGIN_NAME, createChunkHashHandler(PLUGIN_NAME)); | 
					
						
							| 
									
										
										
										
											2025-06-04 02:20:37 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2020-08-25 16:30:56 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = CommonJsChunkFormatPlugin; |