| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2016-12-29 11:42:17 +08:00
										 |  |  |  MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  |  Author Tobias Koppers @sokra | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-29 11:42:17 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-22 19:05:58 +08:00
										 |  |  | const { ConcatSource } = require("webpack-sources"); | 
					
						
							| 
									
										
										
										
											2017-02-20 16:17:51 +08:00
										 |  |  | const Template = require("./Template"); | 
					
						
							| 
									
										
										
										
											2016-12-29 11:42:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | /** @typedef {import("./Compilation")} Compilation */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-29 11:42:17 +08:00
										 |  |  | class AmdMainTemplatePlugin { | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2018-09-19 19:05:22 +08:00
										 |  |  | 	 * @param {string=} name the library name | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2016-12-29 11:42:17 +08:00
										 |  |  | 	constructor(name) { | 
					
						
							| 
									
										
										
										
											2018-09-19 19:05:22 +08:00
										 |  |  | 		/** @type {string=} */ | 
					
						
							| 
									
										
										
										
											2016-12-29 11:42:17 +08:00
										 |  |  | 		this.name = name; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Compilation} compilation the compilation instance | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2016-12-29 11:42:17 +08:00
										 |  |  | 	apply(compilation) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		const { mainTemplate, chunkTemplate } = compilation; | 
					
						
							| 
									
										
										
										
											2016-12-29 11:42:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 08:14:19 +08:00
										 |  |  | 		const onRenderWithEntry = (source, chunk, hash) => { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			const externals = chunk.getModules().filter(m => m.external); | 
					
						
							|  |  |  | 			const externalsDepsArray = JSON.stringify( | 
					
						
							|  |  |  | 				externals.map( | 
					
						
							|  |  |  | 					m => (typeof m.request === "object" ? m.request.amd : m.request) | 
					
						
							|  |  |  | 				) | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 			const externalsArguments = externals | 
					
						
							|  |  |  | 				.map( | 
					
						
							|  |  |  | 					m => `__WEBPACK_EXTERNAL_MODULE_${Template.toIdentifier(`${m.id}`)}__` | 
					
						
							|  |  |  | 				) | 
					
						
							|  |  |  | 				.join(", "); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (this.name) { | 
					
						
							| 
									
										
										
										
											2017-11-29 01:43:01 +08:00
										 |  |  | 				const name = mainTemplate.getAssetPath(this.name, { | 
					
						
							| 
									
										
										
										
											2016-12-29 11:42:17 +08:00
										 |  |  | 					hash, | 
					
						
							|  |  |  | 					chunk | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				return new ConcatSource( | 
					
						
							| 
									
										
										
										
											2018-03-26 22:56:10 +08:00
										 |  |  | 					`define(${JSON.stringify( | 
					
						
							|  |  |  | 						name | 
					
						
							|  |  |  | 					)}, ${externalsDepsArray}, function(${externalsArguments}) { return `,
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					source, | 
					
						
							|  |  |  | 					"});" | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 			} else if (externalsArguments) { | 
					
						
							|  |  |  | 				return new ConcatSource( | 
					
						
							| 
									
										
										
										
											2018-03-26 22:56:10 +08:00
										 |  |  | 					`define(${externalsDepsArray}, function(${externalsArguments}) { return `, | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					source, | 
					
						
							|  |  |  | 					"});" | 
					
						
							| 
									
										
										
										
											2016-12-29 11:42:17 +08:00
										 |  |  | 				); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				return new ConcatSource("define(function() { return ", source, "});"); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-12-16 08:14:19 +08:00
										 |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2016-12-29 11:42:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		for (const template of [mainTemplate, chunkTemplate]) { | 
					
						
							|  |  |  | 			template.hooks.renderWithEntry.tap( | 
					
						
							|  |  |  | 				"AmdMainTemplatePlugin", | 
					
						
							|  |  |  | 				onRenderWithEntry | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2017-12-16 09:07:32 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 18:05:45 +08:00
										 |  |  | 		mainTemplate.hooks.globalHashPaths.tap("AmdMainTemplatePlugin", paths => { | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 			if (this.name) { | 
					
						
							|  |  |  | 				paths.push(this.name); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-12-29 11:42:17 +08:00
										 |  |  | 			return paths; | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-08 18:05:45 +08:00
										 |  |  | 		mainTemplate.hooks.hash.tap("AmdMainTemplatePlugin", hash => { | 
					
						
							| 
									
										
										
										
											2016-12-29 11:42:17 +08:00
										 |  |  | 			hash.update("exports amd"); | 
					
						
							| 
									
										
										
										
											2017-03-02 21:40:09 +08:00
										 |  |  | 			hash.update(this.name); | 
					
						
							| 
									
										
										
										
											2016-12-29 11:42:17 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-12-29 11:42:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | module.exports = AmdMainTemplatePlugin; |