| 
									
										
										
										
											2018-01-24 06:09:26 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-26 00:41:29 +08:00
										 |  |  | const { ConcatSource, RawSource } = require("webpack-sources"); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  | class JsonGenerator { | 
					
						
							|  |  |  | 	generate(module, dependencyTemplates, runtimeTemplate) { | 
					
						
							|  |  |  | 		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 | 
					
						
							|  |  |  | 				}) | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-07-02 17:17:52 +08:00
										 |  |  | 		let finalJson; | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		if ( | 
					
						
							|  |  |  | 			Array.isArray(module.buildMeta.providedExports) && | 
					
						
							|  |  |  | 			!module.isUsed("default") | 
					
						
							|  |  |  | 		) { | 
					
						
							| 
									
										
										
										
											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-02-25 09:00:20 +08:00
										 |  |  | 			for (const exportName of module.buildMeta.providedExports) { | 
					
						
							|  |  |  | 				if (exportName === "default") continue; | 
					
						
							| 
									
										
										
										
											2018-01-24 06:09:26 +08:00
										 |  |  | 				const used = module.isUsed(exportName); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 				if (used) { | 
					
						
							| 
									
										
										
										
											2018-01-24 06:09:26 +08:00
										 |  |  | 					reducedJson[used] = data[exportName]; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-07-02 17:17:52 +08:00
										 |  |  | 			finalJson = reducedJson; | 
					
						
							| 
									
										
										
										
											2018-01-24 06:09:26 +08:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2019-07-02 17:17:52 +08:00
										 |  |  | 			finalJson = data; | 
					
						
							| 
									
										
										
										
											2018-01-24 06:09:26 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-07-02 17:17:52 +08:00
										 |  |  | 		// Use JSON because JSON.parse() is much faster than JavaScript evaluation
 | 
					
						
							|  |  |  | 		const jsonSource = JSON.stringify(stringifySafe(finalJson)); | 
					
						
							|  |  |  | 		const jsonExpr = `JSON.parse(${jsonSource})`; | 
					
						
							|  |  |  | 		source.add(`${module.moduleArgument}.exports = ${jsonExpr};`); | 
					
						
							| 
									
										
										
										
											2018-01-24 06:09:26 +08:00
										 |  |  | 		return source; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = JsonGenerator; |