| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2015-12-30 00:44:55 +08:00
										 |  |  | var ConcatSource = require("webpack-sources").ConcatSource; | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | function AmdMainTemplatePlugin(name) { | 
					
						
							|  |  |  | 	this.name = name; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | module.exports = AmdMainTemplatePlugin; | 
					
						
							| 
									
										
										
										
											2014-12-22 23:10:23 +08:00
										 |  |  | AmdMainTemplatePlugin.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; | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		var externalsDepsArray = JSON.stringify(externals.map(function(m) { | 
					
						
							|  |  |  | 			return typeof m.request === "object" ? m.request.amd : m.request; | 
					
						
							|  |  |  | 		})); | 
					
						
							|  |  |  | 		var externalsArguments = externals.map(function(m) { | 
					
						
							|  |  |  | 			return "__WEBPACK_EXTERNAL_MODULE_" + m.id + "__"; | 
					
						
							|  |  |  | 		}).join(", "); | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 		if(this.name) { | 
					
						
							| 
									
										
										
										
											2014-09-17 01:33:21 +08:00
										 |  |  | 			var name = mainTemplate.applyPluginsWaterfall("asset-path", this.name, { | 
					
						
							| 
									
										
										
										
											2014-08-22 19:51:24 +08:00
										 |  |  | 				hash: hash, | 
					
						
							|  |  |  | 				chunk: chunk | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 			return new ConcatSource("define(" + JSON.stringify(name) + ", " + externalsDepsArray + ", function(" + externalsArguments + ") { return ", source, "});"); | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 		} else if(externalsArguments) { | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 			return new ConcatSource("define(" + externalsDepsArray + ", function(" + externalsArguments + ") { return ", source, "});"); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			return new ConcatSource("define(function() { return ", source, "});"); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}.bind(this)); | 
					
						
							| 
									
										
										
										
											2014-08-22 19:51:24 +08:00
										 |  |  | 	mainTemplate.plugin("global-hash-paths", function(paths) { | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 		if(this.name) paths.push(this.name); | 
					
						
							| 
									
										
										
										
											2014-08-22 19:51:24 +08:00
										 |  |  | 		return paths; | 
					
						
							| 
									
										
										
										
											2014-06-03 03:23:53 +08:00
										 |  |  | 	}.bind(this)); | 
					
						
							|  |  |  | 	mainTemplate.plugin("hash", function(hash) { | 
					
						
							|  |  |  | 		hash.update("exports amd"); | 
					
						
							|  |  |  | 		hash.update(this.name + ""); | 
					
						
							|  |  |  | 	}.bind(this)); | 
					
						
							| 
									
										
										
										
											2014-08-22 19:51:24 +08:00
										 |  |  | }; |