| 
									
										
										
										
											2020-06-13 20:45:37 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ModuleNotFoundError = require("../ModuleNotFoundError"); | 
					
						
							|  |  |  | const LazySet = require("../util/LazySet"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {import("../Compilation")} Compilation */ | 
					
						
							| 
									
										
										
										
											2020-06-17 02:20:46 +08:00
										 |  |  | /** @typedef {import("../ResolverFactory").ResolveOptionsWithDependencyType} ResolveOptionsWithDependencyType */ | 
					
						
							| 
									
										
										
										
											2020-06-13 20:45:37 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @template T | 
					
						
							| 
									
										
										
										
											2024-06-11 21:09:50 +08:00
										 |  |  |  * @typedef {object} MatchedConfigs | 
					
						
							| 
									
										
										
										
											2020-06-13 20:45:37 +08:00
										 |  |  |  * @property {Map<string, T>} resolved | 
					
						
							|  |  |  |  * @property {Map<string, T>} unresolved | 
					
						
							|  |  |  |  * @property {Map<string, T>} prefixed | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-17 02:20:46 +08:00
										 |  |  | /** @type {ResolveOptionsWithDependencyType} */ | 
					
						
							|  |  |  | const RESOLVE_OPTIONS = { dependencyType: "esm" }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-13 20:45:37 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @template T | 
					
						
							|  |  |  |  * @param {Compilation} compilation the compilation | 
					
						
							|  |  |  |  * @param {[string, T][]} configs to be processed configs | 
					
						
							|  |  |  |  * @returns {Promise<MatchedConfigs<T>>} resolved matchers | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2024-07-31 04:54:55 +08:00
										 |  |  | module.exports.resolveMatchedConfigs = (compilation, configs) => { | 
					
						
							| 
									
										
										
										
											2020-06-13 20:45:37 +08:00
										 |  |  | 	/** @type {Map<string, T>} */ | 
					
						
							|  |  |  | 	const resolved = new Map(); | 
					
						
							|  |  |  | 	/** @type {Map<string, T>} */ | 
					
						
							|  |  |  | 	const unresolved = new Map(); | 
					
						
							|  |  |  | 	/** @type {Map<string, T>} */ | 
					
						
							|  |  |  | 	const prefixed = new Map(); | 
					
						
							|  |  |  | 	const resolveContext = { | 
					
						
							|  |  |  | 		/** @type {LazySet<string>} */ | 
					
						
							|  |  |  | 		fileDependencies: new LazySet(), | 
					
						
							|  |  |  | 		/** @type {LazySet<string>} */ | 
					
						
							|  |  |  | 		contextDependencies: new LazySet(), | 
					
						
							|  |  |  | 		/** @type {LazySet<string>} */ | 
					
						
							|  |  |  | 		missingDependencies: new LazySet() | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2020-06-17 02:20:46 +08:00
										 |  |  | 	const resolver = compilation.resolverFactory.get("normal", RESOLVE_OPTIONS); | 
					
						
							| 
									
										
										
										
											2020-06-13 20:45:37 +08:00
										 |  |  | 	const context = compilation.compiler.context; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return Promise.all( | 
					
						
							| 
									
										
										
										
											2024-07-31 09:37:24 +08:00
										 |  |  | 		// eslint-disable-next-line array-callback-return
 | 
					
						
							| 
									
										
										
										
											2020-06-13 20:45:37 +08:00
										 |  |  | 		configs.map(([request, config]) => { | 
					
						
							|  |  |  | 			if (/^\.\.?(\/|$)/.test(request)) { | 
					
						
							|  |  |  | 				// relative request
 | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 				return new Promise((resolve) => { | 
					
						
							| 
									
										
										
										
											2020-06-13 20:45:37 +08:00
										 |  |  | 					resolver.resolve( | 
					
						
							|  |  |  | 						{}, | 
					
						
							|  |  |  | 						context, | 
					
						
							|  |  |  | 						request, | 
					
						
							|  |  |  | 						resolveContext, | 
					
						
							|  |  |  | 						(err, result) => { | 
					
						
							| 
									
										
										
										
											2020-06-17 19:22:37 +08:00
										 |  |  | 							if (err || result === false) { | 
					
						
							|  |  |  | 								err = err || new Error(`Can't resolve ${request}`); | 
					
						
							| 
									
										
										
										
											2020-06-13 20:45:37 +08:00
										 |  |  | 								compilation.errors.push( | 
					
						
							|  |  |  | 									new ModuleNotFoundError(null, err, { | 
					
						
							|  |  |  | 										name: `shared module ${request}` | 
					
						
							|  |  |  | 									}) | 
					
						
							|  |  |  | 								); | 
					
						
							| 
									
										
										
										
											2024-03-18 23:28:40 +08:00
										 |  |  | 								return resolve(null); | 
					
						
							| 
									
										
										
										
											2020-06-13 20:45:37 +08:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2023-06-13 01:24:59 +08:00
										 |  |  | 							resolved.set(/** @type {string} */ (result), config); | 
					
						
							| 
									
										
										
										
											2024-03-18 23:28:40 +08:00
										 |  |  | 							resolve(null); | 
					
						
							| 
									
										
										
										
											2020-06-13 20:45:37 +08:00
										 |  |  | 						} | 
					
						
							|  |  |  | 					); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} else if (/^(\/|[A-Za-z]:\\|\\\\)/.test(request)) { | 
					
						
							|  |  |  | 				// absolute path
 | 
					
						
							|  |  |  | 				resolved.set(request, config); | 
					
						
							|  |  |  | 			} else if (request.endsWith("/")) { | 
					
						
							|  |  |  | 				// module request prefix
 | 
					
						
							|  |  |  | 				prefixed.set(request, config); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				// module request
 | 
					
						
							|  |  |  | 				unresolved.set(request, config); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	).then(() => { | 
					
						
							|  |  |  | 		compilation.contextDependencies.addAll(resolveContext.contextDependencies); | 
					
						
							|  |  |  | 		compilation.fileDependencies.addAll(resolveContext.fileDependencies); | 
					
						
							|  |  |  | 		compilation.missingDependencies.addAll(resolveContext.missingDependencies); | 
					
						
							|  |  |  | 		return { resolved, unresolved, prefixed }; | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; |