| 
									
										
										
										
											2015-05-13 06:17:06 +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-02-23 01:17:40 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const DelegatedModule = require("./DelegatedModule"); | 
					
						
							| 
									
										
										
										
											2015-05-13 06:17:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | /** @typedef {import("../declarations/plugins/DllReferencePlugin").DllReferencePluginOptions} DllReferencePluginOptions */ | 
					
						
							|  |  |  | /** @typedef {import("../declarations/plugins/DllReferencePlugin").DllReferencePluginOptionsContent} DllReferencePluginOptionsContent */ | 
					
						
							|  |  |  | /** @typedef {import("./DelegatedModule").Data} Data */ | 
					
						
							|  |  |  | /** @typedef {import("./DelegatedModule").SourceRequest} SourceRequest */ | 
					
						
							|  |  |  | /** @typedef {import("./DelegatedModule").Type} Type */ | 
					
						
							| 
									
										
										
										
											2023-06-17 01:13:03 +08:00
										 |  |  | /** @typedef {import("./NormalModuleFactory")} NormalModuleFactory */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @typedef {object} Options | 
					
						
							|  |  |  |  * @property {SourceRequest} source source | 
					
						
							|  |  |  |  * @property {NonNullable<DllReferencePluginOptions["context"]>} context absolute context path to which lib ident is relative to | 
					
						
							|  |  |  |  * @property {DllReferencePluginOptionsContent} content content | 
					
						
							|  |  |  |  * @property {DllReferencePluginOptions["type"]} type type | 
					
						
							|  |  |  |  * @property {DllReferencePluginOptions["extensions"]} extensions extensions | 
					
						
							|  |  |  |  * @property {DllReferencePluginOptions["scope"]} scope scope | 
					
						
							|  |  |  |  * @property {object=} associatedObjectForCache object for caching | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 01:17:40 +08:00
										 |  |  | class DelegatedModuleFactoryPlugin { | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Options} options options | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-02-23 01:17:40 +08:00
										 |  |  | 	constructor(options) { | 
					
						
							|  |  |  | 		this.options = options; | 
					
						
							|  |  |  | 		options.type = options.type || "require"; | 
					
						
							| 
									
										
										
										
											2020-09-02 20:41:04 +08:00
										 |  |  | 		options.extensions = options.extensions || ["", ".js", ".json", ".wasm"]; | 
					
						
							| 
									
										
										
										
											2017-02-23 01:17:40 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-05-13 06:17:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-17 01:13:03 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {NormalModuleFactory} normalModuleFactory the normal module factory | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-02-23 01:17:40 +08:00
										 |  |  | 	apply(normalModuleFactory) { | 
					
						
							|  |  |  | 		const scope = this.options.scope; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if (scope) { | 
					
						
							| 
									
										
										
										
											2019-01-05 02:17:37 +08:00
										 |  |  | 			normalModuleFactory.hooks.factorize.tapAsync( | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				"DelegatedModuleFactoryPlugin", | 
					
						
							| 
									
										
										
										
											2019-01-05 02:17:37 +08:00
										 |  |  | 				(data, callback) => { | 
					
						
							| 
									
										
										
										
											2020-01-09 01:56:54 +08:00
										 |  |  | 					const [dependency] = data.dependencies; | 
					
						
							| 
									
										
										
										
											2020-01-09 02:03:04 +08:00
										 |  |  | 					const { request } = dependency; | 
					
						
							| 
									
										
										
										
											2020-01-09 02:04:41 +08:00
										 |  |  | 					if (request && request.startsWith(`${scope}/`)) { | 
					
						
							| 
									
										
										
										
											2024-07-31 10:39:30 +08:00
										 |  |  | 						const innerRequest = `.${request.slice(scope.length)}`; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 						let resolved; | 
					
						
							|  |  |  | 						if (innerRequest in this.options.content) { | 
					
						
							|  |  |  | 							resolved = this.options.content[innerRequest]; | 
					
						
							|  |  |  | 							return callback( | 
					
						
							|  |  |  | 								null, | 
					
						
							|  |  |  | 								new DelegatedModule( | 
					
						
							|  |  |  | 									this.options.source, | 
					
						
							|  |  |  | 									resolved, | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 									/** @type {Type} */ (this.options.type), | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 									innerRequest, | 
					
						
							|  |  |  | 									request | 
					
						
							|  |  |  | 								) | 
					
						
							|  |  |  | 							); | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 						const extensions = | 
					
						
							|  |  |  | 							/** @type {string[]} */ | 
					
						
							|  |  |  | 							(this.options.extensions); | 
					
						
							|  |  |  | 						for (let i = 0; i < extensions.length; i++) { | 
					
						
							|  |  |  | 							const extension = extensions[i]; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 							const requestPlusExt = innerRequest + extension; | 
					
						
							|  |  |  | 							if (requestPlusExt in this.options.content) { | 
					
						
							|  |  |  | 								resolved = this.options.content[requestPlusExt]; | 
					
						
							|  |  |  | 								return callback( | 
					
						
							|  |  |  | 									null, | 
					
						
							|  |  |  | 									new DelegatedModule( | 
					
						
							|  |  |  | 										this.options.source, | 
					
						
							|  |  |  | 										resolved, | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 										/** @type {Type} */ (this.options.type), | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 										requestPlusExt, | 
					
						
							|  |  |  | 										request + extension | 
					
						
							|  |  |  | 									) | 
					
						
							|  |  |  | 								); | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2015-05-18 05:03:21 +08:00
										 |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2019-01-05 02:17:37 +08:00
										 |  |  | 					return callback(); | 
					
						
							| 
									
										
										
										
											2017-02-23 15:43:44 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			); | 
					
						
							| 
									
										
										
										
											2017-02-23 01:17:40 +08:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			normalModuleFactory.hooks.module.tap( | 
					
						
							|  |  |  | 				"DelegatedModuleFactoryPlugin", | 
					
						
							|  |  |  | 				module => { | 
					
						
							| 
									
										
										
										
											2018-07-20 22:24:35 +08:00
										 |  |  | 					const request = module.libIdent(this.options); | 
					
						
							| 
									
										
										
										
											2024-08-02 02:36:27 +08:00
										 |  |  | 					if (request && request in this.options.content) { | 
					
						
							|  |  |  | 						const resolved = this.options.content[request]; | 
					
						
							|  |  |  | 						return new DelegatedModule( | 
					
						
							|  |  |  | 							this.options.source, | 
					
						
							|  |  |  | 							resolved, | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 							/** @type {Type} */ (this.options.type), | 
					
						
							| 
									
										
										
										
											2024-08-02 02:36:27 +08:00
										 |  |  | 							request, | 
					
						
							|  |  |  | 							module | 
					
						
							|  |  |  | 						); | 
					
						
							| 
									
										
										
										
											2017-02-23 01:17:40 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					return module; | 
					
						
							| 
									
										
										
										
											2015-05-18 05:03:21 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			); | 
					
						
							| 
									
										
										
										
											2017-02-23 01:17:40 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-05-18 05:03:21 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-02-23 01:17:40 +08:00
										 |  |  | } | 
					
						
							|  |  |  | module.exports = DelegatedModuleFactoryPlugin; |