| 
									
										
										
										
											2016-01-04 04:42:56 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | function LoadersList(list) { | 
					
						
							|  |  |  | 	this.list = list || []; | 
					
						
							|  |  |  | 	this.list.forEach(function(element) { | 
					
						
							|  |  |  | 		if(element === null || typeof element !== "object") | 
					
						
							| 
									
										
										
										
											2016-01-15 01:28:00 +08:00
										 |  |  | 			throw new TypeError("Each element of the loaders list must be an object or array"); | 
					
						
							| 
									
										
										
										
											2016-01-04 04:42:56 +08:00
										 |  |  | 	}); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | module.exports = LoadersList; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function regExpAsMatcher(regExp) { | 
					
						
							|  |  |  | 	return function(str) { | 
					
						
							|  |  |  | 		return regExp.test(str); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function asMatcher(test) { | 
					
						
							|  |  |  | 	if(typeof test === "string") { | 
					
						
							|  |  |  | 		return regExpAsMatcher(new RegExp("^" + test.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"))); | 
					
						
							| 
									
										
										
										
											2016-01-19 17:10:44 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if(typeof test === "function") { | 
					
						
							| 
									
										
										
										
											2016-01-04 04:42:56 +08:00
										 |  |  | 		return test; | 
					
						
							| 
									
										
										
										
											2016-01-19 17:10:44 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if(test instanceof RegExp) { | 
					
						
							| 
									
										
										
										
											2016-01-04 04:42:56 +08:00
										 |  |  | 		return regExpAsMatcher(test); | 
					
						
							| 
									
										
										
										
											2016-01-19 17:10:44 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if(Array.isArray(test)) { | 
					
						
							|  |  |  | 		// create an array of match functions
 | 
					
						
							|  |  |  | 		var matchers = test.map(asMatcher); | 
					
						
							| 
									
										
										
										
											2016-01-04 04:42:56 +08:00
										 |  |  | 		return function(str) { | 
					
						
							| 
									
										
										
										
											2016-01-30 18:14:17 +08:00
										 |  |  | 			return matchers.some(function(matcher) { | 
					
						
							| 
									
										
										
										
											2016-01-19 17:10:44 +08:00
										 |  |  | 				return matcher(str); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2016-01-04 04:42:56 +08:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-01-19 17:10:44 +08:00
										 |  |  | 	throw new TypeError(test + " is not a valid test"); | 
					
						
							| 
									
										
										
										
											2016-01-04 04:42:56 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-15 01:28:00 +08:00
										 |  |  | function getLoaderWithQuery(loader, query) { | 
					
						
							|  |  |  | 	if(typeof query === "string") { | 
					
						
							|  |  |  | 		return loader + "?" + query; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-01-19 17:10:44 +08:00
										 |  |  | 	return loader + "?" + JSON.stringify(query); | 
					
						
							| 
									
										
										
										
											2016-01-15 01:28:00 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-04 04:42:56 +08:00
										 |  |  | function getLoadersFromObject(element) { | 
					
						
							| 
									
										
										
										
											2016-01-15 01:28:00 +08:00
										 |  |  | 	var loaders = element.loaders || element.loader; | 
					
						
							|  |  |  | 	if(typeof loaders === "string") { | 
					
						
							|  |  |  | 		loaders = loaders.split("!"); | 
					
						
							|  |  |  | 	} else if(!Array.isArray(loaders)) { | 
					
						
							|  |  |  | 		throw new TypeError("Element from loaders list should have one of the fields 'loader' or 'loaders'"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// legacy query usage
 | 
					
						
							| 
									
										
										
										
											2016-01-04 04:42:56 +08:00
										 |  |  | 	if(element.query) { | 
					
						
							| 
									
										
										
										
											2016-01-15 01:28:00 +08:00
										 |  |  | 		if(loaders.length > 1) { | 
					
						
							|  |  |  | 			throw new Error("Cannot define 'query' and multiple loaders in loaders list"); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		var loader = loaders[0]; | 
					
						
							|  |  |  | 		return [getLoaderWithQuery(loader, element.query)]; | 
					
						
							| 
									
										
										
										
											2016-01-04 04:42:56 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-01-15 01:28:00 +08:00
										 |  |  | 	return loaders.map(function(entry) { | 
					
						
							|  |  |  | 		if(typeof entry === "string") { | 
					
						
							|  |  |  | 			return entry; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if(typeof entry === "object") { | 
					
						
							|  |  |  | 			var loader = entry.loader; | 
					
						
							|  |  |  | 			if(!loader) { | 
					
						
							|  |  |  | 				throw new TypeError("Element from loaders list with objects should have a 'loader' specified"); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			var query = entry.query; | 
					
						
							|  |  |  | 			if(!query) { | 
					
						
							|  |  |  | 				return loader; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			return getLoaderWithQuery(loader, query); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		throw new TypeError("Element from loaders list should be a string or an object"); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2016-01-04 04:42:56 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | LoadersList.prototype.matchPart = function matchPart(str, test) { | 
					
						
							|  |  |  | 	var matcher = asMatcher(test); | 
					
						
							|  |  |  | 	return matcher(str); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | LoadersList.prototype.match = function match(str) { | 
					
						
							| 
									
										
										
										
											2016-01-15 01:28:00 +08:00
										 |  |  | 	return this.list | 
					
						
							|  |  |  | 		.map(function(element) { | 
					
						
							| 
									
										
										
										
											2016-05-29 18:17:36 +08:00
										 |  |  | 			if(Array.isArray(element)) { | 
					
						
							|  |  |  | 				for(var i = 0; i < element.length; i++) { | 
					
						
							|  |  |  | 					if(this.matchObject(str, element[i])) return getLoadersFromObject(element[i]); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				if(this.matchObject(str, element)) return getLoadersFromObject(element); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-01-15 01:28:00 +08:00
										 |  |  | 		}, this) | 
					
						
							|  |  |  | 		.filter(Boolean) | 
					
						
							|  |  |  | 		.reduce(function(loaders, r) { | 
					
						
							|  |  |  | 			return loaders.concat(r); | 
					
						
							| 
									
										
										
										
											2016-01-19 17:10:44 +08:00
										 |  |  | 		}, []); | 
					
						
							| 
									
										
										
										
											2016-01-04 04:42:56 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | LoadersList.prototype.matchObject = function matchObject(str, obj) { | 
					
						
							| 
									
										
										
										
											2016-01-15 01:28:00 +08:00
										 |  |  | 	if(obj.test && !this.matchPart(str, obj.test)) return false; | 
					
						
							|  |  |  | 	if(obj.include && !this.matchPart(str, obj.include)) return false; | 
					
						
							|  |  |  | 	if(obj.exclude && this.matchPart(str, obj.exclude)) return false; | 
					
						
							| 
									
										
										
										
											2016-01-04 04:42:56 +08:00
										 |  |  | 	return true; | 
					
						
							|  |  |  | }; |