JsonModulesPlugin.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 createSchemaValidation = require("../util/create-schema-validation");
  8. const JsonGenerator = require("./JsonGenerator");
  9. const JsonParser = require("./JsonParser");
  10. /** @typedef {import("../Compiler")} Compiler */
  11. const validate = createSchemaValidation(
  12. require("../../schemas/plugins/JsonModulesPluginParser.check.js"),
  13. () => require("../../schemas/plugins/JsonModulesPluginParser.json"),
  14. {
  15. name: "Json Modules Plugin",
  16. baseDataPath: "parser"
  17. }
  18. );
  19. const PLUGIN_NAME = "JsonModulesPlugin";
  20. /**
  21. * The JsonModulesPlugin is the entrypoint plugin for the json modules feature.
  22. * It adds the json module type to the compiler and registers the json parser and generator.
  23. */
  24. class JsonModulesPlugin {
  25. /**
  26. * Apply the plugin
  27. * @param {Compiler} compiler the compiler instance
  28. * @returns {void}
  29. *
  30. */
  31. apply(compiler) {
  32. compiler.hooks.compilation.tap(
  33. PLUGIN_NAME,
  34. (compilation, { normalModuleFactory }) => {
  35. normalModuleFactory.hooks.createParser
  36. .for(JSON_MODULE_TYPE)
  37. .tap(PLUGIN_NAME, parserOptions => {
  38. validate(parserOptions);
  39. return new JsonParser(parserOptions);
  40. });
  41. normalModuleFactory.hooks.createGenerator
  42. .for(JSON_MODULE_TYPE)
  43. .tap(PLUGIN_NAME, () => {
  44. return new JsonGenerator();
  45. });
  46. }
  47. );
  48. }
  49. }
  50. module.exports = JsonModulesPlugin;