JsonModulesPlugin.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const createSchemaValidation = require("../util/create-schema-validation");
  7. const JsonGenerator = require("./JsonGenerator");
  8. const JsonParser = require("./JsonParser");
  9. /** @typedef {import("../Compiler")} Compiler */
  10. const validate = createSchemaValidation(
  11. require("../../schemas/plugins/JsonModulesPluginParser.check.js"),
  12. () => require("../../schemas/plugins/JsonModulesPluginParser.json"),
  13. {
  14. name: "Json Modules Plugin",
  15. baseDataPath: "parser"
  16. }
  17. );
  18. class JsonModulesPlugin {
  19. /**
  20. * Apply the plugin
  21. * @param {Compiler} compiler the compiler instance
  22. * @returns {void}
  23. */
  24. apply(compiler) {
  25. compiler.hooks.compilation.tap(
  26. "JsonModulesPlugin",
  27. (compilation, { normalModuleFactory }) => {
  28. normalModuleFactory.hooks.createParser
  29. .for("json")
  30. .tap("JsonModulesPlugin", parserOptions => {
  31. validate(parserOptions);
  32. return new JsonParser(parserOptions);
  33. });
  34. normalModuleFactory.hooks.createGenerator
  35. .for("json")
  36. .tap("JsonModulesPlugin", () => {
  37. return new JsonGenerator();
  38. });
  39. }
  40. );
  41. }
  42. }
  43. module.exports = JsonModulesPlugin;