| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-03 17:06:45 +08:00
										 |  |  | const { dirname, join } = require("./util/fs"); | 
					
						
							| 
									
										
										
										
											2015-07-13 06:20:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-03 04:05:46 +08:00
										 |  |  | /** @typedef {import("./Compiler")} Compiler */ | 
					
						
							| 
									
										
										
										
											2025-03-12 09:56:14 +08:00
										 |  |  | /** @typedef {import("./NormalModuleFactory").ResolveData} ResolveData */ | 
					
						
							| 
									
										
										
										
											2024-03-12 00:33:52 +08:00
										 |  |  | /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-12 09:56:14 +08:00
										 |  |  | /** @typedef {(resolveData: ResolveData) => void} ModuleReplacer */ | 
					
						
							| 
									
										
										
										
											2018-11-03 04:05:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-23 20:03:37 +08:00
										 |  |  | const PLUGIN_NAME = "NormalModuleReplacementPlugin"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2025-03-12 09:56:14 +08:00
										 |  |  | 	 * @param {string | ModuleReplacer} newResource the resource replacement | 
					
						
							| 
									
										
										
										
											2018-11-03 04:05:46 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2025-04-23 20:03:37 +08:00
										 |  |  | 		compiler.hooks.normalModuleFactory.tap(PLUGIN_NAME, nmf => { | 
					
						
							|  |  |  | 			nmf.hooks.beforeResolve.tap(PLUGIN_NAME, result => { | 
					
						
							|  |  |  | 				if (resourceRegExp.test(result.request)) { | 
					
						
							|  |  |  | 					if (typeof newResource === "function") { | 
					
						
							|  |  |  | 						newResource(result); | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						result.request = newResource; | 
					
						
							| 
									
										
										
										
											2017-01-13 01:25:51 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2025-04-23 20:03:37 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			nmf.hooks.afterResolve.tap(PLUGIN_NAME, result => { | 
					
						
							|  |  |  | 				const createData = result.createData; | 
					
						
							|  |  |  | 				if (resourceRegExp.test(/** @type {string} */ (createData.resource))) { | 
					
						
							|  |  |  | 					if (typeof newResource === "function") { | 
					
						
							|  |  |  | 						newResource(result); | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						const fs = | 
					
						
							|  |  |  | 							/** @type {InputFileSystem} */ | 
					
						
							|  |  |  | 							(compiler.inputFileSystem); | 
					
						
							|  |  |  | 						if ( | 
					
						
							|  |  |  | 							newResource.startsWith("/") || | 
					
						
							|  |  |  | 							(newResource.length > 1 && newResource[1] === ":") | 
					
						
							|  |  |  | 						) { | 
					
						
							|  |  |  | 							createData.resource = newResource; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						} else { | 
					
						
							| 
									
										
										
										
											2025-04-23 20:03:37 +08:00
										 |  |  | 							createData.resource = join( | 
					
						
							|  |  |  | 								fs, | 
					
						
							|  |  |  | 								dirname(fs, /** @type {string} */ (createData.resource)), | 
					
						
							|  |  |  | 								newResource | 
					
						
							|  |  |  | 							); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2017-01-13 01:25:51 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2025-04-23 20:03:37 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-01-13 01:25:51 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = NormalModuleReplacementPlugin; |