| 
									
										
										
										
											2013-01-31 09:33:11 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-13 01:25:51 +08:00
										 |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2014-05-28 03:13:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-11 19:09:42 +08:00
										 |  |  | const { join, dirname } = require("./util/fs"); | 
					
						
							| 
									
										
										
										
											2015-07-13 06:20:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-03 04:05:46 +08:00
										 |  |  | /** @typedef {import("./Compiler")} Compiler */ | 
					
						
							| 
									
										
										
										
											2022-11-10 17:48:26 +08:00
										 |  |  | /** @typedef {function(import("./NormalModuleFactory").ResolveData): void} ModuleReplacer */ | 
					
						
							| 
									
										
										
										
											2018-11-03 04:05:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-13 01:25:51 +08:00
										 |  |  | class NormalModuleReplacementPlugin { | 
					
						
							| 
									
										
										
										
											2018-11-03 04:05:46 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Create an instance of the plugin | 
					
						
							|  |  |  | 	 * @param {RegExp} resourceRegExp the resource matcher | 
					
						
							|  |  |  | 	 * @param {string|ModuleReplacer} newResource the resource replacement | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-01-13 01:25:51 +08:00
										 |  |  | 	constructor(resourceRegExp, newResource) { | 
					
						
							|  |  |  | 		this.resourceRegExp = resourceRegExp; | 
					
						
							|  |  |  | 		this.newResource = newResource; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-03 04:05:46 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Apply the plugin | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-01-13 01:25:51 +08:00
										 |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2017-02-05 07:33:21 +08:00
										 |  |  | 		const resourceRegExp = this.resourceRegExp; | 
					
						
							|  |  |  | 		const newResource = this.newResource; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		compiler.hooks.normalModuleFactory.tap( | 
					
						
							|  |  |  | 			"NormalModuleReplacementPlugin", | 
					
						
							|  |  |  | 			nmf => { | 
					
						
							|  |  |  | 				nmf.hooks.beforeResolve.tap("NormalModuleReplacementPlugin", result => { | 
					
						
							|  |  |  | 					if (resourceRegExp.test(result.request)) { | 
					
						
							|  |  |  | 						if (typeof newResource === "function") { | 
					
						
							|  |  |  | 							newResource(result); | 
					
						
							|  |  |  | 						} else { | 
					
						
							|  |  |  | 							result.request = newResource; | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2017-01-13 01:25:51 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 				nmf.hooks.afterResolve.tap("NormalModuleReplacementPlugin", result => { | 
					
						
							| 
									
										
										
										
											2019-01-05 02:17:37 +08:00
										 |  |  | 					const createData = result.createData; | 
					
						
							| 
									
										
										
										
											2023-05-25 06:41:32 +08:00
										 |  |  | 					if ( | 
					
						
							|  |  |  | 						resourceRegExp.test(/** @type {string} */ (createData.resource)) | 
					
						
							|  |  |  | 					) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						if (typeof newResource === "function") { | 
					
						
							|  |  |  | 							newResource(result); | 
					
						
							|  |  |  | 						} else { | 
					
						
							| 
									
										
										
										
											2019-06-11 19:09:42 +08:00
										 |  |  | 							const fs = compiler.inputFileSystem; | 
					
						
							|  |  |  | 							if ( | 
					
						
							|  |  |  | 								newResource.startsWith("/") || | 
					
						
							|  |  |  | 								(newResource.length > 1 && newResource[1] === ":") | 
					
						
							|  |  |  | 							) { | 
					
						
							|  |  |  | 								createData.resource = newResource; | 
					
						
							|  |  |  | 							} else { | 
					
						
							|  |  |  | 								createData.resource = join( | 
					
						
							|  |  |  | 									fs, | 
					
						
							| 
									
										
										
										
											2023-05-25 06:41:32 +08:00
										 |  |  | 									dirname(fs, /** @type {string} */ (createData.resource)), | 
					
						
							| 
									
										
										
										
											2019-06-11 19:09:42 +08:00
										 |  |  | 									newResource | 
					
						
							|  |  |  | 								); | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2017-01-13 01:25:51 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-01-13 01:25:51 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = NormalModuleReplacementPlugin; |