| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {import("../../declarations/WebpackOptions").LibraryOptions} LibraryOptions */ | 
					
						
							|  |  |  | /** @typedef {import("../../declarations/WebpackOptions").LibraryType} LibraryType */ | 
					
						
							|  |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** @type {WeakMap<Compiler, Set<LibraryType>>} */ | 
					
						
							|  |  |  | const enabledTypes = new WeakMap(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const getEnabledTypes = compiler => { | 
					
						
							|  |  |  | 	let set = enabledTypes.get(compiler); | 
					
						
							|  |  |  | 	if (set === undefined) { | 
					
						
							|  |  |  | 		set = new Set(); | 
					
						
							|  |  |  | 		enabledTypes.set(compiler, set); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return set; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class EnableLibraryPlugin { | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {LibraryType} type library type that should be available | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	constructor(type) { | 
					
						
							|  |  |  | 		this.type = type; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-26 03:45:56 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							|  |  |  | 	 * @param {LibraryType} type type of library | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	static setEnabled(compiler, type) { | 
					
						
							|  |  |  | 		getEnabledTypes(compiler).add(type); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-26 19:34:57 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							|  |  |  | 	 * @param {LibraryType} type type of library | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	static checkEnabled(compiler, type) { | 
					
						
							|  |  |  | 		if (!getEnabledTypes(compiler).has(type)) { | 
					
						
							|  |  |  | 			throw new Error( | 
					
						
							|  |  |  | 				`Library type "${type}" is not enabled. ` + | 
					
						
							|  |  |  | 					"EnableLibraryPlugin need to be used to enable this type of library. " + | 
					
						
							|  |  |  | 					'This usually happens through the "output.enabledLibraryTypes" option. ' + | 
					
						
							|  |  |  | 					'If you are using a function as entry which sets "library", you need to add all potential library types to "output.enabledLibraryTypes". ' + | 
					
						
							| 
									
										
										
										
											2021-01-05 18:30:48 +08:00
										 |  |  | 					"These types are enabled: " + | 
					
						
							| 
									
										
										
										
											2020-02-26 19:34:57 +08:00
										 |  |  | 					Array.from(getEnabledTypes(compiler)).join(", ") | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2020-04-23 16:48:36 +08:00
										 |  |  | 	 * Apply the plugin | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							|  |  |  | 		const { type } = this; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Only enable once
 | 
					
						
							| 
									
										
										
										
											2020-02-26 19:34:57 +08:00
										 |  |  | 		const enabled = getEnabledTypes(compiler); | 
					
						
							|  |  |  | 		if (enabled.has(type)) return; | 
					
						
							|  |  |  | 		enabled.add(type); | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (typeof type === "string") { | 
					
						
							| 
									
										
										
										
											2021-02-11 02:14:50 +08:00
										 |  |  | 			const enableExportProperty = () => { | 
					
						
							|  |  |  | 				const ExportPropertyTemplatePlugin = require("./ExportPropertyLibraryPlugin"); | 
					
						
							|  |  |  | 				new ExportPropertyTemplatePlugin({ | 
					
						
							|  |  |  | 					type, | 
					
						
							|  |  |  | 					nsObjectUsed: type !== "module" | 
					
						
							|  |  |  | 				}).apply(compiler); | 
					
						
							|  |  |  | 			}; | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | 			switch (type) { | 
					
						
							|  |  |  | 				case "var": { | 
					
						
							| 
									
										
										
										
											2020-11-26 20:12:49 +08:00
										 |  |  | 					//@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
 | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | 					const AssignLibraryPlugin = require("./AssignLibraryPlugin"); | 
					
						
							|  |  |  | 					new AssignLibraryPlugin({ | 
					
						
							|  |  |  | 						type, | 
					
						
							|  |  |  | 						prefix: [], | 
					
						
							|  |  |  | 						declare: "var", | 
					
						
							|  |  |  | 						unnamed: "error" | 
					
						
							|  |  |  | 					}).apply(compiler); | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2021-01-04 23:12:48 +08:00
										 |  |  | 				case "assign-properties": { | 
					
						
							| 
									
										
										
										
											2021-01-18 23:57:05 +08:00
										 |  |  | 					//@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
 | 
					
						
							| 
									
										
										
										
											2021-01-04 23:12:48 +08:00
										 |  |  | 					const AssignLibraryPlugin = require("./AssignLibraryPlugin"); | 
					
						
							|  |  |  | 					new AssignLibraryPlugin({ | 
					
						
							|  |  |  | 						type, | 
					
						
							|  |  |  | 						prefix: [], | 
					
						
							|  |  |  | 						declare: false, | 
					
						
							|  |  |  | 						unnamed: "error", | 
					
						
							|  |  |  | 						named: "copy" | 
					
						
							|  |  |  | 					}).apply(compiler); | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | 				case "assign": { | 
					
						
							| 
									
										
										
										
											2020-11-26 20:12:49 +08:00
										 |  |  | 					//@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
 | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | 					const AssignLibraryPlugin = require("./AssignLibraryPlugin"); | 
					
						
							|  |  |  | 					new AssignLibraryPlugin({ | 
					
						
							|  |  |  | 						type, | 
					
						
							|  |  |  | 						prefix: [], | 
					
						
							|  |  |  | 						declare: false, | 
					
						
							|  |  |  | 						unnamed: "error" | 
					
						
							|  |  |  | 					}).apply(compiler); | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				case "this": { | 
					
						
							| 
									
										
										
										
											2020-11-26 20:12:49 +08:00
										 |  |  | 					//@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
 | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | 					const AssignLibraryPlugin = require("./AssignLibraryPlugin"); | 
					
						
							|  |  |  | 					new AssignLibraryPlugin({ | 
					
						
							|  |  |  | 						type, | 
					
						
							|  |  |  | 						prefix: ["this"], | 
					
						
							|  |  |  | 						declare: false, | 
					
						
							|  |  |  | 						unnamed: "copy" | 
					
						
							|  |  |  | 					}).apply(compiler); | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				case "window": { | 
					
						
							| 
									
										
										
										
											2020-11-26 20:12:49 +08:00
										 |  |  | 					//@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
 | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | 					const AssignLibraryPlugin = require("./AssignLibraryPlugin"); | 
					
						
							|  |  |  | 					new AssignLibraryPlugin({ | 
					
						
							|  |  |  | 						type, | 
					
						
							|  |  |  | 						prefix: ["window"], | 
					
						
							|  |  |  | 						declare: false, | 
					
						
							|  |  |  | 						unnamed: "copy" | 
					
						
							|  |  |  | 					}).apply(compiler); | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				case "self": { | 
					
						
							| 
									
										
										
										
											2020-11-26 20:12:49 +08:00
										 |  |  | 					//@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
 | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | 					const AssignLibraryPlugin = require("./AssignLibraryPlugin"); | 
					
						
							|  |  |  | 					new AssignLibraryPlugin({ | 
					
						
							|  |  |  | 						type, | 
					
						
							|  |  |  | 						prefix: ["self"], | 
					
						
							|  |  |  | 						declare: false, | 
					
						
							|  |  |  | 						unnamed: "copy" | 
					
						
							|  |  |  | 					}).apply(compiler); | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				case "global": { | 
					
						
							| 
									
										
										
										
											2020-11-26 20:12:49 +08:00
										 |  |  | 					//@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
 | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | 					const AssignLibraryPlugin = require("./AssignLibraryPlugin"); | 
					
						
							|  |  |  | 					new AssignLibraryPlugin({ | 
					
						
							|  |  |  | 						type, | 
					
						
							|  |  |  | 						prefix: "global", | 
					
						
							|  |  |  | 						declare: false, | 
					
						
							|  |  |  | 						unnamed: "copy" | 
					
						
							|  |  |  | 					}).apply(compiler); | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				case "commonjs": { | 
					
						
							| 
									
										
										
										
											2020-11-26 20:12:49 +08:00
										 |  |  | 					//@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
 | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | 					const AssignLibraryPlugin = require("./AssignLibraryPlugin"); | 
					
						
							|  |  |  | 					new AssignLibraryPlugin({ | 
					
						
							|  |  |  | 						type, | 
					
						
							|  |  |  | 						prefix: ["exports"], | 
					
						
							|  |  |  | 						declare: false, | 
					
						
							|  |  |  | 						unnamed: "copy" | 
					
						
							|  |  |  | 					}).apply(compiler); | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				case "commonjs2": | 
					
						
							|  |  |  | 				case "commonjs-module": { | 
					
						
							| 
									
										
										
										
											2020-11-26 20:12:49 +08:00
										 |  |  | 					//@ts-expect-error https://github.com/microsoft/TypeScript/issues/41697
 | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | 					const AssignLibraryPlugin = require("./AssignLibraryPlugin"); | 
					
						
							|  |  |  | 					new AssignLibraryPlugin({ | 
					
						
							|  |  |  | 						type, | 
					
						
							|  |  |  | 						prefix: ["module", "exports"], | 
					
						
							|  |  |  | 						declare: false, | 
					
						
							|  |  |  | 						unnamed: "assign" | 
					
						
							|  |  |  | 					}).apply(compiler); | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				case "amd": | 
					
						
							|  |  |  | 				case "amd-require": { | 
					
						
							| 
									
										
										
										
											2021-02-11 02:14:50 +08:00
										 |  |  | 					enableExportProperty(); | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | 					const AmdLibraryPlugin = require("./AmdLibraryPlugin"); | 
					
						
							|  |  |  | 					new AmdLibraryPlugin({ | 
					
						
							|  |  |  | 						type, | 
					
						
							|  |  |  | 						requireAsWrapper: type === "amd-require" | 
					
						
							|  |  |  | 					}).apply(compiler); | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				case "umd": | 
					
						
							|  |  |  | 				case "umd2": { | 
					
						
							| 
									
										
										
										
											2021-02-11 02:14:50 +08:00
										 |  |  | 					enableExportProperty(); | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | 					const UmdLibraryPlugin = require("./UmdLibraryPlugin"); | 
					
						
							|  |  |  | 					new UmdLibraryPlugin({ | 
					
						
							|  |  |  | 						type, | 
					
						
							|  |  |  | 						optionalAmdExternalAsGlobal: type === "umd2" | 
					
						
							|  |  |  | 					}).apply(compiler); | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				case "system": { | 
					
						
							| 
									
										
										
										
											2021-02-11 02:14:50 +08:00
										 |  |  | 					enableExportProperty(); | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | 					const SystemLibraryPlugin = require("./SystemLibraryPlugin"); | 
					
						
							|  |  |  | 					new SystemLibraryPlugin({ | 
					
						
							|  |  |  | 						type | 
					
						
							|  |  |  | 					}).apply(compiler); | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				case "jsonp": { | 
					
						
							| 
									
										
										
										
											2021-02-11 02:14:50 +08:00
										 |  |  | 					enableExportProperty(); | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | 					const JsonpLibraryPlugin = require("./JsonpLibraryPlugin"); | 
					
						
							|  |  |  | 					new JsonpLibraryPlugin({ | 
					
						
							|  |  |  | 						type | 
					
						
							|  |  |  | 					}).apply(compiler); | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2021-02-05 06:17:20 +08:00
										 |  |  | 				case "module": { | 
					
						
							| 
									
										
										
										
											2021-02-11 02:14:50 +08:00
										 |  |  | 					enableExportProperty(); | 
					
						
							| 
									
										
										
										
											2021-02-05 06:17:20 +08:00
										 |  |  | 					const ModuleLibraryPlugin = require("./ModuleLibraryPlugin"); | 
					
						
							|  |  |  | 					new ModuleLibraryPlugin({ | 
					
						
							|  |  |  | 						type | 
					
						
							|  |  |  | 					}).apply(compiler); | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | 					break; | 
					
						
							| 
									
										
										
										
											2021-02-05 06:17:20 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | 				default: | 
					
						
							| 
									
										
										
										
											2020-08-26 03:45:56 +08:00
										 |  |  | 					throw new Error(`Unsupported library type ${type}.
 | 
					
						
							|  |  |  | Plugins which provide custom library types must call EnableLibraryPlugin.setEnabled(compiler, type) to disable this error.`);
 | 
					
						
							| 
									
										
										
										
											2020-02-20 03:25:49 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			// TODO support plugin instances here
 | 
					
						
							|  |  |  | 			// apply them to the compiler
 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = EnableLibraryPlugin; |