| 
									
										
										
										
											2014-06-03 03:23:53 +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-01-05 02:00:09 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 19:05:58 +08:00
										 |  |  | const { ConcatSource } = require("webpack-sources"); | 
					
						
							| 
									
										
										
										
											2017-01-05 02:00:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | /** @typedef {import("./Compilation")} Compilation */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-05 02:00:09 +08:00
										 |  |  | class SetVarMainTemplatePlugin { | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {string} varExpression the accessor where the library is exported | 
					
						
							|  |  |  | 	 * @param {boolean} copyObject specify copying the exports | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-01-05 02:00:09 +08:00
										 |  |  | 	constructor(varExpression, copyObject) { | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 		/** @type {string} */ | 
					
						
							| 
									
										
										
										
											2017-01-05 02:00:09 +08:00
										 |  |  | 		this.varExpression = varExpression; | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 		/** @type {boolean} */ | 
					
						
							| 
									
										
										
										
											2017-01-05 02:00:09 +08:00
										 |  |  | 		this.copyObject = copyObject; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Compilation} compilation the compilation instance | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-01-05 02:00:09 +08:00
										 |  |  | 	apply(compilation) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		const { mainTemplate, chunkTemplate } = compilation; | 
					
						
							| 
									
										
										
										
											2017-12-16 08:14:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		const onRenderWithEntry = (source, chunk, hash) => { | 
					
						
							| 
									
										
										
										
											2017-11-29 01:43:01 +08:00
										 |  |  | 			const varExpression = mainTemplate.getAssetPath(this.varExpression, { | 
					
						
							| 
									
										
										
										
											2017-01-05 02:00:09 +08:00
										 |  |  | 				hash, | 
					
						
							|  |  |  | 				chunk | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			if (this.copyObject) { | 
					
						
							|  |  |  | 				return new ConcatSource( | 
					
						
							|  |  |  | 					`(function(e, a) { for(var i in a) e[i] = a[i]; }(${varExpression}, `, | 
					
						
							|  |  |  | 					source, | 
					
						
							|  |  |  | 					"))" | 
					
						
							|  |  |  | 				); | 
					
						
							| 
									
										
										
										
											2017-01-05 02:00:09 +08:00
										 |  |  | 			} else { | 
					
						
							|  |  |  | 				const prefix = `${varExpression} =\n`; | 
					
						
							|  |  |  | 				return new ConcatSource(prefix, source); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-12-16 08:14:19 +08:00
										 |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2017-12-16 09:07:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		for (const template of [mainTemplate, chunkTemplate]) { | 
					
						
							|  |  |  | 			template.hooks.renderWithEntry.tap( | 
					
						
							|  |  |  | 				"SetVarMainTemplatePlugin", | 
					
						
							|  |  |  | 				onRenderWithEntry | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2017-12-16 09:07:32 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		mainTemplate.hooks.globalHashPaths.tap( | 
					
						
							|  |  |  | 			"SetVarMainTemplatePlugin", | 
					
						
							|  |  |  | 			paths => { | 
					
						
							|  |  |  | 				if (this.varExpression) paths.push(this.varExpression); | 
					
						
							|  |  |  | 				return paths; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-12-08 18:05:45 +08:00
										 |  |  | 		mainTemplate.hooks.hash.tap("SetVarMainTemplatePlugin", hash => { | 
					
						
							| 
									
										
										
										
											2017-01-05 02:00:09 +08:00
										 |  |  | 			hash.update("set var"); | 
					
						
							|  |  |  | 			hash.update(`${this.varExpression}`); | 
					
						
							|  |  |  | 			hash.update(`${this.copyObject}`); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-01-05 02:00:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | module.exports = SetVarMainTemplatePlugin; |