| 
									
										
										
										
											2013-03-11 21:51:23 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2013-03-26 23:54:41 +08:00
										 |  |  | var ConcatSource = require("webpack-core/lib/ConcatSource"); | 
					
						
							|  |  |  | var OriginalSource = require("webpack-core/lib/OriginalSource"); | 
					
						
							| 
									
										
										
										
											2013-03-11 21:51:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | function UmdMainTemplateDecorator(mainTemplate, name) { | 
					
						
							|  |  |  | 	this.mainTemplate = mainTemplate; | 
					
						
							|  |  |  | 	this.name = name; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | module.exports = UmdMainTemplateDecorator; | 
					
						
							|  |  |  | UmdMainTemplateDecorator.prototype.render = function(hash, chunk, moduleTemplate, dependencyTemplates) { | 
					
						
							|  |  |  | 	var source = this.mainTemplate.render(hash, chunk, moduleTemplate, dependencyTemplates); | 
					
						
							| 
									
										
										
										
											2014-03-05 16:58:51 +08:00
										 |  |  | 	var externals = chunk.modules.filter(function(m) { | 
					
						
							|  |  |  | 		return m.external; | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-03-06 02:55:53 +08:00
										 |  |  | 	function externalsDepsArray() { | 
					
						
							|  |  |  | 		return "[" + externals.map(function(m) { | 
					
						
							|  |  |  | 			return JSON.stringify(typeof m.request === "object" ? m.request.amd : m.request); | 
					
						
							|  |  |  | 		}).join(", ") + "]"; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	function externalsRootArray() { | 
					
						
							|  |  |  | 		return externals.map(function(m) { | 
					
						
							|  |  |  | 			var request = m.request; | 
					
						
							|  |  |  | 			if(typeof request === "object") request = request.root; | 
					
						
							|  |  |  | 			return "root" + [].concat(request).map(function(r) { | 
					
						
							| 
									
										
										
										
											2014-03-05 16:58:51 +08:00
										 |  |  | 				return "[" + JSON.stringify(r) + "]"; | 
					
						
							|  |  |  | 			}).join(""); | 
					
						
							| 
									
										
										
										
											2014-03-06 02:55:53 +08:00
										 |  |  | 		}).join(", "); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	function externalsRequireArray(type) { | 
					
						
							|  |  |  | 		return externals.map(function(m) { | 
					
						
							|  |  |  | 			var request = m.request; | 
					
						
							|  |  |  | 			if(typeof request === "object") request = request[type]; | 
					
						
							|  |  |  | 			if(Array.isArray(request)) { | 
					
						
							|  |  |  | 				return "require(" + JSON.stringify(request[0]) + ")" + request.slice(1).map(function(r) { | 
					
						
							|  |  |  | 					return "[" + JSON.stringify(r) + "]"; | 
					
						
							|  |  |  | 				}).join(""); | 
					
						
							|  |  |  | 			} else  | 
					
						
							|  |  |  | 				return "require(" + JSON.stringify(request) + ")"; | 
					
						
							|  |  |  | 		}).join(", "); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-03-05 16:58:51 +08:00
										 |  |  | 	var externalsArguments = externals.map(function(m) { | 
					
						
							|  |  |  | 		return "__WEBPACK_EXTERNAL_MODULE_" + m.id + "__"; | 
					
						
							|  |  |  | 	}).join(", "); | 
					
						
							| 
									
										
										
										
											2013-03-26 23:54:41 +08:00
										 |  |  | 	return new ConcatSource(new OriginalSource( | 
					
						
							| 
									
										
										
										
											2014-03-05 16:58:51 +08:00
										 |  |  | 		"(function webpackUniversalModuleDefinition(root, factory) {\n" + | 
					
						
							|  |  |  | 		"	if(typeof exports === 'object' && typeof module === 'object')\n" + | 
					
						
							| 
									
										
										
										
											2014-03-06 02:55:53 +08:00
										 |  |  | 		"		module.exports = factory(" + externalsRequireArray("commonjs2") + ");\n" + | 
					
						
							| 
									
										
										
										
											2014-03-05 16:58:51 +08:00
										 |  |  | 		"	else if(typeof define === 'function' && define.amd)\n" + | 
					
						
							|  |  |  | 		(externalsArguments ? | 
					
						
							| 
									
										
										
										
											2014-03-06 02:55:53 +08:00
										 |  |  | 		"		define(" + externalsDepsArray() + ", factory);\n" | 
					
						
							| 
									
										
										
										
											2014-03-05 16:58:51 +08:00
										 |  |  | 		: | 
					
						
							|  |  |  | 		"		define(factory);\n" | 
					
						
							|  |  |  | 		) + | 
					
						
							| 
									
										
										
										
											2013-10-16 04:00:28 +08:00
										 |  |  | 		(this.name ? | 
					
						
							| 
									
										
										
										
											2014-03-05 16:58:51 +08:00
										 |  |  | 		"	else if(typeof exports === 'object')\n" + | 
					
						
							| 
									
										
										
										
											2014-03-06 02:55:53 +08:00
										 |  |  | 		"		exports[" + JSON.stringify(this.name) + "] = factory(" + externalsRequireArray("commonjs") + ");\n" + | 
					
						
							| 
									
										
										
										
											2014-03-05 16:58:51 +08:00
										 |  |  | 		"	else\n" + | 
					
						
							| 
									
										
										
										
											2014-03-06 02:55:53 +08:00
										 |  |  | 		"		root[" + JSON.stringify(this.name) + "] = factory(" + externalsRootArray() + ");\n" | 
					
						
							| 
									
										
										
										
											2013-10-16 04:00:28 +08:00
										 |  |  | 		: | 
					
						
							| 
									
										
										
										
											2014-03-05 16:58:51 +08:00
										 |  |  | 		"	else {\n" + | 
					
						
							|  |  |  | 		(externalsArguments ? | 
					
						
							| 
									
										
										
										
											2014-03-06 02:55:53 +08:00
										 |  |  | 		"		var a = typeof exports === 'object' ? factory(" + externalsRequireArray("commonjs") + ") : factory(" + externalsRootArray() + ");\n" | 
					
						
							| 
									
										
										
										
											2014-03-05 16:58:51 +08:00
										 |  |  | 		: | 
					
						
							|  |  |  | 		"		var a = factory();\n" | 
					
						
							|  |  |  | 		) + | 
					
						
							|  |  |  | 		"		for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];\n" + | 
					
						
							|  |  |  | 		"	}\n" | 
					
						
							| 
									
										
										
										
											2013-10-16 04:00:28 +08:00
										 |  |  | 		) + | 
					
						
							| 
									
										
										
										
											2014-03-05 16:58:51 +08:00
										 |  |  | 		"})(this, function(" + externalsArguments + ") {\nreturn ", "webpack/universalModuleDefinition"), source, "\n})\n"); | 
					
						
							| 
									
										
										
										
											2013-03-11 21:51:23 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | UmdMainTemplateDecorator.prototype.updateHash = function(hash) { | 
					
						
							|  |  |  | 	hash.update("umd"); | 
					
						
							| 
									
										
										
										
											2013-06-28 20:20:11 +08:00
										 |  |  | 	hash.update(this.name + ""); | 
					
						
							| 
									
										
										
										
											2013-03-11 21:51:23 +08:00
										 |  |  | 	this.mainTemplate.updateHash(hash); | 
					
						
							| 
									
										
										
										
											2013-11-29 15:46:54 +08:00
										 |  |  | }; |