ImportPlugin.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const ImportContextDependency = require("./ImportContextDependency");
  7. const ImportDependency = require("./ImportDependency");
  8. const ImportEagerDependency = require("./ImportEagerDependency");
  9. const ImportParserPlugin = require("./ImportParserPlugin");
  10. const ImportWeakDependency = require("./ImportWeakDependency");
  11. /** @typedef {import("../Compiler")} Compiler */
  12. class ImportPlugin {
  13. /**
  14. * Apply the plugin
  15. * @param {Compiler} compiler the compiler instance
  16. * @returns {void}
  17. */
  18. apply(compiler) {
  19. compiler.hooks.compilation.tap(
  20. "ImportPlugin",
  21. (compilation, { contextModuleFactory, normalModuleFactory }) => {
  22. compilation.dependencyFactories.set(
  23. ImportDependency,
  24. normalModuleFactory
  25. );
  26. compilation.dependencyTemplates.set(
  27. ImportDependency,
  28. new ImportDependency.Template()
  29. );
  30. compilation.dependencyFactories.set(
  31. ImportEagerDependency,
  32. normalModuleFactory
  33. );
  34. compilation.dependencyTemplates.set(
  35. ImportEagerDependency,
  36. new ImportEagerDependency.Template()
  37. );
  38. compilation.dependencyFactories.set(
  39. ImportWeakDependency,
  40. normalModuleFactory
  41. );
  42. compilation.dependencyTemplates.set(
  43. ImportWeakDependency,
  44. new ImportWeakDependency.Template()
  45. );
  46. compilation.dependencyFactories.set(
  47. ImportContextDependency,
  48. contextModuleFactory
  49. );
  50. compilation.dependencyTemplates.set(
  51. ImportContextDependency,
  52. new ImportContextDependency.Template()
  53. );
  54. const handler = (parser, parserOptions) => {
  55. if (parserOptions.import !== undefined && !parserOptions.import)
  56. return;
  57. new ImportParserPlugin(parserOptions).apply(parser);
  58. };
  59. normalModuleFactory.hooks.parser
  60. .for("javascript/auto")
  61. .tap("ImportPlugin", handler);
  62. normalModuleFactory.hooks.parser
  63. .for("javascript/dynamic")
  64. .tap("ImportPlugin", handler);
  65. normalModuleFactory.hooks.parser
  66. .for("javascript/esm")
  67. .tap("ImportPlugin", handler);
  68. }
  69. );
  70. }
  71. }
  72. module.exports = ImportPlugin;