| 
									
										
										
										
											2013-01-31 09:33:11 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2014-06-18 04:46:24 +08:00
										 |  |  | var path = require("path"); | 
					
						
							| 
									
										
										
										
											2016-01-07 02:56:17 +08:00
										 |  |  | var ContextElementDependency = require("./dependencies/ContextElementDependency"); | 
					
						
							| 
									
										
										
										
											2014-06-18 04:46:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-18 04:38:17 +08:00
										 |  |  | function ContextReplacementPlugin(resourceRegExp, newContentResource, newContentRecursive, newContentRegExp) { | 
					
						
							| 
									
										
										
										
											2013-01-31 09:33:11 +08:00
										 |  |  | 	this.resourceRegExp = resourceRegExp; | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 	if(typeof newContentResource === "function") { | 
					
						
							| 
									
										
										
										
											2014-10-23 16:58:26 +08:00
										 |  |  | 		this.newContentCallback = newContentResource; | 
					
						
							| 
									
										
										
										
											2016-01-07 02:56:17 +08:00
										 |  |  | 	} else if(typeof newContentResource === "string" && typeof newContentRecursive === "object") { | 
					
						
							|  |  |  | 		this.newContentResource = newContentResource; | 
					
						
							|  |  |  | 		this.newContentCreateContextMap = function(fs, callback) { | 
					
						
							|  |  |  | 			callback(null, newContentRecursive) | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 	} else if(typeof newContentResource === "string" && typeof newContentRecursive === "function") { | 
					
						
							|  |  |  | 		this.newContentResource = newContentResource; | 
					
						
							|  |  |  | 		this.newContentCreateContextMap = newContentRecursive; | 
					
						
							| 
									
										
										
										
											2014-10-23 16:58:26 +08:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 		if(typeof newContentResource !== "string") { | 
					
						
							| 
									
										
										
										
											2014-10-23 16:58:26 +08:00
										 |  |  | 			newContentRegExp = newContentRecursive; | 
					
						
							|  |  |  | 			newContentRecursive = newContentResource; | 
					
						
							|  |  |  | 			newContentResource = undefined; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 		if(typeof newContentRecursive !== "boolean") { | 
					
						
							| 
									
										
										
										
											2014-10-23 16:58:26 +08:00
										 |  |  | 			newContentRegExp = newContentRecursive; | 
					
						
							|  |  |  | 			newContentRecursive = undefined; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		this.newContentResource = newContentResource; | 
					
						
							|  |  |  | 		this.newContentRecursive = newContentRecursive; | 
					
						
							|  |  |  | 		this.newContentRegExp = newContentRegExp; | 
					
						
							| 
									
										
										
										
											2014-06-18 04:38:17 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-01-31 09:33:11 +08:00
										 |  |  | } | 
					
						
							|  |  |  | module.exports = ContextReplacementPlugin; | 
					
						
							|  |  |  | ContextReplacementPlugin.prototype.apply = function(compiler) { | 
					
						
							|  |  |  | 	var resourceRegExp = this.resourceRegExp; | 
					
						
							| 
									
										
										
										
											2014-10-23 16:58:26 +08:00
										 |  |  | 	var newContentCallback = this.newContentCallback; | 
					
						
							| 
									
										
										
										
											2014-06-18 04:38:17 +08:00
										 |  |  | 	var newContentResource = this.newContentResource; | 
					
						
							|  |  |  | 	var newContentRecursive = this.newContentRecursive; | 
					
						
							| 
									
										
										
										
											2013-01-31 09:33:11 +08:00
										 |  |  | 	var newContentRegExp = this.newContentRegExp; | 
					
						
							| 
									
										
										
										
											2016-01-07 02:56:17 +08:00
										 |  |  | 	var newContentCreateContextMap = this.newContentCreateContextMap; | 
					
						
							| 
									
										
										
										
											2013-01-31 09:33:11 +08:00
										 |  |  | 	compiler.plugin("context-module-factory", function(cmf) { | 
					
						
							| 
									
										
										
										
											2013-11-06 01:19:04 +08:00
										 |  |  | 		cmf.plugin("before-resolve", function(result, callback) { | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 			if(!result) return callback(); | 
					
						
							|  |  |  | 			if(resourceRegExp.test(result.request)) { | 
					
						
							| 
									
										
										
										
											2016-01-07 02:56:17 +08:00
										 |  |  | 				if(typeof newContentResource !== "undefined") | 
					
						
							|  |  |  | 					result.request = newContentResource; | 
					
						
							|  |  |  | 				if(typeof newContentRecursive !== "undefined") | 
					
						
							|  |  |  | 					result.recursive = newContentRecursive; | 
					
						
							|  |  |  | 				if(typeof newContentRegExp !== "undefined") | 
					
						
							|  |  |  | 					result.regExp = newContentRegExp; | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 				if(typeof newContentCallback === "function") { | 
					
						
							| 
									
										
										
										
											2014-10-23 16:58:26 +08:00
										 |  |  | 					newContentCallback(result); | 
					
						
							| 
									
										
										
										
											2016-07-20 18:51:40 +08:00
										 |  |  | 				} else { | 
					
						
							|  |  |  | 					result.dependencies.forEach(function(d) { | 
					
						
							|  |  |  | 						if(d.critical) | 
					
						
							|  |  |  | 							d.critical = false; | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2014-10-23 16:58:26 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-11-06 01:19:04 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			return callback(null, result); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-01-31 09:33:11 +08:00
										 |  |  | 		cmf.plugin("after-resolve", function(result, callback) { | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 			if(!result) return callback(); | 
					
						
							|  |  |  | 			if(resourceRegExp.test(result.resource)) { | 
					
						
							| 
									
										
										
										
											2016-01-07 02:56:17 +08:00
										 |  |  | 				if(typeof newContentResource !== "undefined") | 
					
						
							|  |  |  | 					result.resource = path.resolve(result.resource, newContentResource); | 
					
						
							|  |  |  | 				if(typeof newContentRecursive !== "undefined") | 
					
						
							|  |  |  | 					result.recursive = newContentRecursive; | 
					
						
							|  |  |  | 				if(typeof newContentRegExp !== "undefined") | 
					
						
							|  |  |  | 					result.regExp = newContentRegExp; | 
					
						
							|  |  |  | 				if(typeof newContentCreateContextMap === "function") | 
					
						
							|  |  |  | 					result.resolveDependencies = createResolveDependenciesFromContextMap(newContentCreateContextMap); | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 				if(typeof newContentCallback === "function") { | 
					
						
							| 
									
										
										
										
											2014-10-23 16:58:26 +08:00
										 |  |  | 					var origResource = result.resource; | 
					
						
							|  |  |  | 					newContentCallback(result); | 
					
						
							| 
									
										
										
										
											2015-07-16 06:19:23 +08:00
										 |  |  | 					if(result.resource !== origResource) { | 
					
						
							| 
									
										
										
										
											2014-10-23 16:58:26 +08:00
										 |  |  | 						result.resource = path.resolve(origResource, result.resource); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2016-07-20 18:51:40 +08:00
										 |  |  | 				} else { | 
					
						
							|  |  |  | 					result.dependencies.forEach(function(d) { | 
					
						
							|  |  |  | 						if(d.critical) | 
					
						
							|  |  |  | 							d.critical = false; | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2014-10-23 16:58:26 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-01-31 09:33:11 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			return callback(null, result); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-10-23 16:58:26 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2016-01-07 02:56:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | function createResolveDependenciesFromContextMap(createContextMap) { | 
					
						
							|  |  |  | 	return function resolveDependenciesFromContextMap(fs, resource, recursive, regExp, callback) { | 
					
						
							|  |  |  | 		createContextMap(fs, function(err, map) { | 
					
						
							|  |  |  | 			if(err) return callback(err); | 
					
						
							|  |  |  | 			var dependencies = Object.keys(map).map(function(key) { | 
					
						
							|  |  |  | 				return new ContextElementDependency(map[key], key); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			callback(null, dependencies); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-11-21 08:00:25 +08:00
										 |  |  | } |