| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | var ConcatSource = require("webpack-core/lib/ConcatSource"); | 
					
						
							|  |  |  | var OriginalSource = require("webpack-core/lib/OriginalSource"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function accessorToObjectAccess(accessor) { | 
					
						
							|  |  |  | 	return accessor.map(function(a) { | 
					
						
							|  |  |  | 		return "[" + JSON.stringify(a) + "]"; | 
					
						
							|  |  |  | 	}).join(""); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function accessorAccess(base, accessor) { | 
					
						
							|  |  |  | 	accessor = [].concat(accessor); | 
					
						
							|  |  |  | 	return accessor.map(function(a, idx) { | 
					
						
							| 
									
										
										
										
											2014-08-22 19:51:24 +08:00
										 |  |  | 		a = base + accessorToObjectAccess(accessor.slice(0, idx + 1)); | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 		if(idx === accessor.length - 1) return a; | 
					
						
							|  |  |  | 		return a + " = " + a + " || {}"; | 
					
						
							|  |  |  | 	}).join(", "); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-22 16:45:14 +08:00
										 |  |  | function UmdMainTemplatePlugin(name, optionalAmdExternalAsGlobal) { | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 	this.name = name; | 
					
						
							| 
									
										
										
										
											2014-07-22 16:45:14 +08:00
										 |  |  | 	this.optionalAmdExternalAsGlobal = optionalAmdExternalAsGlobal; | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | } | 
					
						
							|  |  |  | module.exports = UmdMainTemplatePlugin; | 
					
						
							| 
									
										
										
										
											2014-12-22 23:10:23 +08:00
										 |  |  | UmdMainTemplatePlugin.prototype.apply = function(compilation) { | 
					
						
							|  |  |  | 	var mainTemplate = compilation.mainTemplate; | 
					
						
							|  |  |  | 	compilation.templatesPlugin("render-with-entry", function(source, chunk, hash) { | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 		var externals = chunk.modules.filter(function(m) { | 
					
						
							|  |  |  | 			return m.external; | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-07-17 16:39:44 +08:00
										 |  |  | 		var optionalExternals = [], requiredExternals = []; | 
					
						
							| 
									
										
										
										
											2014-08-27 22:25:28 +08:00
										 |  |  | 		if(this.optionalAmdExternalAsGlobal) { | 
					
						
							| 
									
										
										
										
											2014-07-22 16:45:14 +08:00
										 |  |  | 			externals.forEach(function(m) { | 
					
						
							| 
									
										
										
										
											2014-08-27 22:25:28 +08:00
										 |  |  | 				if(m.optional) { | 
					
						
							|  |  |  | 					optionalExternals.push(m); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					requiredExternals.push(m); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-07-22 16:45:14 +08:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 			externals = requiredExternals.concat(optionalExternals); | 
					
						
							| 
									
										
										
										
											2014-08-27 22:25:28 +08:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2014-07-22 16:45:14 +08:00
										 |  |  | 			requiredExternals = externals; | 
					
						
							| 
									
										
										
										
											2014-08-27 22:25:28 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-07-22 16:45:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 		function replaceKeys(str) { | 
					
						
							| 
									
										
										
										
											2014-08-22 19:51:24 +08:00
										 |  |  | 			return mainTemplate.applyPluginsWaterfall("asset-path", str, { | 
					
						
							|  |  |  | 				hash: hash, | 
					
						
							|  |  |  | 				chunk: chunk | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-07-17 16:39:44 +08:00
										 |  |  | 		function externalsDepsArray(modules) { | 
					
						
							|  |  |  | 			return "[" + replaceKeys(modules.map(function(m) { | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 				return JSON.stringify(typeof m.request === "object" ? m.request.amd : m.request); | 
					
						
							|  |  |  | 			}).join(", ")) + "]"; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-07-17 16:39:44 +08:00
										 |  |  | 		function externalsRootArray(modules) { | 
					
						
							|  |  |  | 			return replaceKeys(modules.map(function(m) { | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 				var request = m.request; | 
					
						
							|  |  |  | 				if(typeof request === "object") request = request.root; | 
					
						
							|  |  |  | 				return "root" + accessorToObjectAccess([].concat(request)); | 
					
						
							|  |  |  | 			}).join(", ")); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		function externalsRequireArray(type) { | 
					
						
							|  |  |  | 			return replaceKeys(externals.map(function(m) { | 
					
						
							|  |  |  | 				var request = m.request; | 
					
						
							|  |  |  | 				if(typeof request === "object") request = request[type]; | 
					
						
							|  |  |  | 				if(Array.isArray(request)) { | 
					
						
							| 
									
										
										
										
											2014-07-03 06:00:06 +08:00
										 |  |  | 					var expr = "require(" + JSON.stringify(request[0]) + ")" + accessorToObjectAccess(request.slice(1)); | 
					
						
							| 
									
										
										
										
											2014-07-17 16:39:44 +08:00
										 |  |  | 				} else | 
					
						
							| 
									
										
										
										
											2014-07-03 06:00:06 +08:00
										 |  |  | 					var expr = "require(" + JSON.stringify(request) + ")"; | 
					
						
							|  |  |  | 				if(m.optional) { | 
					
						
							|  |  |  | 					expr = "(function webpackLoadOptionalExternalModule() { try { return " + expr + "; } catch(e) {} }())"; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				return expr; | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 			}).join(", ")); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-07-17 16:39:44 +08:00
										 |  |  | 		function externalsArguments(modules) { | 
					
						
							|  |  |  | 			return modules.map(function(m) { | 
					
						
							|  |  |  | 				return "__WEBPACK_EXTERNAL_MODULE_" + m.id + "__"; | 
					
						
							|  |  |  | 			}).join(", "); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-08-27 22:25:28 +08:00
										 |  |  | 		if(optionalExternals.length > 0) { | 
					
						
							|  |  |  | 			var amdFactory = "function webpackLoadOptionalExternalModuleAmd(" + externalsArguments(requiredExternals) + ") {\n" + | 
					
						
							|  |  |  | 			"			return factory(" + ( | 
					
						
							|  |  |  | 				requiredExternals.length > 0 ? | 
					
						
							|  |  |  | 					externalsArguments(requiredExternals) + ", " + externalsRootArray(optionalExternals) : | 
					
						
							|  |  |  | 					externalsRootArray(optionalExternals) | 
					
						
							|  |  |  | 			) + ");\n" + | 
					
						
							|  |  |  | 			"		}"; | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			var amdFactory = "factory"; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 		return new ConcatSource(new OriginalSource( | 
					
						
							|  |  |  | 			"(function webpackUniversalModuleDefinition(root, factory) {\n" + | 
					
						
							|  |  |  | 			"	if(typeof exports === 'object' && typeof module === 'object')\n" + | 
					
						
							|  |  |  | 			"		module.exports = factory(" + externalsRequireArray("commonjs2") + ");\n" + | 
					
						
							|  |  |  | 			"	else if(typeof define === 'function' && define.amd)\n" + | 
					
						
							| 
									
										
										
										
											2014-07-17 16:39:44 +08:00
										 |  |  | 			(requiredExternals.length > 0 ? | 
					
						
							|  |  |  | 			"		define(" + externalsDepsArray(requiredExternals) + ", " + amdFactory + ");\n" | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 			: | 
					
						
							| 
									
										
										
										
											2014-07-17 16:39:44 +08:00
										 |  |  | 			"		define(" + amdFactory + ");\n" | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 			) + | 
					
						
							|  |  |  | 			(this.name ? | 
					
						
							|  |  |  | 			"	else if(typeof exports === 'object')\n" + | 
					
						
							|  |  |  | 			"		exports[" + JSON.stringify(replaceKeys([].concat(this.name).pop())) + "] = factory(" + externalsRequireArray("commonjs") + ");\n" + | 
					
						
							|  |  |  | 			"	else\n" + | 
					
						
							| 
									
										
										
										
											2014-08-27 22:25:28 +08:00
										 |  |  | 			"		" + replaceKeys(accessorAccess("root", this.name)) + " = factory(" + externalsRootArray(externals) + ");\n" | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 			: | 
					
						
							|  |  |  | 			"	else {\n" + | 
					
						
							| 
									
										
										
										
											2014-07-17 16:39:44 +08:00
										 |  |  | 			(externals.length > 0 ? | 
					
						
							| 
									
										
										
										
											2014-08-27 22:25:28 +08:00
										 |  |  | 			"		var a = typeof exports === 'object' ? factory(" + externalsRequireArray("commonjs") + ") : factory(" + externalsRootArray(externals) + ");\n" | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 			: | 
					
						
							|  |  |  | 			"		var a = factory();\n" | 
					
						
							|  |  |  | 			) + | 
					
						
							|  |  |  | 			"		for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];\n" + | 
					
						
							|  |  |  | 			"	}\n" | 
					
						
							|  |  |  | 			) + | 
					
						
							| 
									
										
										
										
											2014-08-27 22:25:28 +08:00
										 |  |  | 			"})(this, function(" + externalsArguments(externals) + ") {\nreturn ", "webpack/universalModuleDefinition"), source, "\n});\n"); | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 	}.bind(this)); | 
					
						
							| 
									
										
										
										
											2014-08-22 19:51:24 +08:00
										 |  |  | 	mainTemplate.plugin("global-hash-paths", function(paths) { | 
					
						
							|  |  |  | 		if (this.name) paths = paths.concat(this.name); | 
					
						
							|  |  |  | 		return paths; | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 	}.bind(this)); | 
					
						
							|  |  |  | 	mainTemplate.plugin("hash", function(hash) { | 
					
						
							|  |  |  | 		hash.update("umd"); | 
					
						
							|  |  |  | 		hash.update(this.name + ""); | 
					
						
							|  |  |  | 	}.bind(this)); | 
					
						
							| 
									
										
										
										
											2014-08-03 22:05:40 +08:00
										 |  |  | }; |