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.

88 lines
2.7 KiB

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