| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2017-02-25 23:21:24 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const SetVarMainTemplatePlugin = require("./SetVarMainTemplatePlugin"); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | const accessorToObjectAccess = accessor => { | 
					
						
							|  |  |  | 	return accessor | 
					
						
							|  |  |  | 		.map(a => { | 
					
						
							|  |  |  | 			return `[${JSON.stringify(a)}]`; | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 		.join(""); | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-04-06 00:10:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-08 18:32:05 +08:00
										 |  |  | const accessorAccess = (base, accessor, joinWith) => { | 
					
						
							| 
									
										
										
										
											2014-04-06 00:10:47 +08:00
										 |  |  | 	accessor = [].concat(accessor); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 	return accessor | 
					
						
							|  |  |  | 		.map((a, idx) => { | 
					
						
							|  |  |  | 			a = base | 
					
						
							|  |  |  | 				? base + accessorToObjectAccess(accessor.slice(0, idx + 1)) | 
					
						
							|  |  |  | 				: accessor[0] + accessorToObjectAccess(accessor.slice(1, idx + 1)); | 
					
						
							|  |  |  | 			if (idx === accessor.length - 1) return a; | 
					
						
							|  |  |  | 			if (idx === 0 && typeof base === "undefined") | 
					
						
							|  |  |  | 				return `${a} = typeof ${a} === "object" ? ${a} : {}`; | 
					
						
							|  |  |  | 			return `${a} = ${a} || {}`; | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 		.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 { | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		compiler.hooks.thisCompilation.tap("LibraryTemplatePlugin", compilation => { | 
					
						
							|  |  |  | 			if (this.exportProperty) { | 
					
						
							| 
									
										
										
										
											2017-06-02 20:52:41 +08:00
										 |  |  | 				var 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( | 
					
						
							|  |  |  | 						`var ${accessorAccess(false, this.name)}` | 
					
						
							|  |  |  | 					).apply(compilation); | 
					
						
							| 
									
										
										
										
											2017-02-25 23:21:24 +08:00
										 |  |  | 					break; | 
					
						
							|  |  |  | 				case "assign": | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					new SetVarMainTemplatePlugin( | 
					
						
							|  |  |  | 						accessorAccess(undefined, this.name) | 
					
						
							|  |  |  | 					).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-02-25 09:00:20 +08:00
										 |  |  | 					if (this.name) | 
					
						
							|  |  |  | 						new SetVarMainTemplatePlugin( | 
					
						
							|  |  |  | 							accessorAccess(this.target, this.name) | 
					
						
							|  |  |  | 						).apply(compilation); | 
					
						
							| 
									
										
										
										
											2017-02-25 23:21:24 +08:00
										 |  |  | 					else | 
					
						
							| 
									
										
										
										
											2017-12-20 16:53:33 +08:00
										 |  |  | 						new SetVarMainTemplatePlugin(this.target, true).apply(compilation); | 
					
						
							| 
									
										
										
										
											2017-02-25 23:21:24 +08:00
										 |  |  | 					break; | 
					
						
							| 
									
										
										
										
											2017-12-28 01:46:37 +08:00
										 |  |  | 				case "global": | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					if (this.name) | 
					
						
							|  |  |  | 						new SetVarMainTemplatePlugin( | 
					
						
							|  |  |  | 							accessorAccess( | 
					
						
							|  |  |  | 								compilation.runtimeTemplate.outputOptions.globalObject, | 
					
						
							|  |  |  | 								this.name | 
					
						
							|  |  |  | 							) | 
					
						
							|  |  |  | 						).apply(compilation); | 
					
						
							| 
									
										
										
										
											2017-12-28 01:46:37 +08:00
										 |  |  | 					else | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						new SetVarMainTemplatePlugin( | 
					
						
							|  |  |  | 							compilation.runtimeTemplate.outputOptions.globalObject, | 
					
						
							|  |  |  | 							true | 
					
						
							|  |  |  | 						).apply(compilation); | 
					
						
							| 
									
										
										
										
											2017-12-28 01:46:37 +08:00
										 |  |  | 					break; | 
					
						
							| 
									
										
										
										
											2017-02-25 23:21:24 +08:00
										 |  |  | 				case "commonjs": | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					if (this.name) | 
					
						
							|  |  |  | 						new SetVarMainTemplatePlugin( | 
					
						
							|  |  |  | 							accessorAccess("exports", this.name) | 
					
						
							|  |  |  | 						).apply(compilation); | 
					
						
							|  |  |  | 					else new SetVarMainTemplatePlugin("exports", true).apply(compilation); | 
					
						
							| 
									
										
										
										
											2017-02-25 23:21:24 +08:00
										 |  |  | 					break; | 
					
						
							|  |  |  | 				case "commonjs2": | 
					
						
							|  |  |  | 				case "commonjs-module": | 
					
						
							| 
									
										
										
										
											2017-12-20 16:53:33 +08:00
										 |  |  | 					new SetVarMainTemplatePlugin("module.exports").apply(compilation); | 
					
						
							| 
									
										
										
										
											2017-02-25 23:21:24 +08:00
										 |  |  | 					break; | 
					
						
							|  |  |  | 				case "amd": | 
					
						
							|  |  |  | 					var 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; | 
					
						
							|  |  |  | 				case "umd": | 
					
						
							|  |  |  | 				case "umd2": | 
					
						
							|  |  |  | 					var 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; | 
					
						
							|  |  |  | 				case "jsonp": | 
					
						
							| 
									
										
										
										
											2017-11-12 01:48:29 +08:00
										 |  |  | 					var 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; | 
					
						
							|  |  |  | 				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; |