ImportPlugin.js 2.6 KB

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