| 
									
										
										
										
											2017-06-02 20:52:41 +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-06-02 20:52:41 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 19:05:58 +08:00
										 |  |  | const { ConcatSource } = require("webpack-sources"); | 
					
						
							| 
									
										
										
										
											2019-10-09 04:29:46 +08:00
										 |  |  | const { UsageState } = require("./ModuleGraph"); | 
					
						
							| 
									
										
										
										
											2019-10-11 21:46:57 +08:00
										 |  |  | const JavascriptModulesPlugin = require("./javascript/JavascriptModulesPlugin"); | 
					
						
							| 
									
										
										
										
											2017-06-02 20:52:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 22:59:27 +08:00
										 |  |  | /** @typedef {import("./Compiler")} Compiler */ | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @param {string[]} accessor the accessor to convert to path | 
					
						
							|  |  |  |  * @returns {string} the path | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | const accessorToObjectAccess = accessor => { | 
					
						
							| 
									
										
										
										
											2017-06-02 20:52:41 +08:00
										 |  |  | 	return accessor.map(a => `[${JSON.stringify(a)}]`).join(""); | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2017-06-02 20:52:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 22:59:27 +08:00
										 |  |  | class ExportPropertyTemplatePlugin { | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {string|string[]} property the name of the property to export | 
					
						
							| 
									
										
										
										
											2019-10-09 04:29:46 +08:00
										 |  |  | 	 * @param {string} explanation an explanation why this property is used | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2019-10-09 04:29:46 +08:00
										 |  |  | 	constructor(property, explanation) { | 
					
						
							| 
									
										
										
										
											2017-06-02 20:52:41 +08:00
										 |  |  | 		this.property = property; | 
					
						
							| 
									
										
										
										
											2019-10-09 04:29:46 +08:00
										 |  |  | 		this.explanation = explanation; | 
					
						
							| 
									
										
										
										
											2017-06-02 20:52:41 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2019-10-02 22:59:27 +08:00
										 |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2019-10-02 22:59:27 +08:00
										 |  |  | 	apply(compiler) { | 
					
						
							|  |  |  | 		compiler.hooks.thisCompilation.tap( | 
					
						
							|  |  |  | 			"ExportPropertyTemplatePlugin", | 
					
						
							|  |  |  | 			compilation => { | 
					
						
							| 
									
										
										
										
											2019-10-09 04:29:46 +08:00
										 |  |  | 				const moduleGraph = compilation.moduleGraph; | 
					
						
							|  |  |  | 				compilation.hooks.seal.tap("ExportPropertyTemplatePlugin", () => { | 
					
						
							|  |  |  | 					for (const deps of compilation.entryDependencies.values()) { | 
					
						
							|  |  |  | 						const dep = deps[deps.length - 1]; | 
					
						
							|  |  |  | 						if (dep) { | 
					
						
							|  |  |  | 							const module = moduleGraph.getModule(dep); | 
					
						
							|  |  |  | 							if (module) { | 
					
						
							|  |  |  | 								const exportsInfo = moduleGraph.getExportInfo( | 
					
						
							|  |  |  | 									module, | 
					
						
							|  |  |  | 									Array.isArray(this.property) | 
					
						
							|  |  |  | 										? this.property[0] | 
					
						
							|  |  |  | 										: this.property | 
					
						
							|  |  |  | 								); | 
					
						
							|  |  |  | 								exportsInfo.used = UsageState.Used; | 
					
						
							|  |  |  | 								exportsInfo.canMangleUse = false; | 
					
						
							|  |  |  | 								moduleGraph.addExtraReason(module, this.explanation); | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 22:59:27 +08:00
										 |  |  | 				const hooks = JavascriptModulesPlugin.getCompilationHooks(compilation); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-09 17:55:56 +08:00
										 |  |  | 				hooks.render.tap( | 
					
						
							| 
									
										
										
										
											2019-10-02 22:59:27 +08:00
										 |  |  | 					"ExportPropertyTemplatePlugin", | 
					
						
							| 
									
										
										
										
											2019-10-09 17:55:56 +08:00
										 |  |  | 					(source, { chunk, chunkGraph }) => { | 
					
						
							|  |  |  | 						if (chunkGraph.getNumberOfEntryModules(chunk) === 0) return source; | 
					
						
							| 
									
										
										
										
											2019-10-02 22:59:27 +08:00
										 |  |  | 						const postfix = accessorToObjectAccess([].concat(this.property)); | 
					
						
							|  |  |  | 						return new ConcatSource(source, postfix); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-09 17:55:56 +08:00
										 |  |  | 				hooks.chunkHash.tap( | 
					
						
							|  |  |  | 					"ExportPropertyTemplatePlugin", | 
					
						
							|  |  |  | 					(chunk, hash, { chunkGraph }) => { | 
					
						
							|  |  |  | 						if (chunkGraph.getNumberOfEntryModules(chunk) === 0) return; | 
					
						
							|  |  |  | 						hash.update("export property"); | 
					
						
							|  |  |  | 						hash.update(`${this.property}`); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				); | 
					
						
							| 
									
										
										
										
											2019-10-02 22:59:27 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-11-02 22:39:51 +08:00
										 |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-06-02 20:52:41 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 22:59:27 +08:00
										 |  |  | module.exports = ExportPropertyTemplatePlugin; |