InferAsyncModulesPlugin.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const HarmonyImportDependency = require("../dependencies/HarmonyImportDependency");
  7. /** @typedef {import("../Compiler")} Compiler */
  8. /** @typedef {import("../Module")} Module */
  9. const PLUGIN_NAME = "InferAsyncModulesPlugin";
  10. class InferAsyncModulesPlugin {
  11. /**
  12. * Apply the plugin
  13. * @param {Compiler} compiler the compiler instance
  14. * @returns {void}
  15. */
  16. apply(compiler) {
  17. compiler.hooks.compilation.tap(PLUGIN_NAME, compilation => {
  18. const { moduleGraph } = compilation;
  19. compilation.hooks.finishModules.tap(PLUGIN_NAME, modules => {
  20. /** @type {Set<Module>} */
  21. const queue = new Set();
  22. for (const module of modules) {
  23. if (module.buildMeta && module.buildMeta.async) {
  24. queue.add(module);
  25. }
  26. }
  27. for (const module of queue) {
  28. moduleGraph.setAsync(module);
  29. for (const [
  30. originModule,
  31. connections
  32. ] of moduleGraph.getIncomingConnectionsByOriginModule(module)) {
  33. if (
  34. connections.some(
  35. c =>
  36. c.dependency instanceof HarmonyImportDependency &&
  37. c.isTargetActive(undefined)
  38. )
  39. ) {
  40. queue.add(/** @type {Module} */ (originModule));
  41. }
  42. }
  43. }
  44. });
  45. });
  46. }
  47. }
  48. module.exports = InferAsyncModulesPlugin;