You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.0 KiB

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