| 
									
										
										
										
											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"); | 
					
						
							| 
									
										
										
										
											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-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": | 
					
						
							|  |  |  | 				case "javascriptModule	": | 
					
						
							|  |  |  | 					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; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-18 21:29:19 +08:00
										 |  |  | const JS_TYPES = new Set(["javascript"]); | 
					
						
							|  |  |  | const JS_AND_ASSET_TYPES = new Set(["javascript", "asset"]); | 
					
						
							| 
									
										
										
										
											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-03-12 01:44:41 +08:00
										 |  |  | 	 * @param {boolean=} emit generate output asset | 
					
						
							| 
									
										
										
										
											2019-11-16 00:27:36 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2021-03-16 02:03:19 +08:00
										 |  |  | 	constructor(dataUrlOptions, filename, publicPath, 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-03-12 01:44:41 +08:00
										 |  |  | 		this.emit = emit; | 
					
						
							| 
									
										
										
										
											2019-11-16 00:27:36 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							|  |  |  | 							encoding = "base64"; | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						let ext; | 
					
						
							|  |  |  | 						let mimeType = this.dataUrlOptions.mimetype; | 
					
						
							|  |  |  | 						if (mimeType === undefined) { | 
					
						
							|  |  |  | 							ext = path.extname(module.nameForCondition()); | 
					
						
							|  |  |  | 							if ( | 
					
						
							|  |  |  | 								module.resourceResolveData && | 
					
						
							|  |  |  | 								module.resourceResolveData.mimetype !== undefined | 
					
						
							|  |  |  | 							) { | 
					
						
							|  |  |  | 								mimeType = module.resourceResolveData.mimetype; | 
					
						
							|  |  |  | 							} else if (ext) { | 
					
						
							|  |  |  | 								mimeType = mimeTypes.lookup(ext); | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						if (typeof mimeType !== "string") { | 
					
						
							| 
									
										
										
										
											2019-12-24 00:44:41 +08:00
										 |  |  | 							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' | 
					
						
							|  |  |  | 							); | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2019-11-26 20:56:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 						let encodedContent; | 
					
						
							|  |  |  | 						switch (encoding) { | 
					
						
							|  |  |  | 							case "base64": { | 
					
						
							|  |  |  | 								encodedContent = originalSource.buffer().toString("base64"); | 
					
						
							|  |  |  | 								break; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 							case false: { | 
					
						
							|  |  |  | 								const content = originalSource.source(); | 
					
						
							|  |  |  | 								if (typeof content === "string") { | 
					
						
							|  |  |  | 									encodedContent = encodeURI(content); | 
					
						
							|  |  |  | 								} else { | 
					
						
							|  |  |  | 									encodedContent = encodeURI(content.toString("utf-8")); | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 								break; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 							default: | 
					
						
							|  |  |  | 								throw new Error(`Unsupported encoding '${encoding}'`); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						encodedSource = `data:${mimeType}${ | 
					
						
							|  |  |  | 							encoding ? `;${encoding}` : "" | 
					
						
							|  |  |  | 						},${encodedContent}`;
 | 
					
						
							| 
									
										
										
										
											2019-11-26 16:53:47 +08:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  | 					); | 
					
						
							| 
									
										
										
										
											2021-03-11 23:41:19 +08:00
										 |  |  | 					const contentHash = fullHash.slice( | 
					
						
							|  |  |  | 						0, | 
					
						
							|  |  |  | 						runtimeTemplate.outputOptions.hashDigestLength | 
					
						
							|  |  |  | 					); | 
					
						
							|  |  |  | 					module.buildInfo.fullContentHash = fullHash; | 
					
						
							|  |  |  | 					const sourceFilename = makePathsRelative( | 
					
						
							|  |  |  | 						runtimeTemplate.compilation.compiler.context, | 
					
						
							|  |  |  | 						module.matchResource || module.resource, | 
					
						
							|  |  |  | 						runtimeTemplate.compilation.compiler.root | 
					
						
							|  |  |  | 					).replace(/^\.\//, ""); | 
					
						
							| 
									
										
										
										
											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-05-11 15:31:46 +08:00
										 |  |  | 					let publicPath; | 
					
						
							|  |  |  | 					if (this.publicPath) { | 
					
						
							|  |  |  | 						const { path, info } = | 
					
						
							|  |  |  | 							runtimeTemplate.compilation.getAssetPathWithInfo( | 
					
						
							|  |  |  | 								this.publicPath, | 
					
						
							|  |  |  | 								{ | 
					
						
							|  |  |  | 									module, | 
					
						
							|  |  |  | 									runtime, | 
					
						
							|  |  |  | 									filename: sourceFilename, | 
					
						
							|  |  |  | 									chunkGraph, | 
					
						
							|  |  |  | 									contentHash | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 							); | 
					
						
							| 
									
										
										
										
											2021-03-22 22:05:50 +08:00
										 |  |  | 						publicPath = JSON.stringify(path); | 
					
						
							|  |  |  | 						assetInfo = mergeAssetInfo(assetInfo, info); | 
					
						
							| 
									
										
										
										
											2021-03-16 02:03:19 +08:00
										 |  |  | 					} else { | 
					
						
							|  |  |  | 						publicPath = RuntimeGlobals.publicPath; | 
					
						
							|  |  |  | 						runtimeRequirements.add(RuntimeGlobals.publicPath); // add __webpack_require__.p
 | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											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-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-03-16 02:03:19 +08:00
										 |  |  | 						`${ | 
					
						
							|  |  |  | 							RuntimeGlobals.module | 
					
						
							|  |  |  | 						}.exports = ${publicPath} + ${JSON.stringify(filename)};`
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	updateHash(hash, { module }) { | 
					
						
							| 
									
										
										
										
											2020-01-23 18:27:12 +08:00
										 |  |  | 		hash.update(module.buildInfo.dataUrl ? "data-url" : "resource"); | 
					
						
							| 
									
										
										
										
											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; |