ModuleParseError.js 3.9 KB

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