| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const { pathToFileURL } = require("url"); | 
					
						
							|  |  |  | const AsyncDependenciesBlock = require("../AsyncDependenciesBlock"); | 
					
						
							|  |  |  | const CommentCompilationWarning = require("../CommentCompilationWarning"); | 
					
						
							| 
									
										
										
										
											2023-04-01 01:56:32 +08:00
										 |  |  | const { | 
					
						
							|  |  |  | 	JAVASCRIPT_MODULE_TYPE_AUTO, | 
					
						
							|  |  |  | 	JAVASCRIPT_MODULE_TYPE_ESM | 
					
						
							|  |  |  | } = require("../ModuleTypeConstants"); | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | const UnsupportedFeatureWarning = require("../UnsupportedFeatureWarning"); | 
					
						
							|  |  |  | const EnableChunkLoadingPlugin = require("../javascript/EnableChunkLoadingPlugin"); | 
					
						
							| 
									
										
										
										
											2020-09-25 17:11:09 +08:00
										 |  |  | const { equals } = require("../util/ArrayHelpers"); | 
					
						
							| 
									
										
										
										
											2021-04-30 17:28:04 +08:00
										 |  |  | const createHash = require("../util/createHash"); | 
					
						
							| 
									
										
										
										
											2020-10-26 20:57:44 +08:00
										 |  |  | const { contextify } = require("../util/identifier"); | 
					
						
							| 
									
										
										
										
											2021-02-16 00:15:09 +08:00
										 |  |  | const EnableWasmLoadingPlugin = require("../wasm/EnableWasmLoadingPlugin"); | 
					
						
							| 
									
										
										
										
											2021-03-12 19:09:02 +08:00
										 |  |  | const ConstDependency = require("./ConstDependency"); | 
					
						
							| 
									
										
										
										
											2021-05-10 15:33:04 +08:00
										 |  |  | const CreateScriptUrlDependency = require("./CreateScriptUrlDependency"); | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | const { | 
					
						
							|  |  |  | 	harmonySpecifierTag | 
					
						
							|  |  |  | } = require("./HarmonyImportDependencyParserPlugin"); | 
					
						
							|  |  |  | const WorkerDependency = require("./WorkerDependency"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-18 02:36:03 +08:00
										 |  |  | /** @typedef {import("estree").CallExpression} CallExpression */ | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | /** @typedef {import("estree").Expression} Expression */ | 
					
						
							| 
									
										
										
										
											2021-03-12 19:09:02 +08:00
										 |  |  | /** @typedef {import("estree").ObjectExpression} ObjectExpression */ | 
					
						
							|  |  |  | /** @typedef {import("estree").Pattern} Pattern */ | 
					
						
							|  |  |  | /** @typedef {import("estree").Property} Property */ | 
					
						
							|  |  |  | /** @typedef {import("estree").SpreadElement} SpreadElement */ | 
					
						
							| 
									
										
										
										
											2023-06-18 02:36:03 +08:00
										 |  |  | /** @typedef {import("../../declarations/WebpackOptions").ChunkLoading} ChunkLoading */ | 
					
						
							| 
									
										
										
										
											2023-05-22 08:03:05 +08:00
										 |  |  | /** @typedef {import("../../declarations/WebpackOptions").JavascriptParserOptions} JavascriptParserOptions */ | 
					
						
							| 
									
										
										
										
											2023-06-18 02:36:03 +08:00
										 |  |  | /** @typedef {import("../../declarations/WebpackOptions").OutputModule} OutputModule */ | 
					
						
							|  |  |  | /** @typedef {import("../../declarations/WebpackOptions").WasmLoading} WasmLoading */ | 
					
						
							|  |  |  | /** @typedef {import("../../declarations/WebpackOptions").WorkerPublicPath} WorkerPublicPath */ | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							| 
									
										
										
										
											2023-06-18 02:36:03 +08:00
										 |  |  | /** @typedef {import("../Dependency").DependencyLocation} DependencyLocation */ | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | /** @typedef {import("../Entrypoint").EntryOptions} EntryOptions */ | 
					
						
							| 
									
										
										
										
											2023-06-17 02:24:34 +08:00
										 |  |  | /** @typedef {import("../NormalModule")} NormalModule */ | 
					
						
							| 
									
										
										
										
											2021-04-30 17:28:04 +08:00
										 |  |  | /** @typedef {import("../Parser").ParserState} ParserState */ | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | /** @typedef {import("../javascript/BasicEvaluatedExpression")} BasicEvaluatedExpression */ | 
					
						
							|  |  |  | /** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */ | 
					
						
							| 
									
										
										
										
											2023-05-22 08:03:05 +08:00
										 |  |  | /** @typedef {import("../javascript/JavascriptParser")} Parser */ | 
					
						
							| 
									
										
										
										
											2023-06-18 02:36:03 +08:00
										 |  |  | /** @typedef {import("../javascript/JavascriptParser").Range} Range */ | 
					
						
							| 
									
										
										
										
											2024-08-15 02:38:08 +08:00
										 |  |  | /** @typedef {import("../util/createHash").Algorithm} Algorithm */ | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | /** @typedef {import("./HarmonyImportDependencyParserPlugin").HarmonySettings} HarmonySettings */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-17 02:24:34 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param {NormalModule} module module | 
					
						
							|  |  |  |  * @returns {string} url | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2024-07-31 11:31:11 +08:00
										 |  |  | const getUrl = module => pathToFileURL(module.resource).toString(); | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-18 01:04:04 +08:00
										 |  |  | const WorkerSpecifierTag = Symbol("worker specifier tag"); | 
					
						
							| 
									
										
										
										
											2023-05-18 00:59:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-25 17:11:09 +08:00
										 |  |  | const DEFAULT_SYNTAX = [ | 
					
						
							|  |  |  | 	"Worker", | 
					
						
							|  |  |  | 	"SharedWorker", | 
					
						
							|  |  |  | 	"navigator.serviceWorker.register()", | 
					
						
							|  |  |  | 	"Worker from worker_threads" | 
					
						
							|  |  |  | ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-30 17:28:04 +08:00
										 |  |  | /** @type {WeakMap<ParserState, number>} */ | 
					
						
							|  |  |  | const workerIndexMap = new WeakMap(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-01 01:56:32 +08:00
										 |  |  | const PLUGIN_NAME = "WorkerPlugin"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | class WorkerPlugin { | 
					
						
							| 
									
										
										
										
											2023-06-18 02:36:03 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2024-02-17 01:39:12 +08:00
										 |  |  | 	 * @param {ChunkLoading=} chunkLoading chunk loading | 
					
						
							|  |  |  | 	 * @param {WasmLoading=} wasmLoading wasm loading | 
					
						
							|  |  |  | 	 * @param {OutputModule=} module output module | 
					
						
							|  |  |  | 	 * @param {WorkerPublicPath=} workerPublicPath worker public path | 
					
						
							| 
									
										
										
										
											2023-06-18 02:36:03 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2022-10-22 07:30:43 +08:00
										 |  |  | 	constructor(chunkLoading, wasmLoading, module, workerPublicPath) { | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 		this._chunkLoading = chunkLoading; | 
					
						
							| 
									
										
										
										
											2021-02-16 00:15:09 +08:00
										 |  |  | 		this._wasmLoading = wasmLoading; | 
					
						
							| 
									
										
										
										
											2021-06-28 18:30:25 +08:00
										 |  |  | 		this._module = module; | 
					
						
							| 
									
										
										
										
											2022-10-22 07:30:43 +08:00
										 |  |  | 		this._workerPublicPath = workerPublicPath; | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-07-31 12:23:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Apply the plugin | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	apply(compiler) { | 
					
						
							|  |  |  | 		if (this._chunkLoading) { | 
					
						
							|  |  |  | 			new EnableChunkLoadingPlugin(this._chunkLoading).apply(compiler); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-02-16 00:15:09 +08:00
										 |  |  | 		if (this._wasmLoading) { | 
					
						
							|  |  |  | 			new EnableWasmLoadingPlugin(this._wasmLoading).apply(compiler); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-10-26 20:57:44 +08:00
										 |  |  | 		const cachedContextify = contextify.bindContextCache( | 
					
						
							|  |  |  | 			compiler.context, | 
					
						
							|  |  |  | 			compiler.root | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 		compiler.hooks.thisCompilation.tap( | 
					
						
							| 
									
										
										
										
											2023-04-01 01:56:32 +08:00
										 |  |  | 			PLUGIN_NAME, | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 			(compilation, { normalModuleFactory }) => { | 
					
						
							|  |  |  | 				compilation.dependencyFactories.set( | 
					
						
							|  |  |  | 					WorkerDependency, | 
					
						
							|  |  |  | 					normalModuleFactory | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 				compilation.dependencyTemplates.set( | 
					
						
							|  |  |  | 					WorkerDependency, | 
					
						
							|  |  |  | 					new WorkerDependency.Template() | 
					
						
							|  |  |  | 				); | 
					
						
							| 
									
										
										
										
											2021-05-10 15:33:04 +08:00
										 |  |  | 				compilation.dependencyTemplates.set( | 
					
						
							|  |  |  | 					CreateScriptUrlDependency, | 
					
						
							|  |  |  | 					new CreateScriptUrlDependency.Template() | 
					
						
							|  |  |  | 				); | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				/** | 
					
						
							|  |  |  | 				 * @param {JavascriptParser} parser the parser | 
					
						
							|  |  |  | 				 * @param {Expression} expr expression | 
					
						
							| 
									
										
										
										
											2023-06-17 02:24:34 +08:00
										 |  |  | 				 * @returns {[BasicEvaluatedExpression, [number, number]] | void} parsed | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 				 */ | 
					
						
							|  |  |  | 				const parseModuleUrl = (parser, expr) => { | 
					
						
							|  |  |  | 					if ( | 
					
						
							|  |  |  | 						expr.type !== "NewExpression" || | 
					
						
							|  |  |  | 						expr.callee.type === "Super" || | 
					
						
							|  |  |  | 						expr.arguments.length !== 2 | 
					
						
							|  |  |  | 					) | 
					
						
							|  |  |  | 						return; | 
					
						
							|  |  |  | 					const [arg1, arg2] = expr.arguments; | 
					
						
							|  |  |  | 					if (arg1.type === "SpreadElement") return; | 
					
						
							|  |  |  | 					if (arg2.type === "SpreadElement") return; | 
					
						
							|  |  |  | 					const callee = parser.evaluateExpression(expr.callee); | 
					
						
							|  |  |  | 					if (!callee.isIdentifier() || callee.identifier !== "URL") return; | 
					
						
							|  |  |  | 					const arg2Value = parser.evaluateExpression(arg2); | 
					
						
							|  |  |  | 					if ( | 
					
						
							|  |  |  | 						!arg2Value.isString() || | 
					
						
							| 
									
										
										
										
											2023-06-18 02:36:03 +08:00
										 |  |  | 						!(/** @type {string} */ (arg2Value.string).startsWith("file://")) || | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 						arg2Value.string !== getUrl(parser.state.module) | 
					
						
							|  |  |  | 					) { | 
					
						
							|  |  |  | 						return; | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2021-03-12 19:09:02 +08:00
										 |  |  | 					const arg1Value = parser.evaluateExpression(arg1); | 
					
						
							| 
									
										
										
										
											2023-06-18 02:36:03 +08:00
										 |  |  | 					return [ | 
					
						
							|  |  |  | 						arg1Value, | 
					
						
							|  |  |  | 						[ | 
					
						
							|  |  |  | 							/** @type {Range} */ (arg1.range)[0], | 
					
						
							|  |  |  | 							/** @type {Range} */ (arg2.range)[1] | 
					
						
							|  |  |  | 						] | 
					
						
							|  |  |  | 					]; | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 				}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				/** | 
					
						
							|  |  |  | 				 * @param {JavascriptParser} parser the parser | 
					
						
							| 
									
										
										
										
											2021-03-12 19:09:02 +08:00
										 |  |  | 				 * @param {ObjectExpression} expr expression | 
					
						
							|  |  |  | 				 * @returns {{ expressions: Record<string, Expression | Pattern>, otherElements: (Property | SpreadElement)[], values: Record<string, any>, spread: boolean, insertType: "comma" | "single", insertLocation: number }} parsed object | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 				 */ | 
					
						
							| 
									
										
										
										
											2021-03-12 19:09:02 +08:00
										 |  |  | 				const parseObjectExpression = (parser, expr) => { | 
					
						
							|  |  |  | 					/** @type {Record<string, any>} */ | 
					
						
							|  |  |  | 					const values = {}; | 
					
						
							|  |  |  | 					/** @type {Record<string, Expression | Pattern>} */ | 
					
						
							|  |  |  | 					const expressions = {}; | 
					
						
							|  |  |  | 					/** @type {(Property | SpreadElement)[]} */ | 
					
						
							|  |  |  | 					const otherElements = []; | 
					
						
							|  |  |  | 					let spread = false; | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 					for (const prop of expr.properties) { | 
					
						
							| 
									
										
										
										
											2021-03-12 19:09:02 +08:00
										 |  |  | 						if (prop.type === "SpreadElement") { | 
					
						
							|  |  |  | 							spread = true; | 
					
						
							|  |  |  | 						} else if ( | 
					
						
							|  |  |  | 							prop.type === "Property" && | 
					
						
							|  |  |  | 							!prop.method && | 
					
						
							|  |  |  | 							!prop.computed && | 
					
						
							|  |  |  | 							prop.key.type === "Identifier" | 
					
						
							|  |  |  | 						) { | 
					
						
							|  |  |  | 							expressions[prop.key.name] = prop.value; | 
					
						
							|  |  |  | 							if (!prop.shorthand && !prop.value.type.endsWith("Pattern")) { | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 								const value = parser.evaluateExpression( | 
					
						
							|  |  |  | 									/** @type {Expression} */ (prop.value) | 
					
						
							|  |  |  | 								); | 
					
						
							|  |  |  | 								if (value.isCompileTimeValue()) | 
					
						
							| 
									
										
										
										
											2021-03-12 19:09:02 +08:00
										 |  |  | 									values[prop.key.name] = value.asCompileTimeValue(); | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2021-03-12 19:09:02 +08:00
										 |  |  | 						} else { | 
					
						
							|  |  |  | 							otherElements.push(prop); | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2021-03-12 19:09:02 +08:00
										 |  |  | 					const insertType = expr.properties.length > 0 ? "comma" : "single"; | 
					
						
							| 
									
										
										
										
											2024-01-27 00:17:45 +08:00
										 |  |  | 					const insertLocation = /** @type {Range} */ ( | 
					
						
							|  |  |  | 						expr.properties[expr.properties.length - 1].range | 
					
						
							|  |  |  | 					)[1]; | 
					
						
							| 
									
										
										
										
											2021-03-12 19:09:02 +08:00
										 |  |  | 					return { | 
					
						
							|  |  |  | 						expressions, | 
					
						
							|  |  |  | 						otherElements, | 
					
						
							|  |  |  | 						values, | 
					
						
							|  |  |  | 						spread, | 
					
						
							|  |  |  | 						insertType, | 
					
						
							|  |  |  | 						insertLocation | 
					
						
							|  |  |  | 					}; | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 				}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				/** | 
					
						
							| 
									
										
										
										
											2023-05-22 08:03:05 +08:00
										 |  |  | 				 * @param {Parser} parser parser parser | 
					
						
							|  |  |  | 				 * @param {JavascriptParserOptions} parserOptions parserOptions | 
					
						
							|  |  |  | 				 * @returns {void} | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 				 */ | 
					
						
							|  |  |  | 				const parserPlugin = (parser, parserOptions) => { | 
					
						
							|  |  |  | 					if (parserOptions.worker === false) return; | 
					
						
							| 
									
										
										
										
											2020-09-25 17:11:09 +08:00
										 |  |  | 					const options = !Array.isArray(parserOptions.worker) | 
					
						
							|  |  |  | 						? ["..."] | 
					
						
							|  |  |  | 						: parserOptions.worker; | 
					
						
							| 
									
										
										
										
											2023-06-18 02:36:03 +08:00
										 |  |  | 					/** | 
					
						
							|  |  |  | 					 * @param {CallExpression} expr expression | 
					
						
							|  |  |  | 					 * @returns {boolean | void} true when handled | 
					
						
							|  |  |  | 					 */ | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 					const handleNewWorker = expr => { | 
					
						
							|  |  |  | 						if (expr.arguments.length === 0 || expr.arguments.length > 2) | 
					
						
							|  |  |  | 							return; | 
					
						
							|  |  |  | 						const [arg1, arg2] = expr.arguments; | 
					
						
							| 
									
										
										
										
											2021-03-12 19:09:02 +08:00
										 |  |  | 						if (arg1.type === "SpreadElement") return; | 
					
						
							|  |  |  | 						if (arg2 && arg2.type === "SpreadElement") return; | 
					
						
							|  |  |  | 						const parsedUrl = parseModuleUrl(parser, arg1); | 
					
						
							|  |  |  | 						if (!parsedUrl) return; | 
					
						
							|  |  |  | 						const [url, range] = parsedUrl; | 
					
						
							|  |  |  | 						if (!url.isString()) return; | 
					
						
							|  |  |  | 						const { | 
					
						
							|  |  |  | 							expressions, | 
					
						
							|  |  |  | 							otherElements, | 
					
						
							|  |  |  | 							values: options, | 
					
						
							|  |  |  | 							spread: hasSpreadInOptions, | 
					
						
							|  |  |  | 							insertType, | 
					
						
							|  |  |  | 							insertLocation | 
					
						
							| 
									
										
										
										
											2021-05-11 15:31:46 +08:00
										 |  |  | 						} = arg2 && arg2.type === "ObjectExpression" | 
					
						
							|  |  |  | 							? parseObjectExpression(parser, arg2) | 
					
						
							|  |  |  | 							: { | 
					
						
							| 
									
										
										
										
											2022-11-09 17:34:25 +08:00
										 |  |  | 									/** @type {Record<string, Expression | Pattern>} */ | 
					
						
							| 
									
										
										
										
											2021-05-11 15:31:46 +08:00
										 |  |  | 									expressions: {}, | 
					
						
							|  |  |  | 									otherElements: [], | 
					
						
							| 
									
										
										
										
											2022-11-09 17:34:25 +08:00
										 |  |  | 									/** @type {Record<string, any>} */ | 
					
						
							| 
									
										
										
										
											2021-05-11 15:31:46 +08:00
										 |  |  | 									values: {}, | 
					
						
							|  |  |  | 									spread: false, | 
					
						
							|  |  |  | 									insertType: arg2 ? "spread" : "argument", | 
					
						
							| 
									
										
										
										
											2023-06-18 02:36:03 +08:00
										 |  |  | 									insertLocation: arg2 | 
					
						
							|  |  |  | 										? /** @type {Range} */ (arg2.range) | 
					
						
							|  |  |  | 										: /** @type {Range} */ (arg1.range)[1] | 
					
						
							| 
									
										
										
										
											2024-07-31 05:43:19 +08:00
										 |  |  | 								}; | 
					
						
							| 
									
										
										
										
											2021-05-11 15:31:46 +08:00
										 |  |  | 						const { options: importOptions, errors: commentErrors } = | 
					
						
							| 
									
										
										
										
											2023-06-18 02:36:03 +08:00
										 |  |  | 							parser.parseCommentOptions(/** @type {Range} */ (expr.range)); | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-23 00:25:24 +08:00
										 |  |  | 						if (commentErrors) { | 
					
						
							|  |  |  | 							for (const e of commentErrors) { | 
					
						
							|  |  |  | 								const { comment } = e; | 
					
						
							|  |  |  | 								parser.state.module.addWarning( | 
					
						
							|  |  |  | 									new CommentCompilationWarning( | 
					
						
							|  |  |  | 										`Compilation error while processing magic comment(-s): /*${comment.value}*/: ${e.message}`, | 
					
						
							| 
									
										
										
										
											2024-10-16 22:42:26 +08:00
										 |  |  | 										/** @type {DependencyLocation} */ (comment.loc) | 
					
						
							| 
									
										
										
										
											2021-02-23 00:25:24 +08:00
										 |  |  | 									) | 
					
						
							|  |  |  | 								); | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2021-02-23 00:25:24 +08:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-23 00:25:24 +08:00
										 |  |  | 						/** @type {EntryOptions} */ | 
					
						
							| 
									
										
										
										
											2024-07-31 04:09:42 +08:00
										 |  |  | 						const entryOptions = {}; | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-23 00:25:24 +08:00
										 |  |  | 						if (importOptions) { | 
					
						
							|  |  |  | 							if (importOptions.webpackIgnore !== undefined) { | 
					
						
							|  |  |  | 								if (typeof importOptions.webpackIgnore !== "boolean") { | 
					
						
							|  |  |  | 									parser.state.module.addWarning( | 
					
						
							|  |  |  | 										new UnsupportedFeatureWarning( | 
					
						
							|  |  |  | 											`\`webpackIgnore\` expected a boolean, but received: ${importOptions.webpackIgnore}.`, | 
					
						
							| 
									
										
										
										
											2023-06-18 02:36:03 +08:00
										 |  |  | 											/** @type {DependencyLocation} */ (expr.loc) | 
					
						
							| 
									
										
										
										
											2021-02-23 00:25:24 +08:00
										 |  |  | 										) | 
					
						
							|  |  |  | 									); | 
					
						
							| 
									
										
										
										
											2024-07-31 04:54:55 +08:00
										 |  |  | 								} else if (importOptions.webpackIgnore) { | 
					
						
							|  |  |  | 									return false; | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 								} | 
					
						
							| 
									
										
										
										
											2021-02-23 00:25:24 +08:00
										 |  |  | 							} | 
					
						
							|  |  |  | 							if (importOptions.webpackEntryOptions !== undefined) { | 
					
						
							|  |  |  | 								if ( | 
					
						
							|  |  |  | 									typeof importOptions.webpackEntryOptions !== "object" || | 
					
						
							|  |  |  | 									importOptions.webpackEntryOptions === null | 
					
						
							|  |  |  | 								) { | 
					
						
							|  |  |  | 									parser.state.module.addWarning( | 
					
						
							|  |  |  | 										new UnsupportedFeatureWarning( | 
					
						
							|  |  |  | 											`\`webpackEntryOptions\` expected a object, but received: ${importOptions.webpackEntryOptions}.`, | 
					
						
							| 
									
										
										
										
											2023-06-18 02:36:03 +08:00
										 |  |  | 											/** @type {DependencyLocation} */ (expr.loc) | 
					
						
							| 
									
										
										
										
											2021-02-23 00:25:24 +08:00
										 |  |  | 										) | 
					
						
							|  |  |  | 									); | 
					
						
							|  |  |  | 								} else { | 
					
						
							|  |  |  | 									Object.assign( | 
					
						
							|  |  |  | 										entryOptions, | 
					
						
							|  |  |  | 										importOptions.webpackEntryOptions | 
					
						
							|  |  |  | 									); | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 								} | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2021-02-23 00:25:24 +08:00
										 |  |  | 							if (importOptions.webpackChunkName !== undefined) { | 
					
						
							|  |  |  | 								if (typeof importOptions.webpackChunkName !== "string") { | 
					
						
							|  |  |  | 									parser.state.module.addWarning( | 
					
						
							|  |  |  | 										new UnsupportedFeatureWarning( | 
					
						
							|  |  |  | 											`\`webpackChunkName\` expected a string, but received: ${importOptions.webpackChunkName}.`, | 
					
						
							| 
									
										
										
										
											2023-06-18 02:36:03 +08:00
										 |  |  | 											/** @type {DependencyLocation} */ (expr.loc) | 
					
						
							| 
									
										
										
										
											2021-02-23 00:25:24 +08:00
										 |  |  | 										) | 
					
						
							|  |  |  | 									); | 
					
						
							|  |  |  | 								} else { | 
					
						
							|  |  |  | 									entryOptions.name = importOptions.webpackChunkName; | 
					
						
							|  |  |  | 								} | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2021-02-23 00:25:24 +08:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-23 00:25:24 +08:00
										 |  |  | 						if ( | 
					
						
							|  |  |  | 							!Object.prototype.hasOwnProperty.call(entryOptions, "name") && | 
					
						
							|  |  |  | 							options && | 
					
						
							| 
									
										
										
										
											2021-03-12 19:09:02 +08:00
										 |  |  | 							typeof options.name === "string" | 
					
						
							| 
									
										
										
										
											2021-02-23 00:25:24 +08:00
										 |  |  | 						) { | 
					
						
							|  |  |  | 							entryOptions.name = options.name; | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-05 16:53:18 +08:00
										 |  |  | 						if (entryOptions.runtime === undefined) { | 
					
						
							| 
									
										
										
										
											2024-07-31 04:09:42 +08:00
										 |  |  | 							const i = workerIndexMap.get(parser.state) || 0; | 
					
						
							| 
									
										
										
										
											2021-04-30 17:28:04 +08:00
										 |  |  | 							workerIndexMap.set(parser.state, i + 1); | 
					
						
							| 
									
										
										
										
											2024-07-31 04:09:42 +08:00
										 |  |  | 							const name = `${cachedContextify( | 
					
						
							| 
									
										
										
										
											2021-02-23 00:25:24 +08:00
										 |  |  | 								parser.state.module.identifier() | 
					
						
							| 
									
										
										
										
											2021-04-30 17:28:04 +08:00
										 |  |  | 							)}|${i}`;
 | 
					
						
							| 
									
										
										
										
											2024-08-15 02:38:08 +08:00
										 |  |  | 							const hash = createHash( | 
					
						
							|  |  |  | 								/** @type {Algorithm} */ | 
					
						
							|  |  |  | 								(compilation.outputOptions.hashFunction) | 
					
						
							| 
									
										
										
										
											2021-05-11 15:31:46 +08:00
										 |  |  | 							); | 
					
						
							| 
									
										
										
										
											2024-08-15 02:38:08 +08:00
										 |  |  | 							hash.update(name); | 
					
						
							|  |  |  | 							const digest = | 
					
						
							|  |  |  | 								/** @type {string} */ | 
					
						
							|  |  |  | 								(hash.digest(compilation.outputOptions.hashDigest)); | 
					
						
							| 
									
										
										
										
											2021-04-30 17:28:04 +08:00
										 |  |  | 							entryOptions.runtime = digest.slice( | 
					
						
							|  |  |  | 								0, | 
					
						
							|  |  |  | 								compilation.outputOptions.hashDigestLength | 
					
						
							|  |  |  | 							); | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2021-02-23 00:25:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 						const block = new AsyncDependenciesBlock({ | 
					
						
							|  |  |  | 							name: entryOptions.name, | 
					
						
							|  |  |  | 							entryOptions: { | 
					
						
							|  |  |  | 								chunkLoading: this._chunkLoading, | 
					
						
							|  |  |  | 								wasmLoading: this._wasmLoading, | 
					
						
							|  |  |  | 								...entryOptions | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 						block.loc = expr.loc; | 
					
						
							| 
									
										
										
										
											2023-06-18 02:36:03 +08:00
										 |  |  | 						const dep = new WorkerDependency( | 
					
						
							|  |  |  | 							/** @type {string} */ (url.string), | 
					
						
							|  |  |  | 							range, | 
					
						
							|  |  |  | 							{ | 
					
						
							|  |  |  | 								publicPath: this._workerPublicPath | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 						dep.loc = /** @type {DependencyLocation} */ (expr.loc); | 
					
						
							| 
									
										
										
										
											2021-02-23 00:25:24 +08:00
										 |  |  | 						block.addDependency(dep); | 
					
						
							|  |  |  | 						parser.state.module.addBlock(block); | 
					
						
							| 
									
										
										
										
											2021-05-10 15:33:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-10 16:34:21 +08:00
										 |  |  | 						if (compilation.outputOptions.trustedTypes) { | 
					
						
							| 
									
										
										
										
											2021-05-10 15:33:04 +08:00
										 |  |  | 							const dep = new CreateScriptUrlDependency( | 
					
						
							| 
									
										
										
										
											2023-06-18 02:36:03 +08:00
										 |  |  | 								/** @type {Range} */ (expr.arguments[0].range) | 
					
						
							| 
									
										
										
										
											2021-05-10 15:33:04 +08:00
										 |  |  | 							); | 
					
						
							| 
									
										
										
										
											2023-06-18 02:36:03 +08:00
										 |  |  | 							dep.loc = /** @type {DependencyLocation} */ (expr.loc); | 
					
						
							| 
									
										
										
										
											2021-05-10 15:33:04 +08:00
										 |  |  | 							parser.state.module.addDependency(dep); | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2021-03-12 19:09:02 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 						if (expressions.type) { | 
					
						
							|  |  |  | 							const expr = expressions.type; | 
					
						
							|  |  |  | 							if (options.type !== false) { | 
					
						
							| 
									
										
										
										
											2021-06-28 18:30:25 +08:00
										 |  |  | 								const dep = new ConstDependency( | 
					
						
							|  |  |  | 									this._module ? '"module"' : "undefined", | 
					
						
							| 
									
										
										
										
											2023-06-18 02:36:03 +08:00
										 |  |  | 									/** @type {Range} */ (expr.range) | 
					
						
							| 
									
										
										
										
											2021-06-28 18:30:25 +08:00
										 |  |  | 								); | 
					
						
							| 
									
										
										
										
											2023-06-18 02:36:03 +08:00
										 |  |  | 								dep.loc = /** @type {DependencyLocation} */ (expr.loc); | 
					
						
							| 
									
										
										
										
											2021-03-12 19:09:02 +08:00
										 |  |  | 								parser.state.module.addPresentationalDependency(dep); | 
					
						
							| 
									
										
										
										
											2024-03-18 23:28:40 +08:00
										 |  |  | 								/** @type {TODO} */ | 
					
						
							|  |  |  | 								(expressions).type = undefined; | 
					
						
							| 
									
										
										
										
											2021-03-12 19:09:02 +08:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2021-06-28 18:30:25 +08:00
										 |  |  | 						} else if (insertType === "comma") { | 
					
						
							|  |  |  | 							if (this._module || hasSpreadInOptions) { | 
					
						
							|  |  |  | 								const dep = new ConstDependency( | 
					
						
							|  |  |  | 									`, type: ${this._module ? '"module"' : "undefined"}`, | 
					
						
							|  |  |  | 									insertLocation | 
					
						
							|  |  |  | 								); | 
					
						
							| 
									
										
										
										
											2023-06-18 02:36:03 +08:00
										 |  |  | 								dep.loc = /** @type {DependencyLocation} */ (expr.loc); | 
					
						
							| 
									
										
										
										
											2021-06-28 18:30:25 +08:00
										 |  |  | 								parser.state.module.addPresentationalDependency(dep); | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2021-03-12 19:09:02 +08:00
										 |  |  | 						} else if (insertType === "spread") { | 
					
						
							|  |  |  | 							const dep1 = new ConstDependency( | 
					
						
							|  |  |  | 								"Object.assign({}, ", | 
					
						
							| 
									
										
										
										
											2023-06-18 02:36:03 +08:00
										 |  |  | 								/** @type {Range} */ (insertLocation)[0] | 
					
						
							| 
									
										
										
										
											2021-03-12 19:09:02 +08:00
										 |  |  | 							); | 
					
						
							|  |  |  | 							const dep2 = new ConstDependency( | 
					
						
							| 
									
										
										
										
											2021-06-28 18:30:25 +08:00
										 |  |  | 								`, { type: ${this._module ? '"module"' : "undefined"} })`, | 
					
						
							| 
									
										
										
										
											2023-06-18 02:36:03 +08:00
										 |  |  | 								/** @type {Range} */ (insertLocation)[1] | 
					
						
							| 
									
										
										
										
											2021-03-12 19:09:02 +08:00
										 |  |  | 							); | 
					
						
							| 
									
										
										
										
											2023-06-18 02:36:03 +08:00
										 |  |  | 							dep1.loc = /** @type {DependencyLocation} */ (expr.loc); | 
					
						
							|  |  |  | 							dep2.loc = /** @type {DependencyLocation} */ (expr.loc); | 
					
						
							| 
									
										
										
										
											2021-03-12 19:09:02 +08:00
										 |  |  | 							parser.state.module.addPresentationalDependency(dep1); | 
					
						
							|  |  |  | 							parser.state.module.addPresentationalDependency(dep2); | 
					
						
							| 
									
										
										
										
											2024-08-02 02:36:27 +08:00
										 |  |  | 						} else if (insertType === "argument" && this._module) { | 
					
						
							|  |  |  | 							const dep = new ConstDependency( | 
					
						
							|  |  |  | 								', { type: "module" }', | 
					
						
							|  |  |  | 								insertLocation | 
					
						
							|  |  |  | 							); | 
					
						
							|  |  |  | 							dep.loc = /** @type {DependencyLocation} */ (expr.loc); | 
					
						
							|  |  |  | 							parser.state.module.addPresentationalDependency(dep); | 
					
						
							| 
									
										
										
										
											2021-03-12 19:09:02 +08:00
										 |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-10 15:33:04 +08:00
										 |  |  | 						parser.walkExpression(expr.callee); | 
					
						
							| 
									
										
										
										
											2021-03-12 19:09:02 +08:00
										 |  |  | 						for (const key of Object.keys(expressions)) { | 
					
						
							|  |  |  | 							if (expressions[key]) parser.walkExpression(expressions[key]); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						for (const prop of otherElements) { | 
					
						
							|  |  |  | 							parser.walkProperty(prop); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						if (insertType === "spread") { | 
					
						
							|  |  |  | 							parser.walkExpression(arg2); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-23 00:25:24 +08:00
										 |  |  | 						return true; | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 					}; | 
					
						
							| 
									
										
										
										
											2023-06-17 02:24:34 +08:00
										 |  |  | 					/** | 
					
						
							|  |  |  | 					 * @param {string} item item | 
					
						
							|  |  |  | 					 */ | 
					
						
							| 
									
										
										
										
											2020-09-25 17:11:09 +08:00
										 |  |  | 					const processItem = item => { | 
					
						
							| 
									
										
										
										
											2023-05-18 00:59:51 +08:00
										 |  |  | 						if ( | 
					
						
							|  |  |  | 							item.startsWith("*") && | 
					
						
							|  |  |  | 							item.includes(".") && | 
					
						
							|  |  |  | 							item.endsWith("()") | 
					
						
							|  |  |  | 						) { | 
					
						
							|  |  |  | 							const firstDot = item.indexOf("."); | 
					
						
							|  |  |  | 							const pattern = item.slice(1, firstDot); | 
					
						
							|  |  |  | 							const itemMembers = item.slice(firstDot + 1, -2); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 22:42:10 +08:00
										 |  |  | 							parser.hooks.preDeclarator.tap(PLUGIN_NAME, (decl, statement) => { | 
					
						
							|  |  |  | 								if (decl.id.type === "Identifier" && decl.id.name === pattern) { | 
					
						
							|  |  |  | 									parser.tagVariable(decl.id.name, WorkerSpecifierTag); | 
					
						
							|  |  |  | 									return true; | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 							}); | 
					
						
							| 
									
										
										
										
											2023-05-18 00:59:51 +08:00
										 |  |  | 							parser.hooks.pattern.for(pattern).tap(PLUGIN_NAME, pattern => { | 
					
						
							|  |  |  | 								parser.tagVariable(pattern.name, WorkerSpecifierTag); | 
					
						
							|  |  |  | 								return true; | 
					
						
							|  |  |  | 							}); | 
					
						
							|  |  |  | 							parser.hooks.callMemberChain | 
					
						
							|  |  |  | 								.for(WorkerSpecifierTag) | 
					
						
							|  |  |  | 								.tap(PLUGIN_NAME, (expression, members) => { | 
					
						
							|  |  |  | 									if (itemMembers !== members.join(".")) { | 
					
						
							|  |  |  | 										return; | 
					
						
							|  |  |  | 									} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 									return handleNewWorker(expression); | 
					
						
							|  |  |  | 								}); | 
					
						
							|  |  |  | 						} else if (item.endsWith("()")) { | 
					
						
							| 
									
										
										
										
											2020-09-25 17:11:09 +08:00
										 |  |  | 							parser.hooks.call | 
					
						
							|  |  |  | 								.for(item.slice(0, -2)) | 
					
						
							| 
									
										
										
										
											2023-04-01 01:56:32 +08:00
										 |  |  | 								.tap(PLUGIN_NAME, handleNewWorker); | 
					
						
							| 
									
										
										
										
											2020-09-25 17:11:09 +08:00
										 |  |  | 						} else { | 
					
						
							|  |  |  | 							const match = /^(.+?)(\(\))?\s+from\s+(.+)$/.exec(item); | 
					
						
							|  |  |  | 							if (match) { | 
					
						
							|  |  |  | 								const ids = match[1].split("."); | 
					
						
							|  |  |  | 								const call = match[2]; | 
					
						
							|  |  |  | 								const source = match[3]; | 
					
						
							|  |  |  | 								(call ? parser.hooks.call : parser.hooks.new) | 
					
						
							|  |  |  | 									.for(harmonySpecifierTag) | 
					
						
							| 
									
										
										
										
											2023-04-01 01:56:32 +08:00
										 |  |  | 									.tap(PLUGIN_NAME, expr => { | 
					
						
							| 
									
										
										
										
											2021-05-11 15:31:46 +08:00
										 |  |  | 										const settings = /** @type {HarmonySettings} */ ( | 
					
						
							|  |  |  | 											parser.currentTagData | 
					
						
							|  |  |  | 										); | 
					
						
							| 
									
										
										
										
											2020-09-25 17:11:09 +08:00
										 |  |  | 										if ( | 
					
						
							|  |  |  | 											!settings || | 
					
						
							|  |  |  | 											settings.source !== source || | 
					
						
							|  |  |  | 											!equals(settings.ids, ids) | 
					
						
							|  |  |  | 										) { | 
					
						
							|  |  |  | 											return; | 
					
						
							|  |  |  | 										} | 
					
						
							|  |  |  | 										return handleNewWorker(expr); | 
					
						
							|  |  |  | 									}); | 
					
						
							|  |  |  | 							} else { | 
					
						
							| 
									
										
										
										
											2023-04-01 01:56:32 +08:00
										 |  |  | 								parser.hooks.new.for(item).tap(PLUGIN_NAME, handleNewWorker); | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 							} | 
					
						
							| 
									
										
										
										
											2020-09-25 17:11:09 +08:00
										 |  |  | 						} | 
					
						
							|  |  |  | 					}; | 
					
						
							|  |  |  | 					for (const item of options) { | 
					
						
							|  |  |  | 						if (item === "...") { | 
					
						
							| 
									
										
										
										
											2024-08-02 02:36:27 +08:00
										 |  |  | 							for (const itemFromDefault of DEFAULT_SYNTAX) { | 
					
						
							|  |  |  | 								processItem(itemFromDefault); | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2020-09-25 17:11:09 +08:00
										 |  |  | 						} else processItem(item); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 				}; | 
					
						
							|  |  |  | 				normalModuleFactory.hooks.parser | 
					
						
							| 
									
										
										
										
											2023-04-01 01:56:32 +08:00
										 |  |  | 					.for(JAVASCRIPT_MODULE_TYPE_AUTO) | 
					
						
							|  |  |  | 					.tap(PLUGIN_NAME, parserPlugin); | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 				normalModuleFactory.hooks.parser | 
					
						
							| 
									
										
										
										
											2023-04-01 01:56:32 +08:00
										 |  |  | 					.for(JAVASCRIPT_MODULE_TYPE_ESM) | 
					
						
							|  |  |  | 					.tap(PLUGIN_NAME, parserPlugin); | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | module.exports = WorkerPlugin; |