| 
									
										
										
										
											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"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 22:59:27 +08:00
										 |  |  | const SetVarTemplatePlugin = require("./SetVarTemplatePlugin"); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 22:59:27 +08:00
										 |  |  | /** @typedef {import("../declarations/WebpackOptions").LibraryCustomUmdCommentObject} LibraryCustomUmdCommentObject */ | 
					
						
							| 
									
										
										
										
											2018-09-19 19:05:22 +08:00
										 |  |  | /** @typedef {import("../declarations/WebpackOptions").LibraryCustomUmdObject} LibraryCustomUmdObject */ | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2018-09-19 19:05:22 +08:00
										 |  |  |  * @param {string|string[]|LibraryCustomUmdObject} accessor the accessor | 
					
						
							|  |  |  |  * @param {"amd" | "commonjs" | "root"} umdProperty property used when a custom umd object is provided | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  |  * @param {string=} joinWith the element separator | 
					
						
							|  |  |  |  * @returns {string} the path | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-09-19 19:05:22 +08:00
										 |  |  | const accessorAccess = (base, accessor, umdProperty, joinWith = "; ") => { | 
					
						
							|  |  |  | 	const normalizedAccessor = | 
					
						
							| 
									
										
										
										
											2018-10-02 03:31:26 +08:00
										 |  |  | 		typeof accessor === "object" && !Array.isArray(accessor) | 
					
						
							|  |  |  | 			? accessor[umdProperty] | 
					
						
							|  |  |  | 			: accessor; | 
					
						
							| 
									
										
										
										
											2018-09-19 19:05:22 +08:00
										 |  |  | 	const accessors = Array.isArray(normalizedAccessor) | 
					
						
							|  |  |  | 		? normalizedAccessor | 
					
						
							|  |  |  | 		: [normalizedAccessor]; | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 	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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 22:59:27 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @typedef {Object} LibraryOptions | 
					
						
							|  |  |  |  * @property {string|string[]|LibraryCustomUmdObject=} library name of library | 
					
						
							|  |  |  |  * @property {string=} libraryTarget type of library | 
					
						
							|  |  |  |  * @property {boolean=} umdNamedDefine setting this to true will name the UMD module | 
					
						
							|  |  |  |  * @property {string|LibraryCustomUmdCommentObject=} auxiliaryComment comment in the UMD wrapper | 
					
						
							|  |  |  |  * @property {string|string[]=} libraryExport which export should be exposed as library | 
					
						
							|  |  |  |  * @property {string=} globalObject global object expression | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-25 23:21:24 +08:00
										 |  |  | class LibraryTemplatePlugin { | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2019-10-02 22:59:27 +08:00
										 |  |  | 	 * @param {LibraryOptions} options options | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2019-10-02 22:59:27 +08:00
										 |  |  | 	constructor(options) { | 
					
						
							|  |  |  | 		if (arguments.length > 1) { | 
					
						
							|  |  |  | 			options = { | 
					
						
							|  |  |  | 				library: arguments[0], | 
					
						
							|  |  |  | 				libraryTarget: arguments[1], | 
					
						
							|  |  |  | 				umdNamedDefine: arguments[2], | 
					
						
							|  |  |  | 				auxiliaryComment: arguments[3], | 
					
						
							|  |  |  | 				libraryExport: arguments[4], | 
					
						
							|  |  |  | 				globalObject: null | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		this.options = options; | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2019-10-02 22:59:27 +08:00
										 |  |  | 		if (this.options.libraryExport) { | 
					
						
							| 
									
										
										
										
											2019-10-04 18:24:52 +08:00
										 |  |  | 			const ExportPropertyTemplatePlugin = require("./ExportPropertyTemplatePlugin"); | 
					
						
							| 
									
										
										
										
											2019-10-09 04:29:46 +08:00
										 |  |  | 			new ExportPropertyTemplatePlugin( | 
					
						
							|  |  |  | 				this.options.libraryExport, | 
					
						
							|  |  |  | 				"used a library export" | 
					
						
							|  |  |  | 			).apply(compiler); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			const FlagEntryExportAsUsedPlugin = require("./FlagEntryExportAsUsedPlugin"); | 
					
						
							|  |  |  | 			new FlagEntryExportAsUsedPlugin( | 
					
						
							|  |  |  | 				this.options.libraryTarget !== "module", | 
					
						
							|  |  |  | 				"used a library export" | 
					
						
							|  |  |  | 			).apply(compiler); | 
					
						
							| 
									
										
										
										
											2019-10-02 22:59:27 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		switch (this.options.libraryTarget) { | 
					
						
							|  |  |  | 			case "umd": | 
					
						
							|  |  |  | 			case "umd2": { | 
					
						
							|  |  |  | 				const UmdTemplatePlugin = require("./UmdTemplatePlugin"); | 
					
						
							|  |  |  | 				new UmdTemplatePlugin(this.options.library, { | 
					
						
							|  |  |  | 					optionalAmdExternalAsGlobal: this.options.libraryTarget === "umd2", | 
					
						
							|  |  |  | 					namedDefine: this.options.umdNamedDefine, | 
					
						
							|  |  |  | 					auxiliaryComment: this.options.auxiliaryComment || "" | 
					
						
							|  |  |  | 				}).apply(compiler); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			case "amd": | 
					
						
							|  |  |  | 			case "amd-require": { | 
					
						
							|  |  |  | 				const AmdTemplatePlugin = require("./AmdTemplatePlugin"); | 
					
						
							|  |  |  | 				new AmdTemplatePlugin({ | 
					
						
							|  |  |  | 					name: this.options.library, | 
					
						
							|  |  |  | 					requireAsWrapper: this.options.libraryTarget === "amd-require" | 
					
						
							|  |  |  | 				}).apply(compiler); | 
					
						
							|  |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2017-06-02 20:52:41 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-10-02 22:59:27 +08:00
										 |  |  | 			case "var": | 
					
						
							|  |  |  | 				if ( | 
					
						
							|  |  |  | 					!this.options.library || | 
					
						
							|  |  |  | 					(typeof this.options.library === "object" && | 
					
						
							|  |  |  | 						!Array.isArray(this.options.library)) | 
					
						
							|  |  |  | 				) { | 
					
						
							|  |  |  | 					throw new Error( | 
					
						
							|  |  |  | 						"library name must be set and not an UMD custom object for non-UMD target" | 
					
						
							|  |  |  | 					); | 
					
						
							| 
									
										
										
										
											2019-09-30 16:08:08 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2019-10-02 22:59:27 +08:00
										 |  |  | 				new SetVarTemplatePlugin( | 
					
						
							|  |  |  | 					`var ${accessorAccess(undefined, this.options.library, "root")}`, | 
					
						
							|  |  |  | 					false | 
					
						
							|  |  |  | 				).apply(compiler); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case "assign": | 
					
						
							|  |  |  | 				new SetVarTemplatePlugin( | 
					
						
							|  |  |  | 					accessorAccess(undefined, this.options.library, "root"), | 
					
						
							|  |  |  | 					false | 
					
						
							|  |  |  | 				).apply(compiler); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case "this": | 
					
						
							|  |  |  | 			case "self": | 
					
						
							|  |  |  | 			case "window": | 
					
						
							|  |  |  | 				if (this.options.library) { | 
					
						
							|  |  |  | 					new SetVarTemplatePlugin( | 
					
						
							|  |  |  | 						accessorAccess( | 
					
						
							|  |  |  | 							this.options.libraryTarget, | 
					
						
							|  |  |  | 							this.options.library, | 
					
						
							|  |  |  | 							"root" | 
					
						
							|  |  |  | 						), | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 						false | 
					
						
							| 
									
										
										
										
											2019-10-02 22:59:27 +08:00
										 |  |  | 					).apply(compiler); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					new SetVarTemplatePlugin(this.options.libraryTarget, true).apply( | 
					
						
							|  |  |  | 						compiler | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 					); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2019-10-02 22:59:27 +08:00
										 |  |  | 				break; | 
					
						
							|  |  |  | 			case "global": { | 
					
						
							|  |  |  | 				const globalObject = | 
					
						
							|  |  |  | 					this.options.globalObject || compiler.options.output.globalObject; | 
					
						
							|  |  |  | 				if (this.options.library) { | 
					
						
							|  |  |  | 					new SetVarTemplatePlugin( | 
					
						
							|  |  |  | 						accessorAccess(globalObject, this.options.library, "root"), | 
					
						
							|  |  |  | 						false | 
					
						
							|  |  |  | 					).apply(compiler); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					new SetVarTemplatePlugin(globalObject, true).apply(compiler); | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2019-10-02 22:59:27 +08:00
										 |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			case "commonjs": | 
					
						
							|  |  |  | 				if (this.options.library) { | 
					
						
							|  |  |  | 					new SetVarTemplatePlugin( | 
					
						
							|  |  |  | 						accessorAccess("exports", this.options.library, "commonjs"), | 
					
						
							|  |  |  | 						false | 
					
						
							|  |  |  | 					).apply(compiler); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					new SetVarTemplatePlugin("exports", true).apply(compiler); | 
					
						
							| 
									
										
										
										
											2018-05-09 17:32:06 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2019-10-02 22:59:27 +08:00
										 |  |  | 				break; | 
					
						
							|  |  |  | 			case "commonjs2": | 
					
						
							|  |  |  | 			case "commonjs-module": | 
					
						
							|  |  |  | 				new SetVarTemplatePlugin("module.exports", false).apply(compiler); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case "jsonp": { | 
					
						
							|  |  |  | 				if (!this.options.library || typeof this.options.library === "object") { | 
					
						
							|  |  |  | 					throw new Error( | 
					
						
							|  |  |  | 						"library name must be set and not an array or UMD custom object for non-UMD target" | 
					
						
							|  |  |  | 					); | 
					
						
							| 
									
										
										
										
											2019-03-02 05:26:36 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2019-10-02 22:59:27 +08:00
										 |  |  | 				const JsonpExportTemplatePlugin = require("./web/JsonpExportTemplatePlugin"); | 
					
						
							|  |  |  | 				new JsonpExportTemplatePlugin(this.options.library).apply(compiler); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			case "system": { | 
					
						
							|  |  |  | 				const SystemTemplatePlugin = require("./SystemTemplatePlugin"); | 
					
						
							|  |  |  | 				new SystemTemplatePlugin({ | 
					
						
							|  |  |  | 					name: this.options.library | 
					
						
							|  |  |  | 				}).apply(compiler); | 
					
						
							|  |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2017-02-25 23:21:24 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-10-09 04:29:46 +08:00
										 |  |  | 			case "module": | 
					
						
							|  |  |  | 				// TODO
 | 
					
						
							|  |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2019-10-02 22:59:27 +08:00
										 |  |  | 			default: | 
					
						
							|  |  |  | 				throw new Error( | 
					
						
							|  |  |  | 					`${this.options.libraryTarget} is not a valid Library target` | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-02-25 23:21:24 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											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; |