WebAssemblyInInitialChunkError.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. */
  4. "use strict";
  5. const WebpackError = require("../WebpackError");
  6. /** @typedef {import("../ChunkGraph")} ChunkGraph */
  7. /** @typedef {import("../Module")} Module */
  8. /** @typedef {import("../ModuleGraph")} ModuleGraph */
  9. /** @typedef {import("../RequestShortener")} RequestShortener */
  10. /**
  11. * @param {Module} module module to get chains from
  12. * @param {ModuleGraph} moduleGraph the module graph
  13. * @param {ChunkGraph} chunkGraph the chunk graph
  14. * @param {RequestShortener} requestShortener to make readable identifiers
  15. * @returns {string[]} all chains to the module
  16. */
  17. const getInitialModuleChains = (
  18. module,
  19. moduleGraph,
  20. chunkGraph,
  21. requestShortener
  22. ) => {
  23. const queue = [
  24. { head: module, message: module.readableIdentifier(requestShortener) }
  25. ];
  26. /** @type {Set<string>} */
  27. const results = new Set();
  28. /** @type {Set<string>} */
  29. const incompleteResults = new Set();
  30. /** @type {Set<Module>} */
  31. const visitedModules = new Set();
  32. for (const chain of queue) {
  33. const { head, message } = chain;
  34. let final = true;
  35. /** @type {Set<Module>} */
  36. const alreadyReferencedModules = new Set();
  37. for (const connection of moduleGraph.getIncomingConnections(head)) {
  38. const newHead = connection.originModule;
  39. if (newHead) {
  40. if (!chunkGraph.getModuleChunks(newHead).some(c => c.canBeInitial()))
  41. continue;
  42. final = false;
  43. if (alreadyReferencedModules.has(newHead)) continue;
  44. alreadyReferencedModules.add(newHead);
  45. const moduleName = newHead.readableIdentifier(requestShortener);
  46. const detail = connection.explanation
  47. ? ` (${connection.explanation})`
  48. : "";
  49. const newMessage = `${moduleName}${detail} --> ${message}`;
  50. if (visitedModules.has(newHead)) {
  51. incompleteResults.add(`... --> ${newMessage}`);
  52. continue;
  53. }
  54. visitedModules.add(newHead);
  55. queue.push({
  56. head: newHead,
  57. message: newMessage
  58. });
  59. } else {
  60. final = false;
  61. const newMessage = connection.explanation
  62. ? `(${connection.explanation}) --> ${message}`
  63. : message;
  64. results.add(newMessage);
  65. }
  66. }
  67. if (final) {
  68. results.add(message);
  69. }
  70. }
  71. for (const result of incompleteResults) {
  72. results.add(result);
  73. }
  74. return Array.from(results);
  75. };
  76. module.exports = class WebAssemblyInInitialChunkError extends WebpackError {
  77. /**
  78. * @param {Module} module WASM module
  79. * @param {ModuleGraph} moduleGraph the module graph
  80. * @param {ChunkGraph} chunkGraph the chunk graph
  81. * @param {RequestShortener} requestShortener request shortener
  82. */
  83. constructor(module, moduleGraph, chunkGraph, requestShortener) {
  84. const moduleChains = getInitialModuleChains(
  85. module,
  86. moduleGraph,
  87. chunkGraph,
  88. requestShortener
  89. );
  90. const message = `WebAssembly module is included in initial chunk.
  91. This is not allowed, because WebAssembly download and compilation must happen asynchronous.
  92. Add an async split point (i. e. import()) somewhere between your entrypoint and the WebAssembly module:
  93. ${moduleChains.map(s => `* ${s}`).join("\n")}`;
  94. super(message);
  95. this.name = "WebAssemblyInInitialChunkError";
  96. this.hideStack = true;
  97. this.module = module;
  98. }
  99. };