| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Ivan Kopeykin @vankop | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const Dependency = require("../Dependency"); | 
					
						
							|  |  |  | const RuntimeGlobals = require("../RuntimeGlobals"); | 
					
						
							|  |  |  | const makeSerializable = require("../util/makeSerializable"); | 
					
						
							|  |  |  | const ModuleDependency = require("./ModuleDependency"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */ | 
					
						
							|  |  |  | /** @typedef {import("../AsyncDependenciesBlock")} AsyncDependenciesBlock */ | 
					
						
							| 
									
										
										
										
											2025-09-11 08:10:10 +08:00
										 |  |  | /** @typedef {import("../Dependency").ReferencedExports} ReferencedExports */ | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */ | 
					
						
							|  |  |  | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */ | 
					
						
							|  |  |  | /** @typedef {import("../Entrypoint")} Entrypoint */ | 
					
						
							|  |  |  | /** @typedef {import("../ModuleGraph")} ModuleGraph */ | 
					
						
							| 
									
										
										
										
											2023-05-22 04:31:30 +08:00
										 |  |  | /** @typedef {import("../javascript/JavascriptParser").Range} Range */ | 
					
						
							| 
									
										
										
										
											2023-04-12 03:22:51 +08:00
										 |  |  | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ | 
					
						
							|  |  |  | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | /** @typedef {import("../util/Hash")} Hash */ | 
					
						
							|  |  |  | /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class WorkerDependency extends ModuleDependency { | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {string} request request | 
					
						
							| 
									
										
										
										
											2023-05-22 04:31:30 +08:00
										 |  |  | 	 * @param {Range} range range | 
					
						
							| 
									
										
										
										
											2024-06-11 21:09:50 +08:00
										 |  |  | 	 * @param {object} workerDependencyOptions options | 
					
						
							| 
									
										
										
										
											2024-02-17 01:39:12 +08:00
										 |  |  | 	 * @param {string=} workerDependencyOptions.publicPath public path for the worker | 
					
						
							| 
									
										
										
										
											2025-03-05 05:59:19 +08:00
										 |  |  | 	 * @param {boolean=} workerDependencyOptions.needNewUrl need generate `new URL(...)` | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2022-10-22 07:30:43 +08:00
										 |  |  | 	constructor(request, range, workerDependencyOptions) { | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 		super(request); | 
					
						
							|  |  |  | 		this.range = range; | 
					
						
							| 
									
										
										
										
											2023-01-27 02:47:01 +08:00
										 |  |  | 		// If options are updated, don't forget to update the hash and serialization functions
 | 
					
						
							| 
									
										
										
										
											2022-10-22 07:30:43 +08:00
										 |  |  | 		this.options = workerDependencyOptions; | 
					
						
							| 
									
										
										
										
											2023-01-27 02:47:01 +08:00
										 |  |  | 		/** Cache the hash */ | 
					
						
							|  |  |  | 		this._hashUpdate = undefined; | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Returns list of exports referenced by this dependency | 
					
						
							|  |  |  | 	 * @param {ModuleGraph} moduleGraph module graph | 
					
						
							|  |  |  | 	 * @param {RuntimeSpec} runtime the runtime for which the module is analysed | 
					
						
							| 
									
										
										
										
											2025-09-11 08:10:10 +08:00
										 |  |  | 	 * @returns {ReferencedExports} referenced exports | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 	 */ | 
					
						
							|  |  |  | 	getReferencedExports(moduleGraph, runtime) { | 
					
						
							|  |  |  | 		return Dependency.NO_EXPORTS_REFERENCED; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	get type() { | 
					
						
							|  |  |  | 		return "new Worker()"; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	get category() { | 
					
						
							| 
									
										
										
										
											2020-09-29 02:43:03 +08:00
										 |  |  | 		return "worker"; | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-01-27 02:47:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Update the hash | 
					
						
							|  |  |  | 	 * @param {Hash} hash hash to be updated | 
					
						
							| 
									
										
										
										
											2023-01-27 06:44:01 +08:00
										 |  |  | 	 * @param {UpdateHashContext} context context | 
					
						
							| 
									
										
										
										
											2023-01-27 02:47:01 +08:00
										 |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2023-01-27 06:44:01 +08:00
										 |  |  | 	updateHash(hash, context) { | 
					
						
							| 
									
										
										
										
											2023-01-27 02:47:01 +08:00
										 |  |  | 		if (this._hashUpdate === undefined) { | 
					
						
							| 
									
										
										
										
											2023-02-10 10:48:44 +08:00
										 |  |  | 			this._hashUpdate = JSON.stringify(this.options); | 
					
						
							| 
									
										
										
										
											2023-01-27 02:47:01 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		hash.update(this._hashUpdate); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-12 03:22:51 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {ObjectSerializerContext} context context | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2023-01-27 02:47:01 +08:00
										 |  |  | 	serialize(context) { | 
					
						
							|  |  |  | 		const { write } = context; | 
					
						
							| 
									
										
										
										
											2023-02-10 10:48:44 +08:00
										 |  |  | 		write(this.options); | 
					
						
							| 
									
										
										
										
											2023-01-27 02:47:01 +08:00
										 |  |  | 		super.serialize(context); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-12 03:22:51 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {ObjectDeserializerContext} context context | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2023-01-27 02:47:01 +08:00
										 |  |  | 	deserialize(context) { | 
					
						
							|  |  |  | 		const { read } = context; | 
					
						
							| 
									
										
										
										
											2023-02-10 10:48:44 +08:00
										 |  |  | 		this.options = read(); | 
					
						
							| 
									
										
										
										
											2023-01-27 02:47:01 +08:00
										 |  |  | 		super.deserialize(context); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-26 17:52:55 +08:00
										 |  |  | WorkerDependency.Template = class WorkerDependencyTemplate extends ( | 
					
						
							|  |  |  | 	ModuleDependency.Template | 
					
						
							|  |  |  | ) { | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Dependency} dependency the dependency for which the template should be applied | 
					
						
							|  |  |  | 	 * @param {ReplaceSource} source the current replace source which can be modified | 
					
						
							|  |  |  | 	 * @param {DependencyTemplateContext} templateContext the context object | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	apply(dependency, source, templateContext) { | 
					
						
							|  |  |  | 		const { chunkGraph, moduleGraph, runtimeRequirements } = templateContext; | 
					
						
							|  |  |  | 		const dep = /** @type {WorkerDependency} */ (dependency); | 
					
						
							| 
									
										
										
										
											2021-05-11 15:31:46 +08:00
										 |  |  | 		const block = /** @type {AsyncDependenciesBlock} */ ( | 
					
						
							|  |  |  | 			moduleGraph.getParentBlock(dependency) | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 		const entrypoint = /** @type {Entrypoint} */ ( | 
					
						
							|  |  |  | 			chunkGraph.getBlockChunkGroup(block) | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 		const chunk = entrypoint.getEntrypointChunk(); | 
					
						
							| 
									
										
										
										
											2023-02-10 06:51:42 +08:00
										 |  |  | 		// We use the workerPublicPath option if provided, else we fallback to the RuntimeGlobal publicPath
 | 
					
						
							|  |  |  | 		const workerImportBaseUrl = dep.options.publicPath | 
					
						
							|  |  |  | 			? `"${dep.options.publicPath}"` | 
					
						
							|  |  |  | 			: RuntimeGlobals.publicPath; | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		runtimeRequirements.add(RuntimeGlobals.publicPath); | 
					
						
							|  |  |  | 		runtimeRequirements.add(RuntimeGlobals.baseURI); | 
					
						
							|  |  |  | 		runtimeRequirements.add(RuntimeGlobals.getChunkScriptFilename); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-05 05:59:19 +08:00
										 |  |  | 		const workerImportStr = `/* worker import */ ${workerImportBaseUrl} + ${ | 
					
						
							|  |  |  | 			RuntimeGlobals.getChunkScriptFilename | 
					
						
							|  |  |  | 		}(${JSON.stringify(chunk.id)}), ${RuntimeGlobals.baseURI}`;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 		source.replace( | 
					
						
							|  |  |  | 			dep.range[0], | 
					
						
							|  |  |  | 			dep.range[1] - 1, | 
					
						
							| 
									
										
										
										
											2025-03-05 05:59:19 +08:00
										 |  |  | 			dep.options.needNewUrl ? `new URL(${workerImportStr})` : workerImportStr | 
					
						
							| 
									
										
										
										
											2020-09-08 00:02:14 +08:00
										 |  |  | 		); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | makeSerializable(WorkerDependency, "webpack/lib/dependencies/WorkerDependency"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = WorkerDependency; |