ModuleParseError.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. /** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
  9. /** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
  10. const WASM_HEADER = Buffer.from([0x00, 0x61, 0x73, 0x6d]);
  11. class ModuleParseError extends WebpackError {
  12. /**
  13. * @param {string | Buffer} source source code
  14. * @param {Error&any} err the parse error
  15. * @param {string[]} loaders the loaders used
  16. * @param {string} type module type
  17. */
  18. constructor(source, err, loaders, type) {
  19. let message = "Module parse failed: " + (err && err.message);
  20. let loc = undefined;
  21. if (
  22. ((Buffer.isBuffer(source) && source.slice(0, 4).equals(WASM_HEADER)) ||
  23. (typeof source === "string" && /^\0asm/.test(source))) &&
  24. !type.startsWith("webassembly")
  25. ) {
  26. message +=
  27. "\nThe module seem to be a WebAssembly module, but module is not flagged as WebAssembly module for webpack.";
  28. message +=
  29. "\nBREAKING CHANGE: Since webpack 5 WebAssembly is not enabled by default and flagged as experimental feature.";
  30. message +=
  31. "\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).";
  32. message +=
  33. "\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\"').";
  34. } else if (!loaders) {
  35. message +=
  36. "\nYou may need an appropriate loader to handle this file type.";
  37. } else if (loaders.length >= 1) {
  38. message += `\nFile was processed with these loaders:${loaders
  39. .map(loader => `\n * ${loader}`)
  40. .join("")}`;
  41. message +=
  42. "\nYou may need an additional loader to handle the result of these loaders.";
  43. } else {
  44. message +=
  45. "\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";
  46. }
  47. if (
  48. err &&
  49. err.loc &&
  50. typeof err.loc === "object" &&
  51. typeof err.loc.line === "number"
  52. ) {
  53. var lineNumber = err.loc.line;
  54. if (
  55. Buffer.isBuffer(source) ||
  56. /[\0\u0001\u0002\u0003\u0004\u0005\u0006\u0007]/.test(source)
  57. ) {
  58. // binary file
  59. message += "\n(Source code omitted for this binary file)";
  60. } else {
  61. const sourceLines = source.split(/\r?\n/);
  62. const start = Math.max(0, lineNumber - 3);
  63. const linesBefore = sourceLines.slice(start, lineNumber - 1);
  64. const theLine = sourceLines[lineNumber - 1];
  65. const linesAfter = sourceLines.slice(lineNumber, lineNumber + 2);
  66. message +=
  67. linesBefore.map(l => `\n| ${l}`).join("") +
  68. `\n> ${theLine}` +
  69. linesAfter.map(l => `\n| ${l}`).join("");
  70. }
  71. loc = { start: err.loc };
  72. } else if (err && err.stack) {
  73. message += "\n" + err.stack;
  74. }
  75. super(message);
  76. this.name = "ModuleParseError";
  77. this.loc = loc;
  78. this.error = err;
  79. }
  80. /**
  81. * @param {ObjectSerializerContext} context context
  82. */
  83. serialize(context) {
  84. const { write } = context;
  85. write(this.error);
  86. super.serialize(context);
  87. }
  88. /**
  89. * @param {ObjectDeserializerContext} context context
  90. */
  91. deserialize(context) {
  92. const { read } = context;
  93. this.error = read();
  94. super.deserialize(context);
  95. }
  96. }
  97. makeSerializable(ModuleParseError, "webpack/lib/ModuleParseError");
  98. module.exports = ModuleParseError;