JsonModulesPlugin.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { JSON_MODULE_TYPE } = require("../ModuleTypeConstants");
  7. const JsonGenerator = require("./JsonGenerator");
  8. const JsonModule = require("./JsonModule");
  9. const JsonParser = require("./JsonParser");
  10. /** @import Compiler from "../Compiler" */
  11. const PLUGIN_NAME = "JsonModulesPlugin";
  12. /**
  13. * The JsonModulesPlugin is the entrypoint plugin for the json modules feature.
  14. * It adds the json module type to the compiler and registers the json parser and generator.
  15. */
  16. class JsonModulesPlugin {
  17. /**
  18. * Applies the plugin by registering its hooks on the compiler.
  19. * @param {Compiler} compiler the compiler instance
  20. * @returns {void}
  21. */
  22. apply(compiler) {
  23. compiler.hooks.compilation.tap(
  24. PLUGIN_NAME,
  25. (compilation, { normalModuleFactory }) => {
  26. normalModuleFactory.hooks.createModuleClass
  27. .for(JSON_MODULE_TYPE)
  28. .tap(
  29. PLUGIN_NAME,
  30. (createData, _resolveData) => new JsonModule(createData)
  31. );
  32. normalModuleFactory.hooks.createParser
  33. .for(JSON_MODULE_TYPE)
  34. .tap(PLUGIN_NAME, (parserOptions) => {
  35. compiler.validate(
  36. () =>
  37. require("../../schemas/plugins/json/JsonModulesPluginParser.json"),
  38. parserOptions,
  39. {
  40. name: "Json Modules Plugin",
  41. baseDataPath: "parser"
  42. },
  43. (options) =>
  44. require("../../schemas/plugins/json/JsonModulesPluginParser.check")(
  45. options
  46. )
  47. );
  48. return new JsonParser(parserOptions);
  49. });
  50. normalModuleFactory.hooks.createGenerator
  51. .for(JSON_MODULE_TYPE)
  52. .tap(PLUGIN_NAME, (generatorOptions) => {
  53. compiler.validate(
  54. () =>
  55. require("../../schemas/plugins/json/JsonModulesPluginGenerator.json"),
  56. generatorOptions,
  57. {
  58. name: "Json Modules Plugin",
  59. baseDataPath: "generator"
  60. },
  61. (options) =>
  62. require("../../schemas/plugins/json/JsonModulesPluginGenerator.check")(
  63. options
  64. )
  65. );
  66. return new JsonGenerator(generatorOptions);
  67. });
  68. }
  69. );
  70. }
  71. }
  72. module.exports = JsonModulesPlugin;