| 
									
										
										
										
											2022-03-01 23:11:31 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Ivan Kopeykin @vankop | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-01 01:56:32 +08:00
										 |  |  | const { | 
					
						
							|  |  |  | 	JAVASCRIPT_MODULE_TYPE_AUTO, | 
					
						
							|  |  |  | 	JAVASCRIPT_MODULE_TYPE_ESM | 
					
						
							|  |  |  | } = require("../ModuleTypeConstants"); | 
					
						
							| 
									
										
										
										
											2022-03-01 23:11:31 +08:00
										 |  |  | const ContextElementDependency = require("./ContextElementDependency"); | 
					
						
							|  |  |  | const ImportMetaContextDependency = require("./ImportMetaContextDependency"); | 
					
						
							|  |  |  | const ImportMetaContextDependencyParserPlugin = require("./ImportMetaContextDependencyParserPlugin"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-22 08:03:05 +08:00
										 |  |  | /** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */ | 
					
						
							| 
									
										
										
										
											2022-03-01 23:11:31 +08:00
										 |  |  | /** @typedef {import("../../declarations/WebpackOptions").ResolveOptions} ResolveOptions */ | 
					
						
							|  |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							| 
									
										
										
										
											2023-05-22 08:03:05 +08:00
										 |  |  | /** @typedef {import("../javascript/JavascriptParser")} Parser */ | 
					
						
							| 
									
										
										
										
											2022-03-01 23:11:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-01 01:56:32 +08:00
										 |  |  | const PLUGIN_NAME = "ImportMetaContextPlugin"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-01 23:11:31 +08:00
										 |  |  | class ImportMetaContextPlugin { | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Apply the plugin | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							|  |  |  | 		compiler.hooks.compilation.tap( | 
					
						
							| 
									
										
										
										
											2023-04-01 01:56:32 +08:00
										 |  |  | 			PLUGIN_NAME, | 
					
						
							| 
									
										
										
										
											2022-03-01 23:11:31 +08:00
										 |  |  | 			(compilation, { contextModuleFactory, normalModuleFactory }) => { | 
					
						
							|  |  |  | 				compilation.dependencyFactories.set( | 
					
						
							|  |  |  | 					ImportMetaContextDependency, | 
					
						
							|  |  |  | 					contextModuleFactory | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 				compilation.dependencyTemplates.set( | 
					
						
							|  |  |  | 					ImportMetaContextDependency, | 
					
						
							|  |  |  | 					new ImportMetaContextDependency.Template() | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 				compilation.dependencyFactories.set( | 
					
						
							|  |  |  | 					ContextElementDependency, | 
					
						
							|  |  |  | 					normalModuleFactory | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-22 08:03:05 +08:00
										 |  |  | 				/** | 
					
						
							|  |  |  | 				 * @param {Parser} parser parser parser | 
					
						
							|  |  |  | 				 * @param {JavascriptParserOptions} parserOptions parserOptions | 
					
						
							|  |  |  | 				 * @returns {void} | 
					
						
							|  |  |  | 				 */ | 
					
						
							| 
									
										
										
										
											2022-03-01 23:11:31 +08:00
										 |  |  | 				const handler = (parser, parserOptions) => { | 
					
						
							|  |  |  | 					if ( | 
					
						
							|  |  |  | 						parserOptions.importMetaContext !== undefined && | 
					
						
							|  |  |  | 						!parserOptions.importMetaContext | 
					
						
							|  |  |  | 					) | 
					
						
							|  |  |  | 						return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					new ImportMetaContextDependencyParserPlugin().apply(parser); | 
					
						
							|  |  |  | 				}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				normalModuleFactory.hooks.parser | 
					
						
							| 
									
										
										
										
											2023-04-01 01:56:32 +08:00
										 |  |  | 					.for(JAVASCRIPT_MODULE_TYPE_AUTO) | 
					
						
							|  |  |  | 					.tap(PLUGIN_NAME, handler); | 
					
						
							| 
									
										
										
										
											2022-03-01 23:11:31 +08:00
										 |  |  | 				normalModuleFactory.hooks.parser | 
					
						
							| 
									
										
										
										
											2023-04-01 01:56:32 +08:00
										 |  |  | 					.for(JAVASCRIPT_MODULE_TYPE_ESM) | 
					
						
							|  |  |  | 					.tap(PLUGIN_NAME, handler); | 
					
						
							| 
									
										
										
										
											2022-03-01 23:11:31 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = ImportMetaContextPlugin; |