| 
									
										
										
										
											2019-03-31 22:12:19 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							| 
									
										
										
										
											2019-11-19 22:36:11 +08:00
										 |  |  | 	Author Sergey Melyukov @smelukov | 
					
						
							| 
									
										
										
										
											2019-03-31 22:12:19 +08:00
										 |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-25 20:03:21 +08:00
										 |  |  | const mimeTypes = require("mime-types"); | 
					
						
							| 
									
										
										
										
											2019-11-19 22:36:11 +08:00
										 |  |  | const path = require("path"); | 
					
						
							| 
									
										
										
										
											2019-11-18 21:29:19 +08:00
										 |  |  | const { RawSource } = require("webpack-sources"); | 
					
						
							| 
									
										
										
										
											2019-03-31 22:12:19 +08:00
										 |  |  | const Generator = require("../Generator"); | 
					
						
							| 
									
										
										
										
											2019-11-18 21:29:19 +08:00
										 |  |  | const RuntimeGlobals = require("../RuntimeGlobals"); | 
					
						
							| 
									
										
										
										
											2021-03-11 23:41:19 +08:00
										 |  |  | const createHash = require("../util/createHash"); | 
					
						
							|  |  |  | const { makePathsRelative } = require("../util/identifier"); | 
					
						
							| 
									
										
										
										
											2022-02-01 18:35:12 +08:00
										 |  |  | const nonNumericOnlyHash = require("../util/nonNumericOnlyHash"); | 
					
						
							| 
									
										
										
										
											2019-03-31 22:12:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {import("webpack-sources").Source} Source */ | 
					
						
							| 
									
										
										
										
											2021-01-05 18:11:02 +08:00
										 |  |  | /** @typedef {import("../../declarations/WebpackOptions").AssetGeneratorOptions} AssetGeneratorOptions */ | 
					
						
							| 
									
										
										
										
											2021-12-17 22:07:20 +08:00
										 |  |  | /** @typedef {import("../../declarations/WebpackOptions").AssetModuleOutputPath} AssetModuleOutputPath */ | 
					
						
							| 
									
										
										
										
											2021-03-22 22:05:50 +08:00
										 |  |  | /** @typedef {import("../../declarations/WebpackOptions").RawPublicPath} RawPublicPath */ | 
					
						
							| 
									
										
										
										
											2019-11-18 21:29:19 +08:00
										 |  |  | /** @typedef {import("../Compilation")} Compilation */ | 
					
						
							|  |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							| 
									
										
										
										
											2019-03-31 22:12:19 +08:00
										 |  |  | /** @typedef {import("../Generator").GenerateContext} GenerateContext */ | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | /** @typedef {import("../Generator").UpdateHashContext} UpdateHashContext */ | 
					
						
							| 
									
										
										
										
											2019-11-18 21:29:19 +08:00
										 |  |  | /** @typedef {import("../Module")} Module */ | 
					
						
							| 
									
										
										
										
											2019-03-31 22:12:19 +08:00
										 |  |  | /** @typedef {import("../NormalModule")} NormalModule */ | 
					
						
							| 
									
										
										
										
											2019-11-18 21:29:19 +08:00
										 |  |  | /** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */ | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | /** @typedef {import("../util/Hash")} Hash */ | 
					
						
							| 
									
										
										
										
											2019-11-19 22:36:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-22 22:08:06 +08:00
										 |  |  | const mergeMaybeArrays = (a, b) => { | 
					
						
							|  |  |  | 	const set = new Set(); | 
					
						
							|  |  |  | 	if (Array.isArray(a)) for (const item of a) set.add(item); | 
					
						
							|  |  |  | 	else set.add(a); | 
					
						
							|  |  |  | 	if (Array.isArray(b)) for (const item of b) set.add(item); | 
					
						
							|  |  |  | 	else set.add(b); | 
					
						
							|  |  |  | 	return Array.from(set); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-22 22:05:50 +08:00
										 |  |  | const mergeAssetInfo = (a, b) => { | 
					
						
							|  |  |  | 	const result = { ...a, ...b }; | 
					
						
							|  |  |  | 	for (const key of Object.keys(a)) { | 
					
						
							|  |  |  | 		if (key in b) { | 
					
						
							|  |  |  | 			if (a[key] === b[key]) continue; | 
					
						
							|  |  |  | 			switch (key) { | 
					
						
							|  |  |  | 				case "fullhash": | 
					
						
							|  |  |  | 				case "chunkhash": | 
					
						
							|  |  |  | 				case "modulehash": | 
					
						
							| 
									
										
										
										
											2021-03-22 22:08:06 +08:00
										 |  |  | 				case "contenthash": | 
					
						
							|  |  |  | 					result[key] = mergeMaybeArrays(a[key], b[key]); | 
					
						
							| 
									
										
										
										
											2021-03-22 22:05:50 +08:00
										 |  |  | 					break; | 
					
						
							|  |  |  | 				case "immutable": | 
					
						
							|  |  |  | 				case "development": | 
					
						
							|  |  |  | 				case "hotModuleReplacement": | 
					
						
							| 
									
										
										
										
											2021-12-06 20:15:48 +08:00
										 |  |  | 				case "javascriptModule": | 
					
						
							| 
									
										
										
										
											2021-03-22 22:05:50 +08:00
										 |  |  | 					result[key] = a[key] || b[key]; | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				case "related": | 
					
						
							|  |  |  | 					result[key] = mergeRelatedInfo(a[key], b[key]); | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				default: | 
					
						
							|  |  |  | 					throw new Error(`Can't handle conflicting asset info for ${key}`); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return result; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const mergeRelatedInfo = (a, b) => { | 
					
						
							|  |  |  | 	const result = { ...a, ...b }; | 
					
						
							|  |  |  | 	for (const key of Object.keys(a)) { | 
					
						
							|  |  |  | 		if (key in b) { | 
					
						
							|  |  |  | 			if (a[key] === b[key]) continue; | 
					
						
							| 
									
										
										
										
											2021-03-22 22:08:06 +08:00
										 |  |  | 			result[key] = mergeMaybeArrays(a[key], b[key]); | 
					
						
							| 
									
										
										
										
											2021-03-22 22:05:50 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return result; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-13 00:23:16 +08:00
										 |  |  | const encodeDataUri = (encoding, source) => { | 
					
						
							|  |  |  | 	let encodedContent; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch (encoding) { | 
					
						
							|  |  |  | 		case "base64": { | 
					
						
							|  |  |  | 			encodedContent = source.buffer().toString("base64"); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		case false: { | 
					
						
							|  |  |  | 			const content = source.source(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (typeof content !== "string") { | 
					
						
							|  |  |  | 				encodedContent = content.toString("utf-8"); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			encodedContent = encodeURIComponent(encodedContent).replace( | 
					
						
							|  |  |  | 				/[!'()*]/g, | 
					
						
							|  |  |  | 				character => "%" + character.codePointAt(0).toString(16) | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 			throw new Error(`Unsupported encoding '${encoding}'`); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return encodedContent; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-17 23:52:55 +08:00
										 |  |  | const decodeDataUriContent = (encoding, content) => { | 
					
						
							|  |  |  | 	const isBase64 = encoding === "base64"; | 
					
						
							|  |  |  | 	return isBase64 | 
					
						
							|  |  |  | 		? Buffer.from(content, "base64") | 
					
						
							|  |  |  | 		: Buffer.from(decodeURIComponent(content), "ascii"); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-18 21:29:19 +08:00
										 |  |  | const JS_TYPES = new Set(["javascript"]); | 
					
						
							|  |  |  | const JS_AND_ASSET_TYPES = new Set(["javascript", "asset"]); | 
					
						
							| 
									
										
										
										
											2021-11-29 20:37:38 +08:00
										 |  |  | const DEFAULT_ENCODING = "base64"; | 
					
						
							| 
									
										
										
										
											2019-03-31 22:12:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-19 21:32:40 +08:00
										 |  |  | class AssetGenerator extends Generator { | 
					
						
							| 
									
										
										
										
											2019-11-16 00:27:36 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2021-01-05 18:11:02 +08:00
										 |  |  | 	 * @param {AssetGeneratorOptions["dataUrl"]=} dataUrlOptions the options for the data url | 
					
						
							| 
									
										
										
										
											2020-01-22 21:19:45 +08:00
										 |  |  | 	 * @param {string=} filename override for output.assetModuleFilename | 
					
						
							| 
									
										
										
										
											2021-03-22 22:05:50 +08:00
										 |  |  | 	 * @param {RawPublicPath=} publicPath override for output.assetModulePublicPath | 
					
						
							| 
									
										
										
										
											2021-12-17 22:07:20 +08:00
										 |  |  | 	 * @param {AssetModuleOutputPath=} outputPath the output path for the emitted file which is not included in the runtime import | 
					
						
							| 
									
										
										
										
											2021-03-12 01:44:41 +08:00
										 |  |  | 	 * @param {boolean=} emit generate output asset | 
					
						
							| 
									
										
										
										
											2019-11-16 00:27:36 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2021-12-17 22:07:20 +08:00
										 |  |  | 	constructor(dataUrlOptions, filename, publicPath, outputPath, emit) { | 
					
						
							| 
									
										
										
										
											2019-11-16 00:27:36 +08:00
										 |  |  | 		super(); | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | 		this.dataUrlOptions = dataUrlOptions; | 
					
						
							| 
									
										
										
										
											2020-01-22 21:19:45 +08:00
										 |  |  | 		this.filename = filename; | 
					
						
							| 
									
										
										
										
											2021-03-16 02:03:19 +08:00
										 |  |  | 		this.publicPath = publicPath; | 
					
						
							| 
									
										
										
										
											2021-12-17 22:07:20 +08:00
										 |  |  | 		this.outputPath = outputPath; | 
					
						
							| 
									
										
										
										
											2021-03-12 01:44:41 +08:00
										 |  |  | 		this.emit = emit; | 
					
						
							| 
									
										
										
										
											2019-11-16 00:27:36 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 20:37:38 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {NormalModule} module module | 
					
						
							|  |  |  | 	 * @param {RuntimeTemplate} runtimeTemplate runtime template | 
					
						
							|  |  |  | 	 * @returns {string} source file name | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	getSourceFileName(module, runtimeTemplate) { | 
					
						
							|  |  |  | 		return makePathsRelative( | 
					
						
							|  |  |  | 			runtimeTemplate.compilation.compiler.context, | 
					
						
							|  |  |  | 			module.matchResource || module.resource, | 
					
						
							|  |  |  | 			runtimeTemplate.compilation.compiler.root | 
					
						
							|  |  |  | 		).replace(/^\.\//, ""); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {NormalModule} module module | 
					
						
							| 
									
										
										
										
											2022-02-22 19:23:24 +08:00
										 |  |  | 	 * @returns {string} mime type | 
					
						
							| 
									
										
										
										
											2021-11-29 20:37:38 +08:00
										 |  |  | 	 */ | 
					
						
							|  |  |  | 	getMimeType(module) { | 
					
						
							| 
									
										
										
										
											2022-02-22 19:23:24 +08:00
										 |  |  | 		if (typeof this.dataUrlOptions === "function") { | 
					
						
							|  |  |  | 			throw new Error( | 
					
						
							|  |  |  | 				"This method must not be called when dataUrlOptions is a function" | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-11-29 20:37:38 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		let mimeType = this.dataUrlOptions.mimetype; | 
					
						
							|  |  |  | 		if (mimeType === undefined) { | 
					
						
							| 
									
										
										
										
											2022-02-22 19:23:24 +08:00
										 |  |  | 			const ext = path.extname(module.nameForCondition()); | 
					
						
							| 
									
										
										
										
											2021-11-29 20:37:38 +08:00
										 |  |  | 			if ( | 
					
						
							|  |  |  | 				module.resourceResolveData && | 
					
						
							|  |  |  | 				module.resourceResolveData.mimetype !== undefined | 
					
						
							|  |  |  | 			) { | 
					
						
							|  |  |  | 				mimeType = | 
					
						
							|  |  |  | 					module.resourceResolveData.mimetype + | 
					
						
							|  |  |  | 					module.resourceResolveData.parameters; | 
					
						
							|  |  |  | 			} else if (ext) { | 
					
						
							|  |  |  | 				mimeType = mimeTypes.lookup(ext); | 
					
						
							| 
									
										
										
										
											2022-02-22 19:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				if (typeof mimeType !== "string") { | 
					
						
							|  |  |  | 					throw new Error( | 
					
						
							|  |  |  | 						"DataUrl can't be generated automatically, " + | 
					
						
							|  |  |  | 							`because there is no mimetype for "${ext}" in mimetype database. ` + | 
					
						
							|  |  |  | 							'Either pass a mimetype via "generator.mimetype" or ' + | 
					
						
							|  |  |  | 							'use type: "asset/resource" to create a resource file instead of a DataUrl' | 
					
						
							|  |  |  | 					); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2021-11-29 20:37:38 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-22 19:23:24 +08:00
										 |  |  | 		if (typeof mimeType !== "string") { | 
					
						
							|  |  |  | 			throw new Error( | 
					
						
							|  |  |  | 				"DataUrl can't be generated automatically. " + | 
					
						
							|  |  |  | 					'Either pass a mimetype via "generator.mimetype" or ' + | 
					
						
							|  |  |  | 					'use type: "asset/resource" to create a resource file instead of a DataUrl' | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 20:37:38 +08:00
										 |  |  | 		return mimeType; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 22:12:19 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2019-11-18 21:29:19 +08:00
										 |  |  | 	 * @param {NormalModule} module module for which the code should be generated | 
					
						
							|  |  |  | 	 * @param {GenerateContext} generateContext context for generate | 
					
						
							|  |  |  | 	 * @returns {Source} generated code | 
					
						
							| 
									
										
										
										
											2019-03-31 22:12:19 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | 	generate( | 
					
						
							|  |  |  | 		module, | 
					
						
							| 
									
										
										
										
											2020-10-11 07:17:03 +08:00
										 |  |  | 		{ runtime, chunkGraph, runtimeTemplate, runtimeRequirements, type, getData } | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | 	) { | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | 		switch (type) { | 
					
						
							|  |  |  | 			case "asset": | 
					
						
							|  |  |  | 				return module.originalSource(); | 
					
						
							|  |  |  | 			default: { | 
					
						
							|  |  |  | 				runtimeRequirements.add(RuntimeGlobals.module); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-17 20:57:11 +08:00
										 |  |  | 				const originalSource = module.originalSource(); | 
					
						
							| 
									
										
										
										
											2020-01-23 18:27:12 +08:00
										 |  |  | 				if (module.buildInfo.dataUrl) { | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | 					let encodedSource; | 
					
						
							|  |  |  | 					if (typeof this.dataUrlOptions === "function") { | 
					
						
							|  |  |  | 						encodedSource = this.dataUrlOptions.call( | 
					
						
							|  |  |  | 							null, | 
					
						
							|  |  |  | 							originalSource.source(), | 
					
						
							|  |  |  | 							{ | 
					
						
							| 
									
										
										
										
											2020-02-28 00:49:47 +08:00
										 |  |  | 								filename: module.matchResource || module.resource, | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | 								module | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 					} else { | 
					
						
							| 
									
										
										
										
											2021-05-19 14:48:10 +08:00
										 |  |  | 						/** @type {string | false | undefined} */ | 
					
						
							|  |  |  | 						let encoding = this.dataUrlOptions.encoding; | 
					
						
							|  |  |  | 						if (encoding === undefined) { | 
					
						
							|  |  |  | 							if ( | 
					
						
							|  |  |  | 								module.resourceResolveData && | 
					
						
							|  |  |  | 								module.resourceResolveData.encoding !== undefined | 
					
						
							|  |  |  | 							) { | 
					
						
							|  |  |  | 								encoding = module.resourceResolveData.encoding; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						if (encoding === undefined) { | 
					
						
							| 
									
										
										
										
											2021-11-29 20:37:38 +08:00
										 |  |  | 							encoding = DEFAULT_ENCODING; | 
					
						
							| 
									
										
										
										
											2021-05-19 14:48:10 +08:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2021-11-29 20:37:38 +08:00
										 |  |  | 						const mimeType = this.getMimeType(module); | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 						let encodedContent; | 
					
						
							| 
									
										
										
										
											2022-01-17 23:47:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-17 02:35:19 +08:00
										 |  |  | 						if ( | 
					
						
							|  |  |  | 							module.resourceResolveData && | 
					
						
							| 
									
										
										
										
											2022-01-17 23:47:49 +08:00
										 |  |  | 							module.resourceResolveData.encoding === encoding && | 
					
						
							|  |  |  | 							decodeDataUriContent( | 
					
						
							|  |  |  | 								module.resourceResolveData.encoding, | 
					
						
							| 
									
										
										
										
											2022-01-17 23:52:55 +08:00
										 |  |  | 								module.resourceResolveData.encodedContent | 
					
						
							| 
									
										
										
										
											2022-01-17 23:47:49 +08:00
										 |  |  | 							).equals(originalSource.buffer()) | 
					
						
							| 
									
										
										
										
											2021-07-17 02:35:19 +08:00
										 |  |  | 						) { | 
					
						
							|  |  |  | 							encodedContent = module.resourceResolveData.encodedContent; | 
					
						
							|  |  |  | 						} else { | 
					
						
							| 
									
										
										
										
											2022-01-17 23:47:49 +08:00
										 |  |  | 							encodedContent = encodeDataUri(encoding, originalSource); | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						encodedSource = `data:${mimeType}${ | 
					
						
							|  |  |  | 							encoding ? `;${encoding}` : "" | 
					
						
							|  |  |  | 						},${encodedContent}`;
 | 
					
						
							| 
									
										
										
										
											2019-11-26 16:53:47 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2021-11-30 20:46:42 +08:00
										 |  |  | 					const data = getData(); | 
					
						
							| 
									
										
										
										
											2022-01-21 22:52:51 +08:00
										 |  |  | 					data.set("url", Buffer.from(encodedSource)); | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | 					return new RawSource( | 
					
						
							|  |  |  | 						`${RuntimeGlobals.module}.exports = ${JSON.stringify( | 
					
						
							|  |  |  | 							encodedSource | 
					
						
							|  |  |  | 						)};`
 | 
					
						
							|  |  |  | 					); | 
					
						
							|  |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2021-03-11 23:41:19 +08:00
										 |  |  | 					const assetModuleFilename = | 
					
						
							|  |  |  | 						this.filename || runtimeTemplate.outputOptions.assetModuleFilename; | 
					
						
							|  |  |  | 					const hash = createHash(runtimeTemplate.outputOptions.hashFunction); | 
					
						
							|  |  |  | 					if (runtimeTemplate.outputOptions.hashSalt) { | 
					
						
							|  |  |  | 						hash.update(runtimeTemplate.outputOptions.hashSalt); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					hash.update(originalSource.buffer()); | 
					
						
							| 
									
										
										
										
											2021-05-11 15:31:46 +08:00
										 |  |  | 					const fullHash = /** @type {string} */ ( | 
					
						
							|  |  |  | 						hash.digest(runtimeTemplate.outputOptions.hashDigest) | 
					
						
							|  |  |  | 					); | 
					
						
							| 
									
										
										
										
											2022-02-01 18:35:12 +08:00
										 |  |  | 					const contentHash = nonNumericOnlyHash( | 
					
						
							|  |  |  | 						fullHash, | 
					
						
							| 
									
										
										
										
											2021-03-11 23:41:19 +08:00
										 |  |  | 						runtimeTemplate.outputOptions.hashDigestLength | 
					
						
							|  |  |  | 					); | 
					
						
							|  |  |  | 					module.buildInfo.fullContentHash = fullHash; | 
					
						
							| 
									
										
										
										
											2021-11-29 20:37:38 +08:00
										 |  |  | 					const sourceFilename = this.getSourceFileName( | 
					
						
							|  |  |  | 						module, | 
					
						
							|  |  |  | 						runtimeTemplate | 
					
						
							|  |  |  | 					); | 
					
						
							| 
									
										
										
										
											2021-05-11 15:31:46 +08:00
										 |  |  | 					let { path: filename, info: assetInfo } = | 
					
						
							|  |  |  | 						runtimeTemplate.compilation.getAssetPathWithInfo( | 
					
						
							|  |  |  | 							assetModuleFilename, | 
					
						
							| 
									
										
										
										
											2021-03-22 22:05:50 +08:00
										 |  |  | 							{ | 
					
						
							|  |  |  | 								module, | 
					
						
							|  |  |  | 								runtime, | 
					
						
							|  |  |  | 								filename: sourceFilename, | 
					
						
							|  |  |  | 								chunkGraph, | 
					
						
							|  |  |  | 								contentHash | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2021-03-16 02:03:19 +08:00
										 |  |  | 						); | 
					
						
							| 
									
										
										
										
											2021-11-22 01:51:36 +08:00
										 |  |  | 					let assetPath; | 
					
						
							| 
									
										
										
										
											2021-07-24 23:02:09 +08:00
										 |  |  | 					if (this.publicPath !== undefined) { | 
					
						
							| 
									
										
										
										
											2021-05-11 15:31:46 +08:00
										 |  |  | 						const { path, info } = | 
					
						
							|  |  |  | 							runtimeTemplate.compilation.getAssetPathWithInfo( | 
					
						
							|  |  |  | 								this.publicPath, | 
					
						
							|  |  |  | 								{ | 
					
						
							|  |  |  | 									module, | 
					
						
							|  |  |  | 									runtime, | 
					
						
							|  |  |  | 									filename: sourceFilename, | 
					
						
							|  |  |  | 									chunkGraph, | 
					
						
							|  |  |  | 									contentHash | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 							); | 
					
						
							| 
									
										
										
										
											2021-03-22 22:05:50 +08:00
										 |  |  | 						assetInfo = mergeAssetInfo(assetInfo, info); | 
					
						
							| 
									
										
										
										
											2021-11-22 01:51:36 +08:00
										 |  |  | 						assetPath = JSON.stringify(path + filename); | 
					
						
							| 
									
										
										
										
											2021-03-16 02:03:19 +08:00
										 |  |  | 					} else { | 
					
						
							|  |  |  | 						runtimeRequirements.add(RuntimeGlobals.publicPath); // add __webpack_require__.p
 | 
					
						
							| 
									
										
										
										
											2021-11-22 22:58:05 +08:00
										 |  |  | 						assetPath = runtimeTemplate.concatenation( | 
					
						
							|  |  |  | 							{ expr: RuntimeGlobals.publicPath }, | 
					
						
							|  |  |  | 							filename | 
					
						
							| 
									
										
										
										
											2021-11-22 01:51:36 +08:00
										 |  |  | 						); | 
					
						
							| 
									
										
										
										
											2021-03-16 02:03:19 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2021-03-22 22:05:50 +08:00
										 |  |  | 					assetInfo = { | 
					
						
							| 
									
										
										
										
											2021-03-11 23:41:19 +08:00
										 |  |  | 						sourceFilename, | 
					
						
							| 
									
										
										
										
											2021-03-22 22:05:50 +08:00
										 |  |  | 						...assetInfo | 
					
						
							| 
									
										
										
										
											2021-03-11 23:41:19 +08:00
										 |  |  | 					}; | 
					
						
							| 
									
										
										
										
											2021-12-17 22:07:20 +08:00
										 |  |  | 					if (this.outputPath) { | 
					
						
							|  |  |  | 						const { path: outputPath, info } = | 
					
						
							|  |  |  | 							runtimeTemplate.compilation.getAssetPathWithInfo( | 
					
						
							|  |  |  | 								this.outputPath, | 
					
						
							|  |  |  | 								{ | 
					
						
							|  |  |  | 									module, | 
					
						
							|  |  |  | 									runtime, | 
					
						
							|  |  |  | 									filename: sourceFilename, | 
					
						
							|  |  |  | 									chunkGraph, | 
					
						
							|  |  |  | 									contentHash | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 							); | 
					
						
							|  |  |  | 						assetInfo = mergeAssetInfo(assetInfo, info); | 
					
						
							| 
									
										
										
										
											2022-01-19 16:18:17 +08:00
										 |  |  | 						filename = path.posix.join(outputPath, filename); | 
					
						
							| 
									
										
										
										
											2021-12-17 22:07:20 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2021-03-22 22:05:50 +08:00
										 |  |  | 					module.buildInfo.filename = filename; | 
					
						
							|  |  |  | 					module.buildInfo.assetInfo = assetInfo; | 
					
						
							| 
									
										
										
										
											2020-10-11 07:17:03 +08:00
										 |  |  | 					if (getData) { | 
					
						
							| 
									
										
										
										
											2021-03-11 23:41:19 +08:00
										 |  |  | 						// Due to code generation caching module.buildInfo.XXX can't used to store such information
 | 
					
						
							|  |  |  | 						// It need to be stored in the code generation results instead, where it's cached too
 | 
					
						
							|  |  |  | 						// TODO webpack 6 For back-compat reasons we also store in on module.buildInfo
 | 
					
						
							| 
									
										
										
										
											2020-10-11 07:17:03 +08:00
										 |  |  | 						const data = getData(); | 
					
						
							| 
									
										
										
										
											2021-03-11 23:41:19 +08:00
										 |  |  | 						data.set("fullContentHash", fullHash); | 
					
						
							|  |  |  | 						data.set("filename", filename); | 
					
						
							| 
									
										
										
										
											2021-03-22 22:05:50 +08:00
										 |  |  | 						data.set("assetInfo", assetInfo); | 
					
						
							| 
									
										
										
										
											2020-10-11 07:17:03 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					return new RawSource( | 
					
						
							| 
									
										
										
										
											2021-11-22 01:51:36 +08:00
										 |  |  | 						`${RuntimeGlobals.module}.exports = ${assetPath};` | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | 					); | 
					
						
							| 
									
										
										
										
											2019-11-26 16:53:47 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-11-18 21:29:19 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-03-31 22:12:19 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2019-11-18 21:29:19 +08:00
										 |  |  | 	 * @param {NormalModule} module fresh module | 
					
						
							|  |  |  | 	 * @returns {Set<string>} available types (do not mutate) | 
					
						
							| 
									
										
										
										
											2019-03-31 22:12:19 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2019-11-18 21:29:19 +08:00
										 |  |  | 	getTypes(module) { | 
					
						
							| 
									
										
										
										
											2021-07-05 17:22:13 +08:00
										 |  |  | 		if ((module.buildInfo && module.buildInfo.dataUrl) || this.emit === false) { | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | 			return JS_TYPES; | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2019-11-18 21:29:19 +08:00
										 |  |  | 			return JS_AND_ASSET_TYPES; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-03-31 22:12:19 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {NormalModule} module the module | 
					
						
							|  |  |  | 	 * @param {string=} type source type | 
					
						
							|  |  |  | 	 * @returns {number} estimate size of the module | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2019-11-27 04:24:41 +08:00
										 |  |  | 	getSize(module, type) { | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | 		switch (type) { | 
					
						
							|  |  |  | 			case "asset": { | 
					
						
							|  |  |  | 				const originalSource = module.originalSource(); | 
					
						
							| 
									
										
										
										
											2019-06-06 05:29:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | 				if (!originalSource) { | 
					
						
							|  |  |  | 					return 0; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2019-11-18 21:29:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | 				return originalSource.size(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			default: | 
					
						
							| 
									
										
										
										
											2021-07-05 17:22:13 +08:00
										 |  |  | 				if (module.buildInfo && module.buildInfo.dataUrl) { | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | 					const originalSource = module.originalSource(); | 
					
						
							| 
									
										
										
										
											2019-11-18 21:29:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | 					if (!originalSource) { | 
					
						
							|  |  |  | 						return 0; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					// roughly for data url
 | 
					
						
							|  |  |  | 					// Example: m.exports="data:image/png;base64,ag82/f+2=="
 | 
					
						
							|  |  |  | 					// 4/3 = base64 encoding
 | 
					
						
							|  |  |  | 					// 34 = ~ data url header + footer + rounding
 | 
					
						
							|  |  |  | 					return originalSource.size() * 1.34 + 36; | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					// it's only estimated so this number is probably fine
 | 
					
						
							|  |  |  | 					// Example: m.exports=r.p+"0123456789012345678901.ext"
 | 
					
						
							|  |  |  | 					return 42; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2019-11-18 21:29:19 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-03-31 22:12:19 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {Hash} hash hash that will be modified | 
					
						
							|  |  |  | 	 * @param {UpdateHashContext} updateHashContext context for updating hash | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2021-11-29 20:37:38 +08:00
										 |  |  | 	updateHash(hash, { module, runtime, runtimeTemplate, chunkGraph }) { | 
					
						
							|  |  |  | 		if (module.buildInfo.dataUrl) { | 
					
						
							|  |  |  | 			hash.update("data-url"); | 
					
						
							| 
									
										
										
										
											2022-02-22 19:23:24 +08:00
										 |  |  | 			// this.dataUrlOptions as function should be pure and only depend on input source and filename
 | 
					
						
							|  |  |  | 			// therefore it doesn't need to be hashed
 | 
					
						
							| 
									
										
										
										
											2021-11-29 20:37:38 +08:00
										 |  |  | 			if (typeof this.dataUrlOptions === "function") { | 
					
						
							| 
									
										
										
										
											2022-02-22 19:23:24 +08:00
										 |  |  | 				const ident = /** @type {{ ident?: string }} */ (this.dataUrlOptions) | 
					
						
							|  |  |  | 					.ident; | 
					
						
							|  |  |  | 				if (ident) hash.update(ident); | 
					
						
							| 
									
										
										
										
											2021-11-29 20:37:38 +08:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2022-02-28 16:45:00 +08:00
										 |  |  | 				if ( | 
					
						
							|  |  |  | 					this.dataUrlOptions.encoding && | 
					
						
							|  |  |  | 					this.dataUrlOptions.encoding !== DEFAULT_ENCODING | 
					
						
							|  |  |  | 				) { | 
					
						
							| 
									
										
										
										
											2022-02-22 19:23:24 +08:00
										 |  |  | 					hash.update(this.dataUrlOptions.encoding); | 
					
						
							| 
									
										
										
										
											2022-02-28 16:45:00 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2022-02-22 19:23:24 +08:00
										 |  |  | 				if (this.dataUrlOptions.mimetype) | 
					
						
							|  |  |  | 					hash.update(this.dataUrlOptions.mimetype); | 
					
						
							|  |  |  | 				// computed mimetype depends only on module filename which is already part of the hash
 | 
					
						
							| 
									
										
										
										
											2021-11-29 20:37:38 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			hash.update("resource"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			const pathData = { | 
					
						
							|  |  |  | 				module, | 
					
						
							|  |  |  | 				runtime, | 
					
						
							|  |  |  | 				filename: this.getSourceFileName(module, runtimeTemplate), | 
					
						
							|  |  |  | 				chunkGraph, | 
					
						
							|  |  |  | 				contentHash: runtimeTemplate.contentHashReplacement | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (typeof this.publicPath === "function") { | 
					
						
							| 
									
										
										
										
											2022-02-22 19:23:24 +08:00
										 |  |  | 				hash.update("path"); | 
					
						
							|  |  |  | 				const assetInfo = {}; | 
					
						
							|  |  |  | 				hash.update(this.publicPath(pathData, assetInfo)); | 
					
						
							|  |  |  | 				hash.update(JSON.stringify(assetInfo)); | 
					
						
							| 
									
										
										
										
											2021-11-29 20:37:38 +08:00
										 |  |  | 			} else if (this.publicPath) { | 
					
						
							| 
									
										
										
										
											2022-02-22 19:23:24 +08:00
										 |  |  | 				hash.update("path"); | 
					
						
							|  |  |  | 				hash.update(this.publicPath); | 
					
						
							| 
									
										
										
										
											2021-11-29 20:37:38 +08:00
										 |  |  | 			} else { | 
					
						
							|  |  |  | 				hash.update("no-path"); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			const assetModuleFilename = | 
					
						
							|  |  |  | 				this.filename || runtimeTemplate.outputOptions.assetModuleFilename; | 
					
						
							| 
									
										
										
										
											2022-02-22 19:23:24 +08:00
										 |  |  | 			const { path: filename, info } = | 
					
						
							| 
									
										
										
										
											2021-11-29 20:37:38 +08:00
										 |  |  | 				runtimeTemplate.compilation.getAssetPathWithInfo( | 
					
						
							|  |  |  | 					assetModuleFilename, | 
					
						
							|  |  |  | 					pathData | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 			hash.update(filename); | 
					
						
							| 
									
										
										
										
											2022-02-22 19:23:24 +08:00
										 |  |  | 			hash.update(JSON.stringify(info)); | 
					
						
							| 
									
										
										
										
											2021-11-29 20:37:38 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-03-31 22:12:19 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-19 21:32:40 +08:00
										 |  |  | module.exports = AssetGenerator; |