| 
									
										
										
										
											2013-01-31 01:49:25 +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-02-25 23:21:24 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const SetVarMainTemplatePlugin = require("./SetVarMainTemplatePlugin"); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | /** @typedef {import("./Compiler")} Compiler */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @param {string[]} accessor the accessor to convert to path | 
					
						
							|  |  |  |  * @returns {string} the path | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | const accessorToObjectAccess = accessor => { | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 	return accessor.map(a => `[${JSON.stringify(a)}]`).join(""); | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-04-06 00:10:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param {string=} base the path prefix | 
					
						
							|  |  |  |  * @param {string|string[]} accessor the accessor | 
					
						
							|  |  |  |  * @param {string=} joinWith the element separator | 
					
						
							|  |  |  |  * @returns {string} the path | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const accessorAccess = (base, accessor, joinWith = "; ") => { | 
					
						
							|  |  |  | 	const accessors = Array.isArray(accessor) ? accessor : [accessor]; | 
					
						
							|  |  |  | 	return accessors | 
					
						
							|  |  |  | 		.map((_, idx) => { | 
					
						
							|  |  |  | 			const a = base | 
					
						
							|  |  |  | 				? base + accessorToObjectAccess(accessors.slice(0, idx + 1)) | 
					
						
							|  |  |  | 				: accessors[0] + accessorToObjectAccess(accessors.slice(1, idx + 1)); | 
					
						
							|  |  |  | 			if (idx === accessors.length - 1) return a; | 
					
						
							| 
									
										
										
										
											2018-08-21 08:26:50 +08:00
										 |  |  | 			if (idx === 0 && base === undefined) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				return `${a} = typeof ${a} === "object" ? ${a} : {}`; | 
					
						
							| 
									
										
										
										
											2018-05-29 20:50:40 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			return `${a} = ${a} || {}`; | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 		.join(joinWith); | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-04-06 00:10:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-25 23:21:24 +08:00
										 |  |  | class LibraryTemplatePlugin { | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {string} name name of library | 
					
						
							|  |  |  | 	 * @param {string} target type of library | 
					
						
							|  |  |  | 	 * @param {boolean} umdNamedDefine setting this to true will name the UMD module | 
					
						
							|  |  |  | 	 * @param {string|TODO} auxiliaryComment comment in the UMD wrapper | 
					
						
							|  |  |  | 	 * @param {string|string[]} exportProperty which export should be exposed as library | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-06-02 20:52:41 +08:00
										 |  |  | 	constructor(name, target, umdNamedDefine, auxiliaryComment, exportProperty) { | 
					
						
							| 
									
										
										
										
											2017-02-25 23:21:24 +08:00
										 |  |  | 		this.name = name; | 
					
						
							|  |  |  | 		this.target = target; | 
					
						
							|  |  |  | 		this.umdNamedDefine = umdNamedDefine; | 
					
						
							|  |  |  | 		this.auxiliaryComment = auxiliaryComment; | 
					
						
							| 
									
										
										
										
											2017-06-02 20:52:41 +08:00
										 |  |  | 		this.exportProperty = exportProperty; | 
					
						
							| 
									
										
										
										
											2017-02-25 23:21:24 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-02-25 23:21:24 +08:00
										 |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		compiler.hooks.thisCompilation.tap("LibraryTemplatePlugin", compilation => { | 
					
						
							|  |  |  | 			if (this.exportProperty) { | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 				const ExportPropertyMainTemplatePlugin = require("./ExportPropertyMainTemplatePlugin"); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				new ExportPropertyMainTemplatePlugin(this.exportProperty).apply( | 
					
						
							|  |  |  | 					compilation | 
					
						
							|  |  |  | 				); | 
					
						
							| 
									
										
										
										
											2017-06-02 20:52:41 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			switch (this.target) { | 
					
						
							| 
									
										
										
										
											2017-02-25 23:21:24 +08:00
										 |  |  | 				case "var": | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					new SetVarMainTemplatePlugin( | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 						`var ${accessorAccess(undefined, this.name)}`, | 
					
						
							|  |  |  | 						false | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					).apply(compilation); | 
					
						
							| 
									
										
										
										
											2017-02-25 23:21:24 +08:00
										 |  |  | 					break; | 
					
						
							|  |  |  | 				case "assign": | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					new SetVarMainTemplatePlugin( | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 						accessorAccess(undefined, this.name), | 
					
						
							|  |  |  | 						false | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					).apply(compilation); | 
					
						
							| 
									
										
										
										
											2017-02-25 23:21:24 +08:00
										 |  |  | 					break; | 
					
						
							|  |  |  | 				case "this": | 
					
						
							| 
									
										
										
										
											2017-12-28 01:46:37 +08:00
										 |  |  | 				case "self": | 
					
						
							| 
									
										
										
										
											2017-02-25 23:21:24 +08:00
										 |  |  | 				case "window": | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 					if (this.name) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						new SetVarMainTemplatePlugin( | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 							accessorAccess(this.target, this.name), | 
					
						
							|  |  |  | 							false | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						).apply(compilation); | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 					} else { | 
					
						
							| 
									
										
										
										
											2017-12-20 16:53:33 +08:00
										 |  |  | 						new SetVarMainTemplatePlugin(this.target, true).apply(compilation); | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2017-02-25 23:21:24 +08:00
										 |  |  | 					break; | 
					
						
							| 
									
										
										
										
											2017-12-28 01:46:37 +08:00
										 |  |  | 				case "global": | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 					if (this.name) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						new SetVarMainTemplatePlugin( | 
					
						
							|  |  |  | 							accessorAccess( | 
					
						
							|  |  |  | 								compilation.runtimeTemplate.outputOptions.globalObject, | 
					
						
							|  |  |  | 								this.name | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 							), | 
					
						
							|  |  |  | 							false | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						).apply(compilation); | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 					} else { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						new SetVarMainTemplatePlugin( | 
					
						
							|  |  |  | 							compilation.runtimeTemplate.outputOptions.globalObject, | 
					
						
							|  |  |  | 							true | 
					
						
							|  |  |  | 						).apply(compilation); | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2017-12-28 01:46:37 +08:00
										 |  |  | 					break; | 
					
						
							| 
									
										
										
										
											2017-02-25 23:21:24 +08:00
										 |  |  | 				case "commonjs": | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 					if (this.name) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						new SetVarMainTemplatePlugin( | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 							accessorAccess("exports", this.name), | 
					
						
							|  |  |  | 							false | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						).apply(compilation); | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 					} else { | 
					
						
							|  |  |  | 						new SetVarMainTemplatePlugin("exports", true).apply(compilation); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2017-02-25 23:21:24 +08:00
										 |  |  | 					break; | 
					
						
							|  |  |  | 				case "commonjs2": | 
					
						
							|  |  |  | 				case "commonjs-module": | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 					new SetVarMainTemplatePlugin("module.exports", false).apply( | 
					
						
							|  |  |  | 						compilation | 
					
						
							|  |  |  | 					); | 
					
						
							| 
									
										
										
										
											2017-02-25 23:21:24 +08:00
										 |  |  | 					break; | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 				case "amd": { | 
					
						
							|  |  |  | 					const AmdMainTemplatePlugin = require("./AmdMainTemplatePlugin"); | 
					
						
							| 
									
										
										
										
											2017-12-20 16:53:33 +08:00
										 |  |  | 					new AmdMainTemplatePlugin(this.name).apply(compilation); | 
					
						
							| 
									
										
										
										
											2017-02-25 23:21:24 +08:00
										 |  |  | 					break; | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-02-25 23:21:24 +08:00
										 |  |  | 				case "umd": | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 				case "umd2": { | 
					
						
							|  |  |  | 					const UmdMainTemplatePlugin = require("./UmdMainTemplatePlugin"); | 
					
						
							| 
									
										
										
										
											2017-12-20 16:53:33 +08:00
										 |  |  | 					new UmdMainTemplatePlugin(this.name, { | 
					
						
							| 
									
										
										
										
											2017-02-25 23:21:24 +08:00
										 |  |  | 						optionalAmdExternalAsGlobal: this.target === "umd2", | 
					
						
							|  |  |  | 						namedDefine: this.umdNamedDefine, | 
					
						
							|  |  |  | 						auxiliaryComment: this.auxiliaryComment | 
					
						
							| 
									
										
										
										
											2017-12-20 16:53:33 +08:00
										 |  |  | 					}).apply(compilation); | 
					
						
							| 
									
										
										
										
											2017-02-25 23:21:24 +08:00
										 |  |  | 					break; | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				case "jsonp": { | 
					
						
							|  |  |  | 					const JsonpExportMainTemplatePlugin = require("./web/JsonpExportMainTemplatePlugin"); | 
					
						
							| 
									
										
										
										
											2017-12-20 16:53:33 +08:00
										 |  |  | 					new JsonpExportMainTemplatePlugin(this.name).apply(compilation); | 
					
						
							| 
									
										
										
										
											2017-02-25 23:21:24 +08:00
										 |  |  | 					break; | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2017-02-25 23:21:24 +08:00
										 |  |  | 				default: | 
					
						
							|  |  |  | 					throw new Error(`${this.target} is not a valid Library target`); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-02-25 23:21:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | module.exports = LibraryTemplatePlugin; |