| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2017-01-06 23:17:22 +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-06 23:17:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class JsonpExportMainTemplatePlugin { | 
					
						
							| 
									
										
										
										
											2018-09-19 19:05:22 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {string} name jsonp function name | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-01-06 23:17:22 +08:00
										 |  |  | 	constructor(name) { | 
					
						
							|  |  |  | 		this.name = name; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	apply(compilation) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		const { mainTemplate, chunkTemplate } = compilation; | 
					
						
							| 
									
										
										
										
											2017-01-06 23:17:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 08:14:19 +08:00
										 |  |  | 		const onRenderWithEntry = (source, chunk, hash) => { | 
					
						
							| 
									
										
										
										
											2017-11-29 01:43:01 +08:00
										 |  |  | 			const name = mainTemplate.getAssetPath(this.name || "", { | 
					
						
							| 
									
										
										
										
											2017-12-08 18:05:45 +08:00
										 |  |  | 				hash, | 
					
						
							|  |  |  | 				chunk | 
					
						
							| 
									
										
										
										
											2017-01-06 23:17:22 +08:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 			return new ConcatSource(`${name}(`, source, ");"); | 
					
						
							| 
									
										
										
										
											2017-12-16 08:14:19 +08:00
										 |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2017-01-06 23:17:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		for (const template of [mainTemplate, chunkTemplate]) { | 
					
						
							|  |  |  | 			template.hooks.renderWithEntry.tap( | 
					
						
							|  |  |  | 				"JsonpExportMainTemplatePlugin", | 
					
						
							|  |  |  | 				onRenderWithEntry | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2017-12-16 09:07:32 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		mainTemplate.hooks.globalHashPaths.tap( | 
					
						
							|  |  |  | 			"JsonpExportMainTemplatePlugin", | 
					
						
							|  |  |  | 			paths => { | 
					
						
							|  |  |  | 				if (this.name) paths.push(this.name); | 
					
						
							|  |  |  | 				return paths; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-01-06 23:17:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 18:05:45 +08:00
										 |  |  | 		mainTemplate.hooks.hash.tap("JsonpExportMainTemplatePlugin", hash => { | 
					
						
							| 
									
										
										
										
											2017-01-06 23:17:22 +08:00
										 |  |  | 			hash.update("jsonp export"); | 
					
						
							|  |  |  | 			hash.update(`${this.name}`); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-01-06 23:17:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | module.exports = JsonpExportMainTemplatePlugin; |