| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | var SetVarMainTemplatePlugin = require("./SetVarMainTemplatePlugin"); | 
					
						
							| 
									
										
										
										
											2016-09-09 04:26:28 +08:00
										 |  |  | var CommonJsHarmonyMainTemplatePlugin = require("./CommonJsHarmonyMainTemplatePlugin"); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-06 00:10:47 +08:00
										 |  |  | function accessorToObjectAccess(accessor) { | 
					
						
							|  |  |  | 	return accessor.map(function(a) { | 
					
						
							|  |  |  | 		return "[" + JSON.stringify(a) + "]"; | 
					
						
							|  |  |  | 	}).join(""); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function accessorAccess(base, accessor, joinWith) { | 
					
						
							|  |  |  | 	accessor = [].concat(accessor); | 
					
						
							|  |  |  | 	return accessor.map(function(a, idx) { | 
					
						
							| 
									
										
										
										
											2015-04-24 05:55:50 +08:00
										 |  |  | 		a = base ? | 
					
						
							|  |  |  | 			base + accessorToObjectAccess(accessor.slice(0, idx + 1)) : | 
					
						
							|  |  |  | 			accessor[0] + accessorToObjectAccess(accessor.slice(1, idx + 1)); | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 		if(idx === accessor.length - 1) return a; | 
					
						
							|  |  |  | 		if(idx === 0 && typeof base === "undefined") return a + " = typeof " + a + " === \"object\" ? " + a + " : {}"; | 
					
						
							| 
									
										
										
										
											2014-04-06 00:10:47 +08:00
										 |  |  | 		return a + " = " + a + " || {}"; | 
					
						
							|  |  |  | 	}).join(joinWith || "; "); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-27 15:01:49 +08:00
										 |  |  | function LibraryTemplatePlugin(name, target, umdNamedDefine, auxiliaryComment) { | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	this.name = name; | 
					
						
							|  |  |  | 	this.target = target; | 
					
						
							| 
									
										
										
										
											2015-08-26 17:50:42 +08:00
										 |  |  | 	this.umdNamedDefine = umdNamedDefine; | 
					
						
							| 
									
										
										
										
											2016-02-27 15:01:49 +08:00
										 |  |  | 	this.auxiliaryComment = auxiliaryComment; | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | } | 
					
						
							|  |  |  | module.exports = LibraryTemplatePlugin; | 
					
						
							|  |  |  | LibraryTemplatePlugin.prototype.apply = function(compiler) { | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 	compiler.plugin("this-compilation", function(compilation) { | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 		switch(this.target) { | 
					
						
							| 
									
										
										
										
											2015-07-13 06:20:09 +08:00
										 |  |  | 			case "var": | 
					
						
							|  |  |  | 				compilation.apply(new SetVarMainTemplatePlugin("var " + accessorAccess(false, this.name))); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case "assign": | 
					
						
							|  |  |  | 				compilation.apply(new SetVarMainTemplatePlugin(accessorAccess(undefined, this.name))); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case "this": | 
					
						
							|  |  |  | 			case "window": | 
					
						
							|  |  |  | 			case "global": | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 				if(this.name) | 
					
						
							| 
									
										
										
										
											2015-07-13 06:20:09 +08:00
										 |  |  | 					compilation.apply(new SetVarMainTemplatePlugin(accessorAccess(this.target, this.name))); | 
					
						
							|  |  |  | 				else | 
					
						
							|  |  |  | 					compilation.apply(new SetVarMainTemplatePlugin(this.target, true)); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case "commonjs": | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 				if(this.name) | 
					
						
							| 
									
										
										
										
											2015-07-13 06:20:09 +08:00
										 |  |  | 					compilation.apply(new SetVarMainTemplatePlugin(accessorAccess("exports", this.name))); | 
					
						
							|  |  |  | 				else | 
					
						
							|  |  |  | 					compilation.apply(new SetVarMainTemplatePlugin("exports", true)); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case "commonjs2": | 
					
						
							|  |  |  | 				compilation.apply(new SetVarMainTemplatePlugin("module.exports")); | 
					
						
							|  |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2016-09-09 04:26:28 +08:00
										 |  |  | 			case "commonjs-module": | 
					
						
							|  |  |  | 				compilation.apply(new CommonJsHarmonyMainTemplatePlugin()); | 
					
						
							|  |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2015-07-13 06:20:09 +08:00
										 |  |  | 			case "amd": | 
					
						
							|  |  |  | 				var AmdMainTemplatePlugin = require("./AmdMainTemplatePlugin"); | 
					
						
							|  |  |  | 				compilation.apply(new AmdMainTemplatePlugin(this.name)); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case "umd": | 
					
						
							|  |  |  | 			case "umd2": | 
					
						
							|  |  |  | 				var UmdMainTemplatePlugin = require("./UmdMainTemplatePlugin"); | 
					
						
							| 
									
										
										
										
											2015-08-26 17:50:42 +08:00
										 |  |  | 				compilation.apply(new UmdMainTemplatePlugin(this.name, { | 
					
						
							|  |  |  | 					optionalAmdExternalAsGlobal: this.target === "umd2", | 
					
						
							| 
									
										
										
										
											2016-02-27 04:25:53 +08:00
										 |  |  | 					namedDefine: this.umdNamedDefine, | 
					
						
							| 
									
										
										
										
											2016-02-27 15:01:49 +08:00
										 |  |  | 					auxiliaryComment: this.auxiliaryComment | 
					
						
							| 
									
										
										
										
											2015-08-26 17:50:42 +08:00
										 |  |  | 				})); | 
					
						
							| 
									
										
										
										
											2015-07-13 06:20:09 +08:00
										 |  |  | 				break; | 
					
						
							|  |  |  | 			case "jsonp": | 
					
						
							|  |  |  | 				var JsonpExportMainTemplatePlugin = require("./JsonpExportMainTemplatePlugin"); | 
					
						
							|  |  |  | 				compilation.apply(new JsonpExportMainTemplatePlugin(this.name)); | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			default: | 
					
						
							|  |  |  | 				throw new Error(this.target + " is not a valid Library target"); | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	}.bind(this)); | 
					
						
							| 
									
										
										
										
											2014-07-22 16:45:14 +08:00
										 |  |  | }; |