InferAsyncModulesPlugin.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. class InferAsyncModulesPlugin {
  10. /**
  11. * Apply the plugin
  12. * @param {Compiler} compiler the compiler instance
  13. * @returns {void}
  14. */
  15. apply(compiler) {
  16. compiler.hooks.compilation.tap("InferAsyncModulesPlugin", compilation => {
  17. const { moduleGraph } = compilation;
  18. compilation.hooks.finishModules.tap(
  19. "InferAsyncModulesPlugin",
  20. modules => {
  21. /** @type {Set<Module>} */
  22. const queue = new Set();
  23. for (const module of modules) {
  24. if (module.buildMeta && module.buildMeta.async) {
  25. queue.add(module);
  26. }
  27. }
  28. for (const module of queue) {
  29. moduleGraph.setAsync(module);
  30. for (const [
  31. originModule,
  32. connections
  33. ] of moduleGraph.getIncomingConnectionsByOriginModule(module)) {
  34. if (
  35. connections.some(
  36. c =>
  37. c.dependency instanceof HarmonyImportDependency &&
  38. c.isTargetActive(undefined)
  39. )
  40. ) {
  41. queue.add(originModule);
  42. }
  43. }
  44. }
  45. }
  46. );
  47. });
  48. }
  49. }
  50. module.exports = InferAsyncModulesPlugin;