| 
									
										
										
										
											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-03-22 19:05:58 +08:00
										 |  |  | const { ConcatSource } = require("webpack-sources"); | 
					
						
							| 
									
										
										
										
											2018-01-24 06:09:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | const stringifySafe = data => | 
					
						
							|  |  |  | 	JSON.stringify(data).replace( | 
					
						
							|  |  |  | 		/\u2028|\u2029/g, | 
					
						
							|  |  |  | 		str => (str === "\u2029" ? "\\u2029" : "\\u2028") | 
					
						
							|  |  |  | 	); // invalid in JavaScript but valid JSON
 | 
					
						
							| 
									
										
										
										
											2018-01-24 06:09:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class JsonGenerator { | 
					
						
							|  |  |  | 	generate(module, dependencyTemplates, runtimeTemplate) { | 
					
						
							|  |  |  | 		const source = new ConcatSource(); | 
					
						
							|  |  |  | 		const data = module.buildInfo.jsonData; | 
					
						
							| 
									
										
										
										
											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]; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											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; |