| 
									
										
										
										
											2014-06-03 14:45:26 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 18:35:09 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2014-06-03 14:45:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 18:12:20 +08:00
										 |  |  | const { ConcatSource, RawSource, CachedSource } = require("webpack-sources"); | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | const { UsageState } = require("./ExportsInfo"); | 
					
						
							| 
									
										
										
										
											2017-08-02 21:39:43 +08:00
										 |  |  | const Template = require("./Template"); | 
					
						
							| 
									
										
										
										
											2019-10-11 21:46:57 +08:00
										 |  |  | const JavascriptModulesPlugin = require("./javascript/JavascriptModulesPlugin"); | 
					
						
							| 
									
										
										
										
											2014-06-03 14:45:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 18:12:20 +08:00
										 |  |  | /** @typedef {import("webpack-sources").Source} Source */ | 
					
						
							| 
									
										
										
										
											2019-10-02 14:54:21 +08:00
										 |  |  | /** @typedef {import("./Compiler")} Compiler */ | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | /** @typedef {import("./ExportsInfo")} ExportsInfo */ | 
					
						
							|  |  |  | /** @typedef {import("./ExportsInfo").ExportInfo} ExportInfo */ | 
					
						
							| 
									
										
										
										
											2020-12-04 18:12:20 +08:00
										 |  |  | /** @typedef {import("./Module")} Module */ | 
					
						
							| 
									
										
										
										
											2020-08-13 00:05:58 +08:00
										 |  |  | /** @typedef {import("./ModuleGraph")} ModuleGraph */ | 
					
						
							| 
									
										
										
										
											2018-08-07 01:39:43 +08:00
										 |  |  | /** @typedef {import("./ModuleTemplate")} ModuleTemplate */ | 
					
						
							| 
									
										
										
										
											2020-08-12 00:06:27 +08:00
										 |  |  | /** @typedef {import("./RequestShortener")} RequestShortener */ | 
					
						
							| 
									
										
										
										
											2018-08-07 01:39:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-05 21:23:59 +08:00
										 |  |  | const joinIterableWithComma = iterable => { | 
					
						
							|  |  |  | 	// This is more performant than Array.from().join(", ")
 | 
					
						
							|  |  |  | 	// as it doesn't create an array
 | 
					
						
							|  |  |  | 	let str = ""; | 
					
						
							|  |  |  | 	let first = true; | 
					
						
							|  |  |  | 	for (const item of iterable) { | 
					
						
							|  |  |  | 		if (first) { | 
					
						
							|  |  |  | 			first = false; | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			str += ", "; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-05-21 15:44:06 +08:00
										 |  |  | 		str += item; | 
					
						
							| 
									
										
										
										
											2019-01-05 21:23:59 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return str; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param {ConcatSource} source output | 
					
						
							|  |  |  |  * @param {string} indent spacing | 
					
						
							|  |  |  |  * @param {ExportsInfo} exportsInfo data | 
					
						
							| 
									
										
										
										
											2020-08-13 00:05:58 +08:00
										 |  |  |  * @param {ModuleGraph} moduleGraph moduleGraph | 
					
						
							| 
									
										
										
										
											2020-08-12 00:06:27 +08:00
										 |  |  |  * @param {RequestShortener} requestShortener requestShortener | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  |  * @param {Set<ExportInfo>} alreadyPrinted deduplication set | 
					
						
							|  |  |  |  * @returns {void} | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-02-21 18:15:20 +08:00
										 |  |  | const printExportsInfoToSource = ( | 
					
						
							|  |  |  | 	source, | 
					
						
							|  |  |  | 	indent, | 
					
						
							|  |  |  | 	exportsInfo, | 
					
						
							| 
									
										
										
										
											2020-08-13 00:05:58 +08:00
										 |  |  | 	moduleGraph, | 
					
						
							| 
									
										
										
										
											2020-08-12 00:06:27 +08:00
										 |  |  | 	requestShortener, | 
					
						
							| 
									
										
										
										
											2020-02-21 18:15:20 +08:00
										 |  |  | 	alreadyPrinted = new Set() | 
					
						
							|  |  |  | ) => { | 
					
						
							|  |  |  | 	const otherExportsInfo = exportsInfo.otherExportsInfo; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	let alreadyPrintedExports = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// determine exports to print
 | 
					
						
							|  |  |  | 	const printedExports = []; | 
					
						
							| 
									
										
										
										
											2019-03-14 19:06:59 +08:00
										 |  |  | 	for (const exportInfo of exportsInfo.orderedExports) { | 
					
						
							| 
									
										
										
										
											2020-02-21 18:15:20 +08:00
										 |  |  | 		if (!alreadyPrinted.has(exportInfo)) { | 
					
						
							|  |  |  | 			alreadyPrinted.add(exportInfo); | 
					
						
							|  |  |  | 			printedExports.push(exportInfo); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			alreadyPrintedExports++; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	let showOtherExports = false; | 
					
						
							|  |  |  | 	if (!alreadyPrinted.has(otherExportsInfo)) { | 
					
						
							|  |  |  | 		alreadyPrinted.add(otherExportsInfo); | 
					
						
							|  |  |  | 		showOtherExports = true; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		alreadyPrintedExports++; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// print the exports
 | 
					
						
							|  |  |  | 	for (const exportInfo of printedExports) { | 
					
						
							| 
									
										
										
										
											2020-08-13 00:05:58 +08:00
										 |  |  | 		const target = exportInfo.getTarget(moduleGraph); | 
					
						
							| 
									
										
										
										
											2019-03-14 19:06:59 +08:00
										 |  |  | 		source.add( | 
					
						
							|  |  |  | 			Template.toComment( | 
					
						
							| 
									
										
										
										
											2019-12-06 14:10:25 +08:00
										 |  |  | 				`${indent}export ${JSON.stringify(exportInfo.name).slice( | 
					
						
							|  |  |  | 					1, | 
					
						
							|  |  |  | 					-1 | 
					
						
							| 
									
										
										
										
											2020-08-12 00:06:27 +08:00
										 |  |  | 				)} [${exportInfo.getProvidedInfo()}] [${exportInfo.getUsedInfo()}] [${exportInfo.getRenameInfo()}]${ | 
					
						
							| 
									
										
										
										
											2020-08-13 00:05:58 +08:00
										 |  |  | 					target | 
					
						
							|  |  |  | 						? ` -> ${target.module.readableIdentifier(requestShortener)}${ | 
					
						
							|  |  |  | 								target.export | 
					
						
							|  |  |  | 									? ` .${target.export | 
					
						
							| 
									
										
										
										
											2020-08-12 00:06:27 +08:00
										 |  |  | 											.map(e => JSON.stringify(e).slice(1, -1)) | 
					
						
							|  |  |  | 											.join(".")}`
 | 
					
						
							|  |  |  | 									: "" | 
					
						
							|  |  |  | 						  }`
 | 
					
						
							|  |  |  | 						: "" | 
					
						
							|  |  |  | 				}`
 | 
					
						
							| 
									
										
										
										
											2019-03-14 19:06:59 +08:00
										 |  |  | 			) + "\n" | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 		if (exportInfo.exportsInfo) { | 
					
						
							| 
									
										
										
										
											2020-02-21 18:15:20 +08:00
										 |  |  | 			printExportsInfoToSource( | 
					
						
							|  |  |  | 				source, | 
					
						
							|  |  |  | 				indent + "  ", | 
					
						
							|  |  |  | 				exportInfo.exportsInfo, | 
					
						
							| 
									
										
										
										
											2020-08-13 00:05:58 +08:00
										 |  |  | 				moduleGraph, | 
					
						
							| 
									
										
										
										
											2020-08-12 00:06:27 +08:00
										 |  |  | 				requestShortener, | 
					
						
							| 
									
										
										
										
											2020-02-21 18:15:20 +08:00
										 |  |  | 				alreadyPrinted | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2019-03-14 19:06:59 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-05 04:05:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-21 18:15:20 +08:00
										 |  |  | 	if (alreadyPrintedExports) { | 
					
						
							| 
									
										
										
										
											2019-03-14 19:06:59 +08:00
										 |  |  | 		source.add( | 
					
						
							|  |  |  | 			Template.toComment( | 
					
						
							| 
									
										
										
										
											2020-02-21 18:15:20 +08:00
										 |  |  | 				`${indent}... (${alreadyPrintedExports} already listed exports)` | 
					
						
							| 
									
										
										
										
											2019-11-05 04:05:17 +08:00
										 |  |  | 			) + "\n" | 
					
						
							| 
									
										
										
										
											2019-03-14 19:06:59 +08:00
										 |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-02-21 18:15:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (showOtherExports) { | 
					
						
							| 
									
										
										
										
											2020-08-13 00:05:58 +08:00
										 |  |  | 		const target = otherExportsInfo.getTarget(moduleGraph); | 
					
						
							| 
									
										
										
										
											2020-02-21 18:15:20 +08:00
										 |  |  | 		if ( | 
					
						
							| 
									
										
										
										
											2020-08-13 00:05:58 +08:00
										 |  |  | 			target || | 
					
						
							| 
									
										
										
										
											2020-02-21 18:15:20 +08:00
										 |  |  | 			otherExportsInfo.provided !== false || | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | 			otherExportsInfo.getUsed(undefined) !== UsageState.Unused | 
					
						
							| 
									
										
										
										
											2020-02-21 18:15:20 +08:00
										 |  |  | 		) { | 
					
						
							|  |  |  | 			const title = | 
					
						
							|  |  |  | 				printedExports.length > 0 || alreadyPrintedExports > 0 | 
					
						
							|  |  |  | 					? "other exports" | 
					
						
							|  |  |  | 					: "exports"; | 
					
						
							|  |  |  | 			source.add( | 
					
						
							|  |  |  | 				Template.toComment( | 
					
						
							| 
									
										
										
										
											2020-08-13 00:05:58 +08:00
										 |  |  | 					`${indent}${title} [${otherExportsInfo.getProvidedInfo()}] [${otherExportsInfo.getUsedInfo()}]${ | 
					
						
							|  |  |  | 						target | 
					
						
							|  |  |  | 							? ` -> ${target.module.readableIdentifier(requestShortener)}` | 
					
						
							|  |  |  | 							: "" | 
					
						
							|  |  |  | 					}`
 | 
					
						
							| 
									
										
										
										
											2020-02-21 18:15:20 +08:00
										 |  |  | 				) + "\n" | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-03-14 19:06:59 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-04 18:12:20 +08:00
										 |  |  | /** @type {WeakMap<RequestShortener, WeakMap<Module, { header: RawSource, full: WeakMap<Source, CachedSource> }>>} */ | 
					
						
							|  |  |  | const caches = new WeakMap(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 14:54:21 +08:00
										 |  |  | class ModuleInfoHeaderPlugin { | 
					
						
							| 
									
										
										
										
											2020-12-04 18:12:20 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {boolean=} verbose add more information like exports, runtime requirements and bailouts | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	constructor(verbose = true) { | 
					
						
							|  |  |  | 		this._verbose = verbose; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-08-07 01:39:43 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2019-10-02 14:54:21 +08:00
										 |  |  | 	 * @param {Compiler} compiler the compiler | 
					
						
							| 
									
										
										
										
											2018-08-07 01:39:43 +08:00
										 |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2019-10-02 14:54:21 +08:00
										 |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2020-12-04 18:12:20 +08:00
										 |  |  | 		const { _verbose: verbose } = this; | 
					
						
							| 
									
										
										
										
											2019-10-02 14:54:21 +08:00
										 |  |  | 		compiler.hooks.compilation.tap("ModuleInfoHeaderPlugin", compilation => { | 
					
						
							|  |  |  | 			const hooks = JavascriptModulesPlugin.getCompilationHooks(compilation); | 
					
						
							|  |  |  | 			hooks.renderModulePackage.tap( | 
					
						
							|  |  |  | 				"ModuleInfoHeaderPlugin", | 
					
						
							|  |  |  | 				( | 
					
						
							|  |  |  | 					moduleSource, | 
					
						
							|  |  |  | 					module, | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | 					{ chunk, chunkGraph, moduleGraph, runtimeTemplate } | 
					
						
							| 
									
										
										
										
											2019-10-02 14:54:21 +08:00
										 |  |  | 				) => { | 
					
						
							| 
									
										
										
										
											2020-08-13 00:05:58 +08:00
										 |  |  | 					const { requestShortener } = runtimeTemplate; | 
					
						
							| 
									
										
										
										
											2020-12-04 18:12:20 +08:00
										 |  |  | 					let cacheEntry; | 
					
						
							|  |  |  | 					let cache = caches.get(requestShortener); | 
					
						
							|  |  |  | 					if (cache === undefined) { | 
					
						
							|  |  |  | 						caches.set(requestShortener, (cache = new WeakMap())); | 
					
						
							|  |  |  | 						cache.set( | 
					
						
							|  |  |  | 							module, | 
					
						
							|  |  |  | 							(cacheEntry = { header: undefined, full: new WeakMap() }) | 
					
						
							| 
									
										
										
										
											2020-08-13 00:05:58 +08:00
										 |  |  | 						); | 
					
						
							| 
									
										
										
										
											2020-12-04 18:12:20 +08:00
										 |  |  | 					} else { | 
					
						
							|  |  |  | 						cacheEntry = cache.get(module); | 
					
						
							|  |  |  | 						if (cacheEntry === undefined) { | 
					
						
							|  |  |  | 							cache.set( | 
					
						
							|  |  |  | 								module, | 
					
						
							|  |  |  | 								(cacheEntry = { header: undefined, full: new WeakMap() }) | 
					
						
							|  |  |  | 							); | 
					
						
							|  |  |  | 						} else if (!verbose) { | 
					
						
							|  |  |  | 							const cachedSource = cacheEntry.full.get(moduleSource); | 
					
						
							|  |  |  | 							if (cachedSource !== undefined) return cachedSource; | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2020-07-08 16:58:20 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2020-12-04 18:12:20 +08:00
										 |  |  | 					const source = new ConcatSource(); | 
					
						
							|  |  |  | 					let header = cacheEntry.header; | 
					
						
							|  |  |  | 					if (header === undefined) { | 
					
						
							|  |  |  | 						const req = module.readableIdentifier(requestShortener); | 
					
						
							|  |  |  | 						const reqStr = req.replace(/\*\//g, "*_/"); | 
					
						
							|  |  |  | 						const reqStrStar = "*".repeat(reqStr.length); | 
					
						
							| 
									
										
										
										
											2020-12-05 04:06:21 +08:00
										 |  |  | 						const headerStr = `/*!****${reqStrStar}****!*\\\n  !*** ${reqStr} ***!\n  \\****${reqStrStar}****/\n`; | 
					
						
							| 
									
										
										
										
											2020-12-04 18:12:20 +08:00
										 |  |  | 						header = new RawSource(headerStr); | 
					
						
							|  |  |  | 						cacheEntry.header = header; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					source.add(header); | 
					
						
							|  |  |  | 					if (verbose) { | 
					
						
							|  |  |  | 						const exportsType = module.buildMeta.exportsType; | 
					
						
							|  |  |  | 						source.add( | 
					
						
							|  |  |  | 							Template.toComment( | 
					
						
							|  |  |  | 								exportsType | 
					
						
							|  |  |  | 									? `${exportsType} exports` | 
					
						
							|  |  |  | 									: "unknown exports (runtime-defined)" | 
					
						
							|  |  |  | 							) + "\n" | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 						if (exportsType) { | 
					
						
							|  |  |  | 							const exportsInfo = moduleGraph.getExportsInfo(module); | 
					
						
							|  |  |  | 							printExportsInfoToSource( | 
					
						
							|  |  |  | 								source, | 
					
						
							|  |  |  | 								"", | 
					
						
							|  |  |  | 								exportsInfo, | 
					
						
							|  |  |  | 								moduleGraph, | 
					
						
							|  |  |  | 								requestShortener | 
					
						
							|  |  |  | 							); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						source.add( | 
					
						
							|  |  |  | 							Template.toComment( | 
					
						
							|  |  |  | 								`runtime requirements: ${joinIterableWithComma( | 
					
						
							|  |  |  | 									chunkGraph.getModuleRuntimeRequirements(module, chunk.runtime) | 
					
						
							|  |  |  | 								)}`
 | 
					
						
							|  |  |  | 							) + "\n" | 
					
						
							|  |  |  | 						); | 
					
						
							| 
									
										
										
										
											2021-05-11 15:31:46 +08:00
										 |  |  | 						const optimizationBailout = | 
					
						
							|  |  |  | 							moduleGraph.getOptimizationBailout(module); | 
					
						
							| 
									
										
										
										
											2020-12-04 18:12:20 +08:00
										 |  |  | 						if (optimizationBailout) { | 
					
						
							|  |  |  | 							for (const text of optimizationBailout) { | 
					
						
							|  |  |  | 								let code; | 
					
						
							|  |  |  | 								if (typeof text === "function") { | 
					
						
							|  |  |  | 									code = text(requestShortener); | 
					
						
							|  |  |  | 								} else { | 
					
						
							|  |  |  | 									code = text; | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 								source.add(Template.toComment(`${code}`) + "\n"); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2018-01-22 20:52:43 +08:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2020-12-04 18:12:20 +08:00
										 |  |  | 						source.add(moduleSource); | 
					
						
							|  |  |  | 						return source; | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						source.add(moduleSource); | 
					
						
							|  |  |  | 						const cachedSource = new CachedSource(source); | 
					
						
							|  |  |  | 						cacheEntry.full.set(moduleSource, cachedSource); | 
					
						
							|  |  |  | 						return cachedSource; | 
					
						
							| 
									
										
										
										
											2018-01-22 20:52:43 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2017-05-28 21:25:07 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2019-10-02 14:54:21 +08:00
										 |  |  | 			); | 
					
						
							|  |  |  | 			hooks.chunkHash.tap("ModuleInfoHeaderPlugin", (chunk, hash) => { | 
					
						
							|  |  |  | 				hash.update("ModuleInfoHeaderPlugin"); | 
					
						
							|  |  |  | 				hash.update("1"); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-02-21 18:35:09 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-02 14:54:21 +08:00
										 |  |  | module.exports = ModuleInfoHeaderPlugin; |