ModuleParseError.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const WebpackError = require("./WebpackError");
  7. const makeSerializable = require("./util/makeSerializable");
  8. const WASM_HEADER = Buffer.from([0x00, 0x61, 0x73, 0x6d]);
  9. class ModuleParseError extends WebpackError {
  10. /**
  11. * @param {string | Buffer} source source code
  12. * @param {Error&any} err the parse error
  13. * @param {string[]} loaders the loaders used
  14. * @param {string} type module type
  15. */
  16. constructor(source, err, loaders, type) {
  17. let message = "Module parse failed: " + (err && err.message);
  18. let loc = undefined;
  19. if (
  20. ((Buffer.isBuffer(source) && source.slice(0, 4).equals(WASM_HEADER)) ||
  21. (typeof source === "string" && /^\0asm/.test(source))) &&
  22. !type.startsWith("webassembly")
  23. ) {
  24. message +=
  25. "\nThe module seem to be a WebAssembly module, but module is not flagged as WebAssembly module for webpack.";
  26. message +=
  27. "\nBREAKING CHANGE: Since webpack 5 WebAssembly is not enabled by default and flagged as experimental feature.";
  28. message +=
  29. "\nYou need to enable one of the WebAssembly experiments via 'experiments.asyncWebAssembly: true' (based on async modules) or 'experiments.syncWebAssembly: true' (like webpack 4, deprecated).";
  30. message +=
  31. "\nFor files that transpile to WebAssembly, make sure to set the module type in the 'module.rules' section of the config (e. g. 'type: \"webassembly/async\"').";
  32. } else if (!loaders) {
  33. message +=
  34. "\nYou may need an appropriate loader to handle this file type.";
  35. } else if (loaders.length >= 1) {
  36. message += `\nFile was processed with these loaders:${loaders
  37. .map(loader => `\n * ${loader}`)
  38. .join("")}`;
  39. message +=
  40. "\nYou may need an additional loader to handle the result of these loaders.";
  41. } else {
  42. message +=
  43. "\nYou may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders";
  44. }
  45. if (
  46. err &&
  47. err.loc &&
  48. typeof err.loc === "object" &&
  49. typeof err.loc.line === "number"
  50. ) {
  51. var lineNumber = err.loc.line;
  52. if (
  53. Buffer.isBuffer(source) ||
  54. /[\0\u0001\u0002\u0003\u0004\u0005\u0006\u0007]/.test(source)
  55. ) {
  56. // binary file
  57. message += "\n(Source code omitted for this binary file)";
  58. } else {
  59. const sourceLines = source.split(/\r?\n/);
  60. const start = Math.max(0, lineNumber - 3);
  61. const linesBefore = sourceLines.slice(start, lineNumber - 1);
  62. const theLine = sourceLines[lineNumber - 1];
  63. const linesAfter = sourceLines.slice(lineNumber, lineNumber + 2);
  64. message +=
  65. linesBefore.map(l => `\n| ${l}`).join("") +
  66. `\n> ${theLine}` +
  67. linesAfter.map(l => `\n| ${l}`).join("");
  68. }
  69. loc = { start: err.loc };
  70. } else if (err && err.stack) {
  71. message += "\n" + err.stack;
  72. }
  73. super(message);
  74. this.name = "ModuleParseError";
  75. this.loc = loc;
  76. this.error = err;
  77. }
  78. serialize(context) {
  79. const { write } = context;
  80. write(this.error);
  81. super.serialize(context);
  82. }
  83. deserialize(context) {
  84. const { read } = context;
  85. this.error = read();
  86. super.deserialize(context);
  87. }
  88. }
  89. makeSerializable(ModuleParseError, "webpack/lib/ModuleParseError");
  90. module.exports = ModuleParseError;