| 
									
										
										
										
											2020-08-05 05:42:29 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Ivan Kopeykin @vankop | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-10 08:33:58 +08:00
										 |  |  | const InitFragment = require("../InitFragment"); | 
					
						
							| 
									
										
										
										
											2020-08-05 05:42:29 +08:00
										 |  |  | const RuntimeGlobals = require("../RuntimeGlobals"); | 
					
						
							| 
									
										
										
										
											2024-02-28 23:23:23 +08:00
										 |  |  | const RawDataUrlModule = require("../asset/RawDataUrlModule"); | 
					
						
							| 
									
										
										
										
											2021-01-27 20:32:09 +08:00
										 |  |  | const { | 
					
						
							|  |  |  | 	getDependencyUsedByExportsCondition | 
					
						
							|  |  |  | } = require("../optimize/InnerGraph"); | 
					
						
							| 
									
										
										
										
											2020-08-07 01:00:22 +08:00
										 |  |  | const makeSerializable = require("../util/makeSerializable"); | 
					
						
							| 
									
										
										
										
											2021-03-11 22:04:53 +08:00
										 |  |  | const memoize = require("../util/memoize"); | 
					
						
							| 
									
										
										
										
											2020-08-05 05:42:29 +08:00
										 |  |  | const ModuleDependency = require("./ModuleDependency"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */ | 
					
						
							|  |  |  | /** @typedef {import("../Dependency")} Dependency */ | 
					
						
							| 
									
										
										
										
											2024-02-22 22:36:36 +08:00
										 |  |  | /** @typedef {import("../Dependency").GetConditionFn} GetConditionFn */ | 
					
						
							| 
									
										
										
										
											2020-08-05 05:42:29 +08:00
										 |  |  | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */ | 
					
						
							| 
									
										
										
										
											2021-03-11 22:04:53 +08:00
										 |  |  | /** @typedef {import("../Module")} Module */ | 
					
						
							| 
									
										
										
										
											2020-08-05 05:42:29 +08:00
										 |  |  | /** @typedef {import("../ModuleGraph")} ModuleGraph */ | 
					
						
							| 
									
										
										
										
											2023-05-22 04:31:30 +08:00
										 |  |  | /** @typedef {import("../javascript/JavascriptParser").Range} Range */ | 
					
						
							| 
									
										
										
										
											2025-08-28 18:34:30 +08:00
										 |  |  | /** @typedef {import("../optimize/InnerGraph").UsedByExports} UsedByExports */ | 
					
						
							| 
									
										
										
										
											2023-04-12 03:22:51 +08:00
										 |  |  | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ | 
					
						
							|  |  |  | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ | 
					
						
							| 
									
										
										
										
											2020-08-05 05:42:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-31 11:31:11 +08:00
										 |  |  | const getIgnoredRawDataUrlModule = memoize( | 
					
						
							| 
									
										
										
										
											2024-07-31 12:23:44 +08:00
										 |  |  | 	() => new RawDataUrlModule("data:,", "ignored-asset", "(ignored asset)") | 
					
						
							| 
									
										
										
										
											2024-07-31 11:31:11 +08:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2021-03-11 22:04:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-05 05:42:29 +08:00
										 |  |  | class URLDependency extends ModuleDependency { | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {string} request request | 
					
						
							| 
									
										
										
										
											2023-05-22 04:31:30 +08:00
										 |  |  | 	 * @param {Range} range range of the arguments of new URL( |> ... <| ) | 
					
						
							|  |  |  | 	 * @param {Range} outerRange range of the full |> new URL(...) <| | 
					
						
							| 
									
										
										
										
											2021-02-16 23:05:47 +08:00
										 |  |  | 	 * @param {boolean=} relative use relative urls instead of absolute with base uri | 
					
						
							| 
									
										
										
										
											2020-08-05 05:42:29 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2021-02-16 23:05:47 +08:00
										 |  |  | 	constructor(request, range, outerRange, relative) { | 
					
						
							| 
									
										
										
										
											2020-08-05 05:42:29 +08:00
										 |  |  | 		super(request); | 
					
						
							|  |  |  | 		this.range = range; | 
					
						
							| 
									
										
										
										
											2021-01-19 20:31:55 +08:00
										 |  |  | 		this.outerRange = outerRange; | 
					
						
							| 
									
										
										
										
											2021-02-16 23:05:47 +08:00
										 |  |  | 		this.relative = relative || false; | 
					
						
							| 
									
										
										
										
											2025-08-28 18:34:30 +08:00
										 |  |  | 		/** @type {UsedByExports | undefined} */ | 
					
						
							| 
									
										
										
										
											2021-01-19 20:31:55 +08:00
										 |  |  | 		this.usedByExports = undefined; | 
					
						
							| 
									
										
										
										
											2025-07-27 14:14:03 +08:00
										 |  |  | 		this.prefetch = undefined; | 
					
						
							|  |  |  | 		this.preload = undefined; | 
					
						
							|  |  |  | 		this.fetchPriority = undefined; | 
					
						
							| 
									
										
										
										
											2025-09-15 21:24:36 +08:00
										 |  |  | 		/** @type {string|undefined} */ | 
					
						
							|  |  |  | 		this.preloadAs = undefined; | 
					
						
							|  |  |  | 		/** @type {string|undefined} */ | 
					
						
							|  |  |  | 		this.preloadType = undefined; | 
					
						
							|  |  |  | 		/** @type {string|undefined} */ | 
					
						
							|  |  |  | 		this.preloadMedia = undefined; | 
					
						
							| 
									
										
										
										
											2020-08-05 05:42:29 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	get type() { | 
					
						
							|  |  |  | 		return "new URL()"; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	get category() { | 
					
						
							|  |  |  | 		return "url"; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-01-19 20:31:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {ModuleGraph} moduleGraph module graph | 
					
						
							| 
									
										
										
										
											2024-02-22 22:20:17 +08:00
										 |  |  | 	 * @returns {null | false | GetConditionFn} function to determine if the connection is active | 
					
						
							| 
									
										
										
										
											2021-01-19 20:31:55 +08:00
										 |  |  | 	 */ | 
					
						
							|  |  |  | 	getCondition(moduleGraph) { | 
					
						
							| 
									
										
										
										
											2021-01-27 20:32:09 +08:00
										 |  |  | 		return getDependencyUsedByExportsCondition( | 
					
						
							|  |  |  | 			this, | 
					
						
							|  |  |  | 			this.usedByExports, | 
					
						
							|  |  |  | 			moduleGraph | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2021-01-19 20:31:55 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-11 22:04:53 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {string} context context directory | 
					
						
							| 
									
										
										
										
											2025-05-13 21:03:58 +08:00
										 |  |  | 	 * @returns {Module} ignored module | 
					
						
							| 
									
										
										
										
											2021-03-11 22:04:53 +08:00
										 |  |  | 	 */ | 
					
						
							|  |  |  | 	createIgnoredModule(context) { | 
					
						
							| 
									
										
										
										
											2024-02-28 23:08:34 +08:00
										 |  |  | 		return getIgnoredRawDataUrlModule(); | 
					
						
							| 
									
										
										
										
											2021-03-11 22:04:53 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-12 03:22:51 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {ObjectSerializerContext} context context | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2021-01-19 20:31:55 +08:00
										 |  |  | 	serialize(context) { | 
					
						
							|  |  |  | 		const { write } = context; | 
					
						
							|  |  |  | 		write(this.outerRange); | 
					
						
							| 
									
										
										
										
											2021-02-16 23:05:47 +08:00
										 |  |  | 		write(this.relative); | 
					
						
							| 
									
										
										
										
											2021-01-19 20:31:55 +08:00
										 |  |  | 		write(this.usedByExports); | 
					
						
							| 
									
										
										
										
											2025-07-21 13:58:28 +08:00
										 |  |  | 		write(this.prefetch); | 
					
						
							|  |  |  | 		write(this.preload); | 
					
						
							|  |  |  | 		write(this.fetchPriority); | 
					
						
							| 
									
										
										
										
											2025-09-15 21:24:36 +08:00
										 |  |  | 		write(this.preloadAs); | 
					
						
							|  |  |  | 		write(this.preloadType); | 
					
						
							|  |  |  | 		write(this.preloadMedia); | 
					
						
							| 
									
										
										
										
											2021-01-19 20:31:55 +08:00
										 |  |  | 		super.serialize(context); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-12 03:22:51 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {ObjectDeserializerContext} context context | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2021-01-19 20:31:55 +08:00
										 |  |  | 	deserialize(context) { | 
					
						
							|  |  |  | 		const { read } = context; | 
					
						
							|  |  |  | 		this.outerRange = read(); | 
					
						
							| 
									
										
										
										
											2021-02-16 23:05:47 +08:00
										 |  |  | 		this.relative = read(); | 
					
						
							| 
									
										
										
										
											2021-01-19 20:31:55 +08:00
										 |  |  | 		this.usedByExports = read(); | 
					
						
							| 
									
										
										
										
											2025-07-21 13:58:28 +08:00
										 |  |  | 		this.prefetch = read(); | 
					
						
							|  |  |  | 		this.preload = read(); | 
					
						
							|  |  |  | 		this.fetchPriority = read(); | 
					
						
							| 
									
										
										
										
											2025-09-15 21:24:36 +08:00
										 |  |  | 		this.preloadAs = read(); | 
					
						
							|  |  |  | 		this.preloadType = read(); | 
					
						
							|  |  |  | 		this.preloadMedia = read(); | 
					
						
							| 
									
										
										
										
											2021-01-19 20:31:55 +08:00
										 |  |  | 		super.deserialize(context); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-08-05 05:42:29 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-26 17:52:55 +08:00
										 |  |  | URLDependency.Template = class URLDependencyTemplate extends ( | 
					
						
							|  |  |  | 	ModuleDependency.Template | 
					
						
							|  |  |  | ) { | 
					
						
							| 
									
										
										
										
											2025-09-03 09:02:44 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Determines the 'as' attribute value for prefetch/preload based on file extension | 
					
						
							|  |  |  | 	 * https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/rel/preload#what_types_of_content_can_be_preloaded
 | 
					
						
							|  |  |  | 	 * @param {string} request module request string or filename | 
					
						
							|  |  |  | 	 * @returns {string} asset type for link element 'as' attribute | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	static _getAssetType(request) { | 
					
						
							|  |  |  | 		if (/\.(png|jpe?g|gif|svg|webp|avif|bmp|ico|tiff?)$/i.test(request)) { | 
					
						
							|  |  |  | 			return "image"; | 
					
						
							|  |  |  | 		} else if (/\.(woff2?|ttf|otf|eot)$/i.test(request)) { | 
					
						
							|  |  |  | 			return "font"; | 
					
						
							|  |  |  | 		} else if (/\.(js|mjs|jsx|ts|tsx)$/i.test(request)) { | 
					
						
							|  |  |  | 			return "script"; | 
					
						
							|  |  |  | 		} else if (/\.css$/i.test(request)) { | 
					
						
							|  |  |  | 			return "style"; | 
					
						
							|  |  |  | 		} else if (/\.vtt$/i.test(request)) { | 
					
						
							|  |  |  | 			return "track"; | 
					
						
							|  |  |  | 		} else if ( | 
					
						
							|  |  |  | 			/\.(mp4|webm|ogg|mp3|wav|flac|aac|m4a|avi|mov|wmv|mkv)$/i.test(request) | 
					
						
							|  |  |  | 		) { | 
					
						
							|  |  |  | 			// Audio/video files use 'fetch' as browser support varies
 | 
					
						
							|  |  |  | 			return "fetch"; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return "fetch"; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-05 05:42:29 +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, | 
					
						
							| 
									
										
										
										
											2021-01-19 20:31:55 +08:00
										 |  |  | 			runtimeTemplate, | 
					
						
							| 
									
										
										
										
											2025-08-10 08:33:58 +08:00
										 |  |  | 			runtime, | 
					
						
							|  |  |  | 			initFragments | 
					
						
							| 
									
										
										
										
											2020-08-05 05:42:29 +08:00
										 |  |  | 		} = templateContext; | 
					
						
							|  |  |  | 		const dep = /** @type {URLDependency} */ (dependency); | 
					
						
							| 
									
										
										
										
											2025-08-10 08:33:58 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		const module = moduleGraph.getModule(dep); | 
					
						
							| 
									
										
										
										
											2021-01-19 20:31:55 +08:00
										 |  |  | 		const connection = moduleGraph.getConnection(dep); | 
					
						
							|  |  |  | 		// Skip rendering depending when dependency is conditional
 | 
					
						
							|  |  |  | 		if (connection && !connection.isTargetActive(runtime)) { | 
					
						
							|  |  |  | 			source.replace( | 
					
						
							|  |  |  | 				dep.outerRange[0], | 
					
						
							|  |  |  | 				dep.outerRange[1] - 1, | 
					
						
							|  |  |  | 				"/* unused asset import */ undefined" | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-08-05 05:42:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-10 08:33:58 +08:00
										 |  |  | 		// Standard URL generation
 | 
					
						
							| 
									
										
										
										
											2021-02-16 23:05:47 +08:00
										 |  |  | 		if (dep.relative) { | 
					
						
							|  |  |  | 			runtimeRequirements.add(RuntimeGlobals.relativeUrl); | 
					
						
							|  |  |  | 			source.replace( | 
					
						
							|  |  |  | 				dep.outerRange[0], | 
					
						
							|  |  |  | 				dep.outerRange[1] - 1, | 
					
						
							| 
									
										
										
										
											2025-08-10 08:33:58 +08:00
										 |  |  | 				`/* asset import */ new ${RuntimeGlobals.relativeUrl}(${runtimeTemplate.moduleRaw( | 
					
						
							|  |  |  | 					{ | 
					
						
							|  |  |  | 						chunkGraph, | 
					
						
							|  |  |  | 						module, | 
					
						
							|  |  |  | 						request: dep.request, | 
					
						
							|  |  |  | 						runtimeRequirements, | 
					
						
							|  |  |  | 						weak: false | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				)})`
 | 
					
						
							| 
									
										
										
										
											2021-02-16 23:05:47 +08:00
										 |  |  | 			); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			runtimeRequirements.add(RuntimeGlobals.baseURI); | 
					
						
							|  |  |  | 			source.replace( | 
					
						
							|  |  |  | 				dep.range[0], | 
					
						
							|  |  |  | 				dep.range[1] - 1, | 
					
						
							|  |  |  | 				`/* asset import */ ${runtimeTemplate.moduleRaw({ | 
					
						
							|  |  |  | 					chunkGraph, | 
					
						
							| 
									
										
										
										
											2025-08-10 08:33:58 +08:00
										 |  |  | 					module, | 
					
						
							| 
									
										
										
										
											2021-02-16 23:05:47 +08:00
										 |  |  | 					request: dep.request, | 
					
						
							|  |  |  | 					runtimeRequirements, | 
					
						
							|  |  |  | 					weak: false | 
					
						
							|  |  |  | 				})}, ${RuntimeGlobals.baseURI}`
 | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2025-08-10 08:33:58 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Prefetch/Preload via InitFragment
 | 
					
						
							|  |  |  | 		if ((dep.prefetch || dep.preload) && module) { | 
					
						
							|  |  |  | 			const request = dep.request; | 
					
						
							| 
									
										
										
										
											2025-09-15 21:24:36 +08:00
										 |  |  | 			const detectedAssetType = URLDependencyTemplate._getAssetType(request); | 
					
						
							| 
									
										
										
										
											2025-08-10 08:33:58 +08:00
										 |  |  | 			const id = chunkGraph.getModuleId(module); | 
					
						
							|  |  |  | 			if (id !== null) { | 
					
						
							|  |  |  | 				const moduleId = runtimeTemplate.moduleId({ | 
					
						
							|  |  |  | 					module, | 
					
						
							|  |  |  | 					chunkGraph, | 
					
						
							|  |  |  | 					request: dep.request, | 
					
						
							|  |  |  | 					weak: false | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (dep.preload) { | 
					
						
							|  |  |  | 					runtimeRequirements.add(RuntimeGlobals.preloadAsset); | 
					
						
							| 
									
										
										
										
											2025-09-15 21:24:36 +08:00
										 |  |  | 					const asArg = JSON.stringify(dep.preloadAs || detectedAssetType); | 
					
						
							|  |  |  | 					const fetchPriorityArg = dep.fetchPriority | 
					
						
							|  |  |  | 						? JSON.stringify(dep.fetchPriority) | 
					
						
							|  |  |  | 						: "undefined"; | 
					
						
							|  |  |  | 					const typeArg = dep.preloadType | 
					
						
							|  |  |  | 						? JSON.stringify(dep.preloadType) | 
					
						
							|  |  |  | 						: "undefined"; | 
					
						
							|  |  |  | 					const mediaArg = dep.preloadMedia | 
					
						
							|  |  |  | 						? JSON.stringify(dep.preloadMedia) | 
					
						
							|  |  |  | 						: "undefined"; | 
					
						
							| 
									
										
										
										
											2025-08-10 08:33:58 +08:00
										 |  |  | 					initFragments.push( | 
					
						
							|  |  |  | 						new InitFragment( | 
					
						
							| 
									
										
										
										
											2025-09-15 21:24:36 +08:00
										 |  |  | 							`${RuntimeGlobals.preloadAsset}(${moduleId}, ${asArg}, ${fetchPriorityArg}, ${typeArg}, ${mediaArg}, ${dep.relative});\n`, | 
					
						
							| 
									
										
										
										
											2025-08-10 08:33:58 +08:00
										 |  |  | 							InitFragment.STAGE_CONSTANTS, | 
					
						
							|  |  |  | 							-10, | 
					
						
							|  |  |  | 							`asset_preload_${moduleId}` | 
					
						
							|  |  |  | 						) | 
					
						
							|  |  |  | 					); | 
					
						
							|  |  |  | 				} else if (dep.prefetch) { | 
					
						
							|  |  |  | 					runtimeRequirements.add(RuntimeGlobals.prefetchAsset); | 
					
						
							| 
									
										
										
										
											2025-09-15 21:24:36 +08:00
										 |  |  | 					const asArg = JSON.stringify(detectedAssetType); | 
					
						
							|  |  |  | 					const fetchPriorityArg = dep.fetchPriority | 
					
						
							|  |  |  | 						? JSON.stringify(dep.fetchPriority) | 
					
						
							|  |  |  | 						: "undefined"; | 
					
						
							| 
									
										
										
										
											2025-08-10 08:33:58 +08:00
										 |  |  | 					initFragments.push( | 
					
						
							|  |  |  | 						new InitFragment( | 
					
						
							| 
									
										
										
										
											2025-09-15 21:24:36 +08:00
										 |  |  | 							`${RuntimeGlobals.prefetchAsset}(${moduleId}, ${asArg}, ${fetchPriorityArg}, undefined, undefined, ${dep.relative});\n`, | 
					
						
							| 
									
										
										
										
											2025-08-10 08:33:58 +08:00
										 |  |  | 							InitFragment.STAGE_CONSTANTS, | 
					
						
							|  |  |  | 							-5, | 
					
						
							|  |  |  | 							`asset_prefetch_${moduleId}` | 
					
						
							|  |  |  | 						) | 
					
						
							|  |  |  | 					); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-08-05 05:42:29 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-07 01:00:22 +08:00
										 |  |  | makeSerializable(URLDependency, "webpack/lib/dependencies/URLDependency"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-05 05:42:29 +08:00
										 |  |  | module.exports = URLDependency; |