| 
									
										
										
										
											2017-11-12 01:48:29 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-12 01:48:29 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-01 00:45:36 +08:00
										 |  |  | const { JSON_MODULE_TYPE } = require("../ModuleTypeConstants"); | 
					
						
							| 
									
										
										
										
											2021-04-16 21:35:18 +08:00
										 |  |  | const createSchemaValidation = require("../util/create-schema-validation"); | 
					
						
							| 
									
										
										
										
											2018-01-24 06:09:26 +08:00
										 |  |  | const JsonGenerator = require("./JsonGenerator"); | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | const JsonParser = require("./JsonParser"); | 
					
						
							| 
									
										
										
										
											2017-12-07 03:37:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-11 21:46:57 +08:00
										 |  |  | /** @typedef {import("../Compiler")} Compiler */ | 
					
						
							| 
									
										
										
										
											2018-11-03 04:05:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-16 21:35:18 +08:00
										 |  |  | const validate = createSchemaValidation( | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 	require("../../schemas/plugins/json/JsonModulesPluginParser.check"), | 
					
						
							| 
									
										
										
										
											2025-05-01 22:36:51 +08:00
										 |  |  | 	() => require("../../schemas/plugins/json/JsonModulesPluginParser.json"), | 
					
						
							| 
									
										
										
										
											2021-04-16 21:35:18 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		name: "Json Modules Plugin", | 
					
						
							|  |  |  | 		baseDataPath: "parser" | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-12-02 23:37:55 +08:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2019-11-29 01:12:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-13 22:24:10 +08:00
										 |  |  | const validateGenerator = createSchemaValidation( | 
					
						
							| 
									
										
										
										
											2025-07-02 20:10:54 +08:00
										 |  |  | 	require("../../schemas/plugins/json/JsonModulesPluginGenerator.check"), | 
					
						
							| 
									
										
										
										
											2025-05-01 22:36:51 +08:00
										 |  |  | 	() => require("../../schemas/plugins/json/JsonModulesPluginGenerator.json"), | 
					
						
							| 
									
										
										
										
											2025-03-13 22:24:10 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		name: "Json Modules Plugin", | 
					
						
							|  |  |  | 		baseDataPath: "generator" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-01 00:26:38 +08:00
										 |  |  | const PLUGIN_NAME = "JsonModulesPlugin"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * The JsonModulesPlugin is the entrypoint plugin for the json modules feature. | 
					
						
							|  |  |  |  * It adds the json module type to the compiler and registers the json parser and generator. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-11-12 01:48:29 +08:00
										 |  |  | class JsonModulesPlugin { | 
					
						
							| 
									
										
										
										
											2018-11-03 04:05:46 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Apply the plugin | 
					
						
							|  |  |  | 	 * @param {Compiler} compiler the compiler instance | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2017-11-12 01:48:29 +08:00
										 |  |  | 	apply(compiler) { | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 		compiler.hooks.compilation.tap( | 
					
						
							| 
									
										
										
										
											2023-04-01 00:26:38 +08:00
										 |  |  | 			PLUGIN_NAME, | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			(compilation, { normalModuleFactory }) => { | 
					
						
							|  |  |  | 				normalModuleFactory.hooks.createParser | 
					
						
							| 
									
										
										
										
											2023-04-01 00:26:38 +08:00
										 |  |  | 					.for(JSON_MODULE_TYPE) | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 					.tap(PLUGIN_NAME, (parserOptions) => { | 
					
						
							| 
									
										
										
										
											2021-04-16 21:35:18 +08:00
										 |  |  | 						validate(parserOptions); | 
					
						
							| 
									
										
										
										
											2024-12-14 01:14:02 +08:00
										 |  |  | 						return new JsonParser(parserOptions); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 				normalModuleFactory.hooks.createGenerator | 
					
						
							| 
									
										
										
										
											2023-04-01 00:26:38 +08:00
										 |  |  | 					.for(JSON_MODULE_TYPE) | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 					.tap(PLUGIN_NAME, (generatorOptions) => { | 
					
						
							| 
									
										
										
										
											2025-03-13 22:24:10 +08:00
										 |  |  | 						validateGenerator(generatorOptions); | 
					
						
							|  |  |  | 						return new JsonGenerator(generatorOptions); | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-11-12 01:48:29 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = JsonModulesPlugin; |