JsonModulesPlugin.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 JsonParser = require("./JsonParser");
  9. /** @typedef {import("../Compiler")} Compiler */
  10. const PLUGIN_NAME = "JsonModulesPlugin";
  11. /**
  12. * The JsonModulesPlugin is the entrypoint plugin for the json modules feature.
  13. * It adds the json module type to the compiler and registers the json parser and generator.
  14. */
  15. class JsonModulesPlugin {
  16. /**
  17. * Applies the plugin by registering its hooks on the compiler.
  18. * @param {Compiler} compiler the compiler instance
  19. * @returns {void}
  20. */
  21. apply(compiler) {
  22. compiler.hooks.compilation.tap(
  23. PLUGIN_NAME,
  24. (compilation, { normalModuleFactory }) => {
  25. normalModuleFactory.hooks.createParser
  26. .for(JSON_MODULE_TYPE)
  27. .tap(PLUGIN_NAME, (parserOptions) => {
  28. compiler.validate(
  29. () =>
  30. require("../../schemas/plugins/json/JsonModulesPluginParser.json"),
  31. parserOptions,
  32. {
  33. name: "Json Modules Plugin",
  34. baseDataPath: "parser"
  35. },
  36. (options) =>
  37. require("../../schemas/plugins/json/JsonModulesPluginParser.check")(
  38. options
  39. )
  40. );
  41. return new JsonParser(parserOptions);
  42. });
  43. normalModuleFactory.hooks.createGenerator
  44. .for(JSON_MODULE_TYPE)
  45. .tap(PLUGIN_NAME, (generatorOptions) => {
  46. compiler.validate(
  47. () =>
  48. require("../../schemas/plugins/json/JsonModulesPluginGenerator.json"),
  49. generatorOptions,
  50. {
  51. name: "Json Modules Plugin",
  52. baseDataPath: "generator"
  53. },
  54. (options) =>
  55. require("../../schemas/plugins/json/JsonModulesPluginGenerator.check")(
  56. options
  57. )
  58. );
  59. return new JsonGenerator(generatorOptions);
  60. });
  61. }
  62. );
  63. }
  64. }
  65. module.exports = JsonModulesPlugin;