| 
									
										
										
										
											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"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-26 00:41:29 +08:00
										 |  |  | const { ConcatSource, RawSource } = require("webpack-sources"); | 
					
						
							| 
									
										
										
										
											2018-08-07 01:39:43 +08:00
										 |  |  | const Generator = require("./Generator"); | 
					
						
							| 
									
										
										
										
											2018-11-17 01:18:44 +08:00
										 |  |  | const RuntimeGlobals = require("./RuntimeGlobals"); | 
					
						
							| 
									
										
										
										
											2018-08-07 01:39:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** @typedef {import("webpack-sources").Source} Source */ | 
					
						
							|  |  |  | /** @typedef {import("./Generator").GenerateContext} GenerateContext */ | 
					
						
							|  |  |  | /** @typedef {import("./NormalModule")} NormalModule */ | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @returns {Set<string>} available types (do not mutate) | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	getTypes() { | 
					
						
							|  |  |  | 		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) { | 
					
						
							|  |  |  | 		let data = module.buildInfo.jsonData; | 
					
						
							|  |  |  | 		if (!data) return 0; | 
					
						
							|  |  |  | 		return stringifySafe(data).length + 10; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2018-11-17 01:18:44 +08:00
										 |  |  | 	generate(module, { moduleGraph, runtimeTemplate, runtimeRequirements }) { | 
					
						
							| 
									
										
										
										
											2018-01-24 06:09:26 +08:00
										 |  |  | 		const source = new ConcatSource(); | 
					
						
							|  |  |  | 		const data = module.buildInfo.jsonData; | 
					
						
							| 
									
										
										
										
											2018-06-26 00:41:29 +08:00
										 |  |  | 		if (data === undefined) { | 
					
						
							|  |  |  | 			return new RawSource( | 
					
						
							|  |  |  | 				runtimeTemplate.missingModuleStatement({ | 
					
						
							|  |  |  | 					request: module.rawRequest | 
					
						
							|  |  |  | 				}) | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-11-17 01:18:44 +08:00
										 |  |  | 		runtimeRequirements.add(RuntimeGlobals.module); | 
					
						
							| 
									
										
										
										
											2018-12-30 16:03:42 +08:00
										 |  |  | 		const providedExports = moduleGraph.getProvidedExports(module); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if ( | 
					
						
							| 
									
										
										
										
											2018-12-30 16:03:42 +08:00
										 |  |  | 			Array.isArray(providedExports) && | 
					
						
							| 
									
										
										
										
											2018-08-07 03:01:24 +08:00
										 |  |  | 			!module.isExportUsed(moduleGraph, "default") | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		) { | 
					
						
							| 
									
										
										
										
											2018-01-24 06:09:26 +08:00
										 |  |  | 			// Only some exports are used: We can optimize here, by only generating a part of the JSON
 | 
					
						
							|  |  |  | 			const reducedJson = {}; | 
					
						
							| 
									
										
										
										
											2018-12-30 16:03:42 +08:00
										 |  |  | 			for (const exportName of providedExports) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				if (exportName === "default") continue; | 
					
						
							| 
									
										
										
										
											2018-08-07 03:01:24 +08:00
										 |  |  | 				const used = module.getUsedName(moduleGraph, exportName); | 
					
						
							| 
									
										
										
										
											2018-08-07 01:39:43 +08:00
										 |  |  | 				if (typeof used === "string") { | 
					
						
							| 
									
										
										
										
											2018-01-24 06:09:26 +08:00
										 |  |  | 					reducedJson[used] = data[exportName]; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			source.add( | 
					
						
							|  |  |  | 				`${module.moduleArgument}.exports = ${stringifySafe(reducedJson)};` | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2018-01-24 06:09:26 +08:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			source.add(`${module.moduleArgument}.exports = ${stringifySafe(data)};`); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return source; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = JsonGenerator; |