| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 19:05:58 +08:00
										 |  |  | const { ConcatSource } = require("webpack-sources"); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 14:54:21 +08:00
										 |  |  | /** @typedef {import("./Compiler")} Compiler */ | 
					
						
							| 
									
										
										
										
											2018-08-07 01:39:43 +08:00
										 |  |  | /** @typedef {import("./ModuleTemplate")} ModuleTemplate */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-02-21 18:15:20 +08:00
										 |  |  | const printExportsInfoToSource = ( | 
					
						
							|  |  |  | 	source, | 
					
						
							|  |  |  | 	indent, | 
					
						
							|  |  |  | 	exportsInfo, | 
					
						
							|  |  |  | 	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) { | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 				)} [${exportInfo.getProvidedInfo()}] [${exportInfo.getUsedInfo()}] [${exportInfo.getRenameInfo()}]`
 | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							|  |  |  | 				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) { | 
					
						
							|  |  |  | 		if ( | 
					
						
							|  |  |  | 			otherExportsInfo.provided !== false || | 
					
						
							|  |  |  | 			otherExportsInfo.used !== false | 
					
						
							|  |  |  | 		) { | 
					
						
							|  |  |  | 			const title = | 
					
						
							|  |  |  | 				printedExports.length > 0 || alreadyPrintedExports > 0 | 
					
						
							|  |  |  | 					? "other exports" | 
					
						
							|  |  |  | 					: "exports"; | 
					
						
							|  |  |  | 			source.add( | 
					
						
							|  |  |  | 				Template.toComment( | 
					
						
							|  |  |  | 					`${indent}${title} [${otherExportsInfo.getProvidedInfo()}] [${otherExportsInfo.getUsedInfo()}]` | 
					
						
							|  |  |  | 				) + "\n" | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-03-14 19:06:59 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 14:54:21 +08:00
										 |  |  | class ModuleInfoHeaderPlugin { | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							|  |  |  | 		compiler.hooks.compilation.tap("ModuleInfoHeaderPlugin", compilation => { | 
					
						
							|  |  |  | 			const hooks = JavascriptModulesPlugin.getCompilationHooks(compilation); | 
					
						
							|  |  |  | 			hooks.renderModulePackage.tap( | 
					
						
							|  |  |  | 				"ModuleInfoHeaderPlugin", | 
					
						
							|  |  |  | 				( | 
					
						
							|  |  |  | 					moduleSource, | 
					
						
							|  |  |  | 					module, | 
					
						
							|  |  |  | 					{ chunkGraph, moduleGraph, runtimeTemplate } | 
					
						
							|  |  |  | 				) => { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					const source = new ConcatSource(); | 
					
						
							|  |  |  | 					const req = module.readableIdentifier( | 
					
						
							| 
									
										
										
										
											2019-10-02 14:54:21 +08:00
										 |  |  | 						runtimeTemplate.requestShortener | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					); | 
					
						
							| 
									
										
										
										
											2019-01-05 21:23:59 +08:00
										 |  |  | 					const reqStr = req.replace(/\*\//g, "*_/"); | 
					
						
							|  |  |  | 					const reqStrStar = "*".repeat(reqStr.length); | 
					
						
							|  |  |  | 					source.add("/*!****" + reqStrStar + "****!*\\\n"); | 
					
						
							|  |  |  | 					source.add("  !*** " + reqStr + " ***!\n"); | 
					
						
							|  |  |  | 					source.add("  \\****" + reqStrStar + "****/\n"); | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 					const exportsType = module.buildMeta.exportsType; | 
					
						
							|  |  |  | 					source.add( | 
					
						
							|  |  |  | 						Template.toComment( | 
					
						
							|  |  |  | 							exportsType | 
					
						
							| 
									
										
										
										
											2019-12-06 18:32:07 +08:00
										 |  |  | 								? `${exportsType} exports` | 
					
						
							|  |  |  | 								: "unknown exports (runtime-defined)" | 
					
						
							| 
									
										
										
										
											2019-12-05 05:54:26 +08:00
										 |  |  | 						) + "\n" | 
					
						
							|  |  |  | 					); | 
					
						
							| 
									
										
										
										
											2019-01-23 17:05:32 +08:00
										 |  |  | 					const exportsInfo = moduleGraph.getExportsInfo(module); | 
					
						
							| 
									
										
										
										
											2019-03-14 19:06:59 +08:00
										 |  |  | 					printExportsInfoToSource(source, "", exportsInfo); | 
					
						
							| 
									
										
										
										
											2018-11-17 01:18:44 +08:00
										 |  |  | 					source.add( | 
					
						
							|  |  |  | 						Template.toComment( | 
					
						
							| 
									
										
										
										
											2019-01-05 21:23:59 +08:00
										 |  |  | 							`runtime requirements: ${joinIterableWithComma( | 
					
						
							| 
									
										
										
										
											2018-11-17 01:18:44 +08:00
										 |  |  | 								chunkGraph.getModuleRuntimeRequirements(module) | 
					
						
							| 
									
										
										
										
											2019-01-05 21:23:59 +08:00
										 |  |  | 							)}`
 | 
					
						
							| 
									
										
										
										
											2018-11-17 01:18:44 +08:00
										 |  |  | 						) + "\n" | 
					
						
							|  |  |  | 					); | 
					
						
							| 
									
										
										
										
											2018-11-17 01:11:51 +08:00
										 |  |  | 					const optimizationBailout = moduleGraph.getOptimizationBailout( | 
					
						
							| 
									
										
										
										
											2018-08-16 19:55:41 +08:00
										 |  |  | 						module | 
					
						
							| 
									
										
										
										
											2018-08-06 16:42:41 +08:00
										 |  |  | 					); | 
					
						
							|  |  |  | 					if (optimizationBailout) { | 
					
						
							|  |  |  | 						for (const text of optimizationBailout) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							let code; | 
					
						
							|  |  |  | 							if (typeof text === "function") { | 
					
						
							| 
									
										
										
										
											2019-10-02 14:54:21 +08:00
										 |  |  | 								code = text(runtimeTemplate.requestShortener); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							} else { | 
					
						
							|  |  |  | 								code = text; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 							source.add(Template.toComment(`${code}`) + "\n"); | 
					
						
							| 
									
										
										
										
											2018-01-22 20:52:43 +08:00
										 |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					source.add(moduleSource); | 
					
						
							|  |  |  | 					return source; | 
					
						
							| 
									
										
										
										
											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; |