| 
									
										
										
										
											2018-01-24 06:09:26 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-24 06:09:26 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-15 17:46:28 +08:00
										 |  |  | const { RawSource } = require("webpack-sources"); | 
					
						
							|  |  |  | const ConcatenationScope = require("../ConcatenationScope"); | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | const { UsageState } = require("../ExportsInfo"); | 
					
						
							| 
									
										
										
										
											2019-10-11 21:46:57 +08:00
										 |  |  | const Generator = require("../Generator"); | 
					
						
							|  |  |  | const RuntimeGlobals = require("../RuntimeGlobals"); | 
					
						
							| 
									
										
										
										
											2018-08-07 01:39:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {import("webpack-sources").Source} Source */ | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | /** @typedef {import("../ExportsInfo")} ExportsInfo */ | 
					
						
							| 
									
										
										
										
											2019-10-11 21:46:57 +08:00
										 |  |  | /** @typedef {import("../Generator").GenerateContext} GenerateContext */ | 
					
						
							| 
									
										
										
										
											2020-09-15 17:46:28 +08:00
										 |  |  | /** @typedef {import("../Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */ | 
					
						
							| 
									
										
										
										
											2019-10-11 21:46:57 +08:00
										 |  |  | /** @typedef {import("../NormalModule")} NormalModule */ | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */ | 
					
						
							| 
									
										
										
										
											2018-01-24 06:09:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-25 06:35:58 +08:00
										 |  |  | const stringifySafe = data => { | 
					
						
							|  |  |  | 	const stringified = JSON.stringify(data); | 
					
						
							|  |  |  | 	if (!stringified) { | 
					
						
							|  |  |  | 		return undefined; // Invalid JSON
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-05 17:06:32 +08:00
										 |  |  | 	return stringified.replace(/\u2028|\u2029/g, str => | 
					
						
							|  |  |  | 		str === "\u2029" ? "\\u2029" : "\\u2028" | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 	); // invalid in JavaScript but valid JSON
 | 
					
						
							| 
									
										
										
										
											2018-04-25 06:35:58 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2018-01-24 06:09:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 06:24:13 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @param {Object} data data (always an object or array) | 
					
						
							|  |  |  |  * @param {ExportsInfo} exportsInfo exports info | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  |  * @param {RuntimeSpec} runtime the runtime | 
					
						
							| 
									
										
										
										
											2019-10-31 06:24:13 +08:00
										 |  |  |  * @returns {Object} reduced data | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | const createObjectForExportsInfo = (data, exportsInfo, runtime) => { | 
					
						
							|  |  |  | 	if (exportsInfo.otherExportsInfo.getUsed(runtime) !== UsageState.Unused) | 
					
						
							|  |  |  | 		return data; | 
					
						
							| 
									
										
										
										
											2021-02-07 23:35:04 +08:00
										 |  |  | 	const isArray = Array.isArray(data); | 
					
						
							|  |  |  | 	const reducedData = isArray ? [] : {}; | 
					
						
							| 
									
										
										
										
											2019-10-31 06:24:13 +08:00
										 |  |  | 	for (const key of Object.keys(data)) { | 
					
						
							|  |  |  | 		const exportInfo = exportsInfo.getReadOnlyExportInfo(key); | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | 		const used = exportInfo.getUsed(runtime); | 
					
						
							|  |  |  | 		if (used === UsageState.Unused) continue; | 
					
						
							| 
									
										
										
										
											2021-02-07 23:35:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 06:24:13 +08:00
										 |  |  | 		let value; | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | 		if (used === UsageState.OnlyPropertiesUsed && exportInfo.exportsInfo) { | 
					
						
							|  |  |  | 			value = createObjectForExportsInfo( | 
					
						
							|  |  |  | 				data[key], | 
					
						
							|  |  |  | 				exportInfo.exportsInfo, | 
					
						
							|  |  |  | 				runtime | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2019-10-31 06:24:13 +08:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			value = data[key]; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | 		const name = exportInfo.getUsedName(key, runtime); | 
					
						
							| 
									
										
										
										
											2019-10-31 06:24:13 +08:00
										 |  |  | 		reducedData[name] = value; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-02-07 23:35:04 +08:00
										 |  |  | 	if (isArray) { | 
					
						
							|  |  |  | 		let arrayLengthWhenUsed = | 
					
						
							|  |  |  | 			exportsInfo.getReadOnlyExportInfo("length").getUsed(runtime) !== | 
					
						
							|  |  |  | 			UsageState.Unused | 
					
						
							|  |  |  | 				? data.length | 
					
						
							|  |  |  | 				: undefined; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 06:24:13 +08:00
										 |  |  | 		let sizeObjectMinusArray = 0; | 
					
						
							|  |  |  | 		for (let i = 0; i < reducedData.length; i++) { | 
					
						
							|  |  |  | 			if (reducedData[i] === undefined) { | 
					
						
							|  |  |  | 				sizeObjectMinusArray -= 2; | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				sizeObjectMinusArray += `${i}`.length + 3; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-02-07 23:35:04 +08:00
										 |  |  | 		if (arrayLengthWhenUsed !== undefined) { | 
					
						
							|  |  |  | 			sizeObjectMinusArray += | 
					
						
							|  |  |  | 				`${arrayLengthWhenUsed}`.length + | 
					
						
							|  |  |  | 				8 - | 
					
						
							|  |  |  | 				(arrayLengthWhenUsed - reducedData.length) * 2; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (sizeObjectMinusArray < 0) | 
					
						
							|  |  |  | 			return Object.assign( | 
					
						
							|  |  |  | 				arrayLengthWhenUsed === undefined | 
					
						
							|  |  |  | 					? {} | 
					
						
							|  |  |  | 					: { length: arrayLengthWhenUsed }, | 
					
						
							|  |  |  | 				reducedData | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 		const generatedLength = | 
					
						
							|  |  |  | 			arrayLengthWhenUsed !== undefined | 
					
						
							|  |  |  | 				? Math.max(arrayLengthWhenUsed, reducedData.length) | 
					
						
							|  |  |  | 				: reducedData.length; | 
					
						
							|  |  |  | 		for (let i = 0; i < generatedLength; i++) { | 
					
						
							| 
									
										
										
										
											2019-10-31 06:24:13 +08:00
										 |  |  | 			if (reducedData[i] === undefined) { | 
					
						
							|  |  |  | 				reducedData[i] = 0; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return reducedData; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-03 22:00:32 +08:00
										 |  |  | const TYPES = new Set(["javascript"]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-07 01:39:43 +08:00
										 |  |  | class JsonGenerator extends Generator { | 
					
						
							| 
									
										
										
										
											2018-12-03 22:00:32 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2019-11-18 21:29:19 +08:00
										 |  |  | 	 * @param {NormalModule} module fresh module | 
					
						
							| 
									
										
										
										
											2018-12-03 22:00:32 +08:00
										 |  |  | 	 * @returns {Set<string>} available types (do not mutate) | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2019-11-18 21:29:19 +08:00
										 |  |  | 	getTypes(module) { | 
					
						
							| 
									
										
										
										
											2018-12-03 22:00:32 +08:00
										 |  |  | 		return TYPES; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-04 18:23:40 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {NormalModule} module the module | 
					
						
							|  |  |  | 	 * @param {string=} type source type | 
					
						
							|  |  |  | 	 * @returns {number} estimate size of the module | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	getSize(module, type) { | 
					
						
							| 
									
										
										
										
											2021-07-06 16:14:22 +08:00
										 |  |  | 		let data = | 
					
						
							|  |  |  | 			module.buildInfo && | 
					
						
							|  |  |  | 			module.buildInfo.jsonData && | 
					
						
							|  |  |  | 			module.buildInfo.jsonData.get(); | 
					
						
							| 
									
										
										
										
											2018-12-04 18:23:40 +08:00
										 |  |  | 		if (!data) return 0; | 
					
						
							|  |  |  | 		return stringifySafe(data).length + 10; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-15 17:46:28 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {NormalModule} module module for which the bailout reason should be determined | 
					
						
							|  |  |  | 	 * @param {ConcatenationBailoutReasonContext} context context | 
					
						
							|  |  |  | 	 * @returns {string | undefined} reason why this module can't be concatenated, undefined when it can be concatenated | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	getConcatenationBailoutReason(module, context) { | 
					
						
							|  |  |  | 		return undefined; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-07 01:39:43 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {NormalModule} module module for which the code should be generated | 
					
						
							|  |  |  | 	 * @param {GenerateContext} generateContext context for generate | 
					
						
							|  |  |  | 	 * @returns {Source} generated code | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | 	generate( | 
					
						
							|  |  |  | 		module, | 
					
						
							| 
									
										
										
										
											2020-09-15 17:46:28 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			moduleGraph, | 
					
						
							|  |  |  | 			runtimeTemplate, | 
					
						
							|  |  |  | 			runtimeRequirements, | 
					
						
							|  |  |  | 			runtime, | 
					
						
							|  |  |  | 			concatenationScope | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | 	) { | 
					
						
							| 
									
										
										
										
											2021-07-06 16:14:22 +08:00
										 |  |  | 		const data = | 
					
						
							|  |  |  | 			module.buildInfo && | 
					
						
							|  |  |  | 			module.buildInfo.jsonData && | 
					
						
							|  |  |  | 			module.buildInfo.jsonData.get(); | 
					
						
							| 
									
										
										
										
											2018-06-26 00:41:29 +08:00
										 |  |  | 		if (data === undefined) { | 
					
						
							|  |  |  | 			return new RawSource( | 
					
						
							|  |  |  | 				runtimeTemplate.missingModuleStatement({ | 
					
						
							|  |  |  | 					request: module.rawRequest | 
					
						
							|  |  |  | 				}) | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-10-31 06:24:13 +08:00
										 |  |  | 		const exportsInfo = moduleGraph.getExportsInfo(module); | 
					
						
							|  |  |  | 		let finalJson = | 
					
						
							| 
									
										
										
										
											2019-11-05 04:05:17 +08:00
										 |  |  | 			typeof data === "object" && | 
					
						
							|  |  |  | 			data && | 
					
						
							| 
									
										
										
										
											2020-07-28 00:09:48 +08:00
										 |  |  | 			exportsInfo.otherExportsInfo.getUsed(runtime) === UsageState.Unused | 
					
						
							|  |  |  | 				? createObjectForExportsInfo(data, exportsInfo, runtime) | 
					
						
							| 
									
										
										
										
											2019-10-31 06:24:13 +08:00
										 |  |  | 				: data; | 
					
						
							| 
									
										
										
										
											2019-07-02 17:17:52 +08:00
										 |  |  | 		// Use JSON because JSON.parse() is much faster than JavaScript evaluation
 | 
					
						
							| 
									
										
										
										
											2019-10-31 06:24:13 +08:00
										 |  |  | 		const jsonStr = stringifySafe(finalJson); | 
					
						
							|  |  |  | 		const jsonExpr = | 
					
						
							| 
									
										
										
										
											2019-12-20 19:47:03 +08:00
										 |  |  | 			jsonStr.length > 20 && typeof finalJson === "object" | 
					
						
							| 
									
										
										
										
											2021-02-11 00:52:49 +08:00
										 |  |  | 				? `JSON.parse('${jsonStr.replace(/[\\']/g, "\\$&")}')` | 
					
						
							| 
									
										
										
										
											2019-12-20 19:47:03 +08:00
										 |  |  | 				: jsonStr; | 
					
						
							| 
									
										
										
										
											2020-09-15 17:46:28 +08:00
										 |  |  | 		let content; | 
					
						
							|  |  |  | 		if (concatenationScope) { | 
					
						
							|  |  |  | 			content = `${runtimeTemplate.supportsConst() ? "const" : "var"} ${ | 
					
						
							|  |  |  | 				ConcatenationScope.NAMESPACE_OBJECT_EXPORT | 
					
						
							|  |  |  | 			} = ${jsonExpr};`;
 | 
					
						
							|  |  |  | 			concatenationScope.registerNamespaceExport( | 
					
						
							|  |  |  | 				ConcatenationScope.NAMESPACE_OBJECT_EXPORT | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			runtimeRequirements.add(RuntimeGlobals.module); | 
					
						
							|  |  |  | 			content = `${module.moduleArgument}.exports = ${jsonExpr};`; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return new RawSource(content); | 
					
						
							| 
									
										
										
										
											2018-01-24 06:09:26 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = JsonGenerator; |