| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | var async = require("async"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var Tapable = require("tapable"); | 
					
						
							|  |  |  | var NormalModule = require("./NormalModule"); | 
					
						
							| 
									
										
										
										
											2014-03-02 03:07:42 +08:00
										 |  |  | var RawModule = require("./RawModule"); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | var LoadersList = require("webpack-core/lib/LoadersList"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-04 20:59:43 +08:00
										 |  |  | function NormalModuleFactory(context, resolvers, parser, options) { | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	Tapable.call(this); | 
					
						
							|  |  |  | 	this.resolvers = resolvers; | 
					
						
							|  |  |  | 	this.parser = parser; | 
					
						
							|  |  |  | 	this.loaders = new LoadersList(options.loaders); | 
					
						
							|  |  |  | 	this.preLoaders = new LoadersList(options.preLoaders); | 
					
						
							|  |  |  | 	this.postLoaders = new LoadersList(options.postLoaders); | 
					
						
							| 
									
										
										
										
											2013-02-04 20:59:43 +08:00
										 |  |  | 	this.context = context || ""; | 
					
						
							| 
									
										
										
										
											2015-05-13 06:15:01 +08:00
										 |  |  | 	this.plugin("factory", function() { | 
					
						
							| 
									
										
										
										
											2015-11-11 06:31:03 +08:00
										 |  |  | 		var _this = this; | 
					
						
							| 
									
										
										
										
											2015-05-13 06:15:01 +08:00
										 |  |  | 		return function(result, callback) { | 
					
						
							| 
									
										
										
										
											2015-11-11 06:31:03 +08:00
										 |  |  | 			var resolver = _this.applyPluginsWaterfall("resolver", null); | 
					
						
							| 
									
										
										
										
											2015-05-13 06:15:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			// Ignored
 | 
					
						
							|  |  |  | 			if(!resolver) return callback(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			resolver(result, function onDoneResolving(err, data) { | 
					
						
							|  |  |  | 				if(err) return callback(err); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				// Ignored
 | 
					
						
							|  |  |  | 				if(!data) return callback(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				// direct module
 | 
					
						
							|  |  |  | 				if(typeof data.source === "function") | 
					
						
							|  |  |  | 					return callback(null, data); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-11 06:31:03 +08:00
										 |  |  | 				_this.applyPluginsAsyncWaterfall("after-resolve", data, function(err, result) { | 
					
						
							| 
									
										
										
										
											2015-05-13 06:15:01 +08:00
										 |  |  | 					if(err) return callback(err); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					// Ignored
 | 
					
						
							|  |  |  | 					if(!result) return callback(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-11 06:31:03 +08:00
										 |  |  | 					var createdModule = _this.applyPluginsBailResult("create-module", result); | 
					
						
							| 
									
										
										
										
											2015-05-13 06:15:01 +08:00
										 |  |  | 					if(!createdModule) { | 
					
						
							|  |  |  | 						createdModule = new NormalModule( | 
					
						
							|  |  |  | 							result.request, | 
					
						
							|  |  |  | 							result.userRequest, | 
					
						
							|  |  |  | 							result.rawRequest, | 
					
						
							|  |  |  | 							result.loaders, | 
					
						
							|  |  |  | 							result.resource, | 
					
						
							|  |  |  | 							result.parser | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2015-05-17 00:27:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-11 06:31:03 +08:00
										 |  |  | 					createdModule = _this.applyPluginsWaterfall("module", createdModule); | 
					
						
							| 
									
										
										
										
											2015-05-17 00:27:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-13 06:15:01 +08:00
										 |  |  | 					return callback(null, createdModule); | 
					
						
							| 
									
										
										
										
											2015-11-11 06:31:03 +08:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2015-05-13 06:15:01 +08:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 	this.plugin("resolver", function() { | 
					
						
							| 
									
										
										
										
											2015-11-11 06:31:03 +08:00
										 |  |  | 		var _this = this; | 
					
						
							| 
									
										
										
										
											2015-05-13 06:15:01 +08:00
										 |  |  | 		return function(data, callback) { | 
					
						
							|  |  |  | 			var context = data.context; | 
					
						
							|  |  |  | 			var request = data.request; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			var noAutoLoaders = /^-?!/.test(request); | 
					
						
							|  |  |  | 			var noPrePostAutoLoaders = /^!!/.test(request); | 
					
						
							|  |  |  | 			var noPostAutoLoaders = /^-!/.test(request); | 
					
						
							|  |  |  | 			var elements = request.replace(/^-?!+/, "").replace(/!!+/g, "!").split("!"); | 
					
						
							|  |  |  | 			var resource = elements.pop(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			async.parallel([ | 
					
						
							|  |  |  | 				function(callback) { | 
					
						
							| 
									
										
										
										
											2015-11-11 06:31:03 +08:00
										 |  |  | 					_this.resolveRequestArray(context, elements, _this.resolvers.loader, callback); | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2015-05-13 06:15:01 +08:00
										 |  |  | 				function(callback) { | 
					
						
							|  |  |  | 					if(resource === "" || resource[0] === "?") | 
					
						
							|  |  |  | 						return callback(null, resource); | 
					
						
							| 
									
										
										
										
											2015-11-11 06:31:03 +08:00
										 |  |  | 					_this.resolvers.normal.resolve(context, resource, callback); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-05-13 06:15:01 +08:00
										 |  |  | 			], function(err, results) { | 
					
						
							|  |  |  | 				if(err) return callback(err); | 
					
						
							|  |  |  | 				var loaders = results[0]; | 
					
						
							|  |  |  | 				resource = results[1]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if(resource === false) | 
					
						
							|  |  |  | 					return callback(null, | 
					
						
							|  |  |  | 						new RawModule("/* (ignored) */", | 
					
						
							|  |  |  | 							"ignored " + context + " " + request, | 
					
						
							|  |  |  | 							request + " (ignored)")); // ignored
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				var userRequest = loaders.concat([resource]).join("!"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-16 20:59:50 +08:00
										 |  |  | 				var resourcePath = resource; | 
					
						
							|  |  |  | 				var queryIndex = resourcePath.indexOf("?"); | 
					
						
							|  |  |  | 				if(queryIndex >= 0) | 
					
						
							|  |  |  | 					resourcePath = resourcePath.substr(0, queryIndex); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-13 06:15:01 +08:00
										 |  |  | 				if(noPrePostAutoLoaders) | 
					
						
							| 
									
										
										
										
											2015-11-11 06:31:03 +08:00
										 |  |  | 					return onDoneResolving(); | 
					
						
							| 
									
										
										
										
											2015-05-13 06:15:01 +08:00
										 |  |  | 				if(noAutoLoaders) { | 
					
						
							|  |  |  | 					async.parallel([ | 
					
						
							| 
									
										
										
										
											2015-11-14 04:58:38 +08:00
										 |  |  | 						_this.resolveRequestArray.bind(_this, context, noPostAutoLoaders ? [] : _this.postLoaders.match(resourcePath), _this.resolvers.loader), | 
					
						
							|  |  |  | 						_this.resolveRequestArray.bind(_this, context, _this.preLoaders.match(resourcePath), _this.resolvers.loader) | 
					
						
							| 
									
										
										
										
											2015-05-13 06:15:01 +08:00
										 |  |  | 					], function(err, results) { | 
					
						
							|  |  |  | 						if(err) return callback(err); | 
					
						
							|  |  |  | 						loaders = results[0].concat(loaders).concat(results[1]); | 
					
						
							| 
									
										
										
										
											2015-11-11 06:31:03 +08:00
										 |  |  | 						onDoneResolving(); | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2015-05-13 06:15:01 +08:00
										 |  |  | 				} else { | 
					
						
							|  |  |  | 					async.parallel([ | 
					
						
							| 
									
										
										
										
											2015-11-14 04:58:38 +08:00
										 |  |  | 						_this.resolveRequestArray.bind(_this, context, noPostAutoLoaders ? [] : _this.postLoaders.match(resourcePath), _this.resolvers.loader), | 
					
						
							|  |  |  | 						_this.resolveRequestArray.bind(_this, context, _this.loaders.match(resourcePath), _this.resolvers.loader), | 
					
						
							|  |  |  | 						_this.resolveRequestArray.bind(_this, context, _this.preLoaders.match(resourcePath), _this.resolvers.loader) | 
					
						
							| 
									
										
										
										
											2015-05-13 06:15:01 +08:00
										 |  |  | 					], function(err, results) { | 
					
						
							|  |  |  | 						if(err) return callback(err); | 
					
						
							|  |  |  | 						loaders = results[0].concat(loaders).concat(results[1]).concat(results[2]); | 
					
						
							| 
									
										
										
										
											2015-11-11 06:31:03 +08:00
										 |  |  | 						onDoneResolving(); | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2015-05-13 06:15:01 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-07-13 06:20:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-13 06:15:01 +08:00
										 |  |  | 				function onDoneResolving() { | 
					
						
							|  |  |  | 					callback(null, { | 
					
						
							|  |  |  | 						context: context, | 
					
						
							|  |  |  | 						request: loaders.concat([resource]).join("!"), | 
					
						
							|  |  |  | 						userRequest: userRequest, | 
					
						
							|  |  |  | 						rawRequest: request, | 
					
						
							|  |  |  | 						loaders: loaders, | 
					
						
							|  |  |  | 						resource: resource, | 
					
						
							| 
									
										
										
										
											2015-11-11 06:31:03 +08:00
										 |  |  | 						parser: _this.parser | 
					
						
							| 
									
										
										
										
											2015-05-13 06:15:01 +08:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-11-11 06:31:03 +08:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2015-05-13 06:15:01 +08:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | } | 
					
						
							|  |  |  | module.exports = NormalModuleFactory; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | NormalModuleFactory.prototype = Object.create(Tapable.prototype); | 
					
						
							| 
									
										
										
										
											2015-08-18 19:35:57 +08:00
										 |  |  | NormalModuleFactory.prototype.constructor = NormalModuleFactory; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | NormalModuleFactory.prototype.create = function(context, dependency, callback) { | 
					
						
							| 
									
										
										
										
											2015-11-11 06:31:03 +08:00
										 |  |  | 	var _this = this; | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	context = context || this.context; | 
					
						
							|  |  |  | 	var request = dependency.request; | 
					
						
							| 
									
										
										
										
											2015-11-11 06:31:03 +08:00
										 |  |  | 	_this.applyPluginsAsyncWaterfall("before-resolve", { | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 		context: context, | 
					
						
							| 
									
										
										
										
											2015-05-13 06:15:01 +08:00
										 |  |  | 		request: request, | 
					
						
							|  |  |  | 		dependency: dependency | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	}, function(err, result) { | 
					
						
							|  |  |  | 		if(err) return callback(err); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-06 01:19:04 +08:00
										 |  |  | 		// Ignored
 | 
					
						
							|  |  |  | 		if(!result) return callback(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-11 06:31:03 +08:00
										 |  |  | 		var factory = _this.applyPluginsWaterfall("factory", null); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-13 06:15:01 +08:00
										 |  |  | 		// Ignored
 | 
					
						
							|  |  |  | 		if(!factory) return callback(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		factory(result, callback); | 
					
						
							| 
									
										
										
										
											2013-11-06 01:19:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-11 06:31:03 +08:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | NormalModuleFactory.prototype.resolveRequestArray = function resolveRequestArray(context, array, resolver, callback) { | 
					
						
							|  |  |  | 	if(array.length === 0) return callback(null, []); | 
					
						
							|  |  |  | 	async.map(array, function(item, callback) { | 
					
						
							| 
									
										
										
										
											2014-06-25 00:53:32 +08:00
										 |  |  | 		if(item === "" || item[0] === "?") | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 			return callback(null, item); | 
					
						
							|  |  |  | 		resolver.resolve(context, item, callback); | 
					
						
							|  |  |  | 	}, callback); | 
					
						
							|  |  |  | }; |