| 
									
										
										
										
											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"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-29 01:12:11 +08:00
										 |  |  | const validateOptions = require("schema-utils"); | 
					
						
							| 
									
										
										
										
											2019-12-04 01:48:31 +08:00
										 |  |  | const memorize = require("../util/memorize"); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-04 01:31:39 +08:00
										 |  |  | const getParserSchema = memorize(() => | 
					
						
							|  |  |  | 	require("../../schemas/plugins/JsonModulesPluginParser.json") | 
					
						
							| 
									
										
										
										
											2019-12-02 23:37:55 +08:00
										 |  |  | ); | 
					
						
							| 
									
										
										
										
											2019-11-29 01:12:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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( | 
					
						
							|  |  |  | 			"JsonModulesPlugin", | 
					
						
							|  |  |  | 			(compilation, { normalModuleFactory }) => { | 
					
						
							|  |  |  | 				normalModuleFactory.hooks.createParser | 
					
						
							|  |  |  | 					.for("json") | 
					
						
							| 
									
										
										
										
											2019-11-29 01:12:11 +08:00
										 |  |  | 					.tap("JsonModulesPlugin", parserOptions => { | 
					
						
							| 
									
										
										
										
											2019-12-04 01:31:39 +08:00
										 |  |  | 						validateOptions(getParserSchema(), parserOptions, { | 
					
						
							| 
									
										
										
										
											2019-11-29 01:12:11 +08:00
										 |  |  | 							name: "Json Modules Plugin", | 
					
						
							|  |  |  | 							baseDataPath: "parser" | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						return new JsonParser(parserOptions); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 				normalModuleFactory.hooks.createGenerator | 
					
						
							|  |  |  | 					.for("json") | 
					
						
							|  |  |  | 					.tap("JsonModulesPlugin", () => { | 
					
						
							|  |  |  | 						return new JsonGenerator(); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2017-11-12 01:48:29 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = JsonModulesPlugin; |