| 
									
										
										
										
											2015-11-17 06:11:15 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2017-01-04 13:13:20 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2015-11-17 06:11:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 13:13:20 +08:00
										 |  |  | const ModuleFilenameHelpers = require("./ModuleFilenameHelpers"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class LoaderOptionsPlugin { | 
					
						
							|  |  |  | 	constructor(options) { | 
					
						
							|  |  |  | 		if(typeof options !== "object") options = {}; | 
					
						
							|  |  |  | 		if(!options.test) options.test = { | 
					
						
							|  |  |  | 			test: () => true | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 		this.options = options; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-11-17 06:11:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-04 13:13:20 +08:00
										 |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2017-02-05 07:29:26 +08:00
										 |  |  | 		const options = this.options; | 
					
						
							| 
									
										
										
										
											2017-01-04 13:13:20 +08:00
										 |  |  | 		compiler.plugin("compilation", (compilation) => { | 
					
						
							|  |  |  | 			compilation.plugin("normal-module-loader", (context, module) => { | 
					
						
							| 
									
										
										
										
											2017-02-05 07:29:26 +08:00
										 |  |  | 				const resource = module.resource; | 
					
						
							| 
									
										
										
										
											2017-01-04 13:13:20 +08:00
										 |  |  | 				if(!resource) return; | 
					
						
							| 
									
										
										
										
											2017-02-05 07:29:26 +08:00
										 |  |  | 				const i = resource.indexOf("?"); | 
					
						
							| 
									
										
										
										
											2017-01-04 13:13:20 +08:00
										 |  |  | 				if(ModuleFilenameHelpers.matchObject(options, i < 0 ? resource : resource.substr(0, i))) { | 
					
						
							| 
									
										
										
										
											2017-02-05 07:29:26 +08:00
										 |  |  | 					const filterSet = new Set(["include", "exclude", "test"]); | 
					
						
							| 
									
										
										
										
											2017-01-04 13:13:20 +08:00
										 |  |  | 					Object.keys(options) | 
					
						
							|  |  |  | 						.filter((key) => !filterSet.has(key)) | 
					
						
							|  |  |  | 						.forEach((key) => context[key] = options[key]); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2015-11-17 06:11:15 +08:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-01-04 13:13:20 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = LoaderOptionsPlugin; |