| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +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"); | 
					
						
							| 
									
										
										
										
											2018-08-14 17:18:22 +08:00
										 |  |  | const ExternalModule = require("./ExternalModule"); | 
					
						
							| 
									
										
										
										
											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 */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-27 01:41:22 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @typedef {Object} AmdMainTemplatePluginOptions | 
					
						
							| 
									
										
										
										
											2018-09-27 13:01:17 +08:00
										 |  |  |  * @param {string=} name the library name | 
					
						
							| 
									
										
										
										
											2018-09-27 01:41:22 +08:00
										 |  |  |  * @property {boolean=} requireAsWrapper | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-29 11:42:17 +08:00
										 |  |  | class AmdMainTemplatePlugin { | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2018-09-27 01:41:22 +08:00
										 |  |  | 	 * @param {AmdMainTemplatePluginOptions} options the plugin options | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2018-09-27 13:01:17 +08:00
										 |  |  | 	constructor(options) { | 
					
						
							|  |  |  | 		if (!options || typeof options === "string") { | 
					
						
							|  |  |  | 			this.name = options; | 
					
						
							|  |  |  | 			this.requireAsWrapper = false; | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			this.name = options.name; | 
					
						
							|  |  |  | 			this.requireAsWrapper = options.requireAsWrapper; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-12-29 11:42:17 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2019-08-27 02:21:07 +08:00
										 |  |  | 		const { mainTemplate, chunkTemplate, runtimeTemplate } = compilation; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const modern = runtimeTemplate.supportsArrowFunction(); | 
					
						
							| 
									
										
										
										
											2016-12-29 11:42:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 08:14:19 +08:00
										 |  |  | 		const onRenderWithEntry = (source, chunk, hash) => { | 
					
						
							| 
									
										
										
										
											2018-08-14 17:18:22 +08:00
										 |  |  | 			const chunkGraph = compilation.chunkGraph; | 
					
						
							|  |  |  | 			const modules = chunkGraph | 
					
						
							|  |  |  | 				.getChunkModules(chunk) | 
					
						
							|  |  |  | 				.filter(m => m instanceof ExternalModule); | 
					
						
							|  |  |  | 			const externals = /** @type {ExternalModule[]} */ (modules); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			const externalsDepsArray = JSON.stringify( | 
					
						
							| 
									
										
										
										
											2019-02-05 17:06:32 +08:00
										 |  |  | 				externals.map(m => | 
					
						
							| 
									
										
										
										
											2019-02-06 22:37:11 +08:00
										 |  |  | 					typeof m.request === "object" && !Array.isArray(m.request) | 
					
						
							|  |  |  | 						? m.request.amd | 
					
						
							|  |  |  | 						: m.request | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				) | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 			const externalsArguments = externals | 
					
						
							|  |  |  | 				.map( | 
					
						
							| 
									
										
										
										
											2018-08-28 17:56:48 +08:00
										 |  |  | 					m => | 
					
						
							|  |  |  | 						`__WEBPACK_EXTERNAL_MODULE_${Template.toIdentifier( | 
					
						
							|  |  |  | 							`${chunkGraph.getModuleId(m)}` | 
					
						
							|  |  |  | 						)}__`
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				) | 
					
						
							|  |  |  | 				.join(", "); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-27 02:21:07 +08:00
										 |  |  | 			const fnStart = modern | 
					
						
							|  |  |  | 				? `(${externalsArguments}) => ` | 
					
						
							|  |  |  | 				: `function(${externalsArguments}) { return `; | 
					
						
							|  |  |  | 			const fnEnd = modern ? "" : "}"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-27 01:41:22 +08:00
										 |  |  | 			if (this.requireAsWrapper) { | 
					
						
							|  |  |  | 				return new ConcatSource( | 
					
						
							| 
									
										
										
										
											2019-08-27 02:21:07 +08:00
										 |  |  | 					`require(${externalsDepsArray}, ${fnStart}`, | 
					
						
							| 
									
										
										
										
											2018-09-27 01:41:22 +08:00
										 |  |  | 					source, | 
					
						
							| 
									
										
										
										
											2019-08-27 02:21:07 +08:00
										 |  |  | 					`${fnEnd});` | 
					
						
							| 
									
										
										
										
											2018-09-27 01:41:22 +08:00
										 |  |  | 				); | 
					
						
							|  |  |  | 			} else 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( | 
					
						
							| 
									
										
										
										
											2019-08-27 02:21:07 +08:00
										 |  |  | 					`define(${JSON.stringify(name)}, ${externalsDepsArray}, ${fnStart}`, | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					source, | 
					
						
							| 
									
										
										
										
											2019-08-27 02:21:07 +08:00
										 |  |  | 					`${fnEnd});` | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				); | 
					
						
							|  |  |  | 			} else if (externalsArguments) { | 
					
						
							|  |  |  | 				return new ConcatSource( | 
					
						
							| 
									
										
										
										
											2019-08-27 02:21:07 +08:00
										 |  |  | 					`define(${externalsDepsArray}, ${fnStart}`, | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					source, | 
					
						
							| 
									
										
										
										
											2019-08-27 02:21:07 +08:00
										 |  |  | 					`${fnEnd});` | 
					
						
							| 
									
										
										
										
											2016-12-29 11:42:17 +08:00
										 |  |  | 				); | 
					
						
							|  |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2019-08-27 02:21:07 +08:00
										 |  |  | 				return new ConcatSource(`define(${fnStart}`, source, `${fnEnd});`); | 
					
						
							| 
									
										
										
										
											2016-12-29 11:42:17 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-12-16 08:14:19 +08:00
										 |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2016-12-29 11:42:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-02 22:39:51 +08:00
										 |  |  | 		mainTemplate.hooks.renderWithEntry.tap( | 
					
						
							|  |  |  | 			"AmdMainTemplatePlugin", | 
					
						
							|  |  |  | 			onRenderWithEntry | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		chunkTemplate.hooks.renderWithEntry.tap( | 
					
						
							|  |  |  | 			"AmdMainTemplatePlugin", | 
					
						
							|  |  |  | 			onRenderWithEntry | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-12-16 09:07:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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"); | 
					
						
							| 
									
										
										
										
											2018-09-25 20:45:30 +08:00
										 |  |  | 			if (this.name) { | 
					
						
							|  |  |  | 				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; |