選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ModuleParseError.js 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. /** @typedef {import("./Module")} Module */
  8. class ModuleParseError extends WebpackError {
  9. /**
  10. * @param {Module} module the errored module
  11. * @param {string} source source code
  12. * @param {Error&any} err the parse error
  13. * @param {string[]} loaders the loaders used
  14. */
  15. constructor(module, source, err, loaders) {
  16. let message = "Module parse failed: " + err.message;
  17. let loc = undefined;
  18. if (loaders.length >= 1) {
  19. message += `\nFile was processed with these loaders:${loaders
  20. .map(loader => `\n * ${loader}`)
  21. .join("")}`;
  22. message +=
  23. "\nYou may need an additional loader to handle the result of these loaders.";
  24. } else {
  25. message +=
  26. "\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";
  27. }
  28. if (
  29. err.loc &&
  30. typeof err.loc === "object" &&
  31. typeof err.loc.line === "number"
  32. ) {
  33. var lineNumber = err.loc.line;
  34. if (/[\0\u0001\u0002\u0003\u0004\u0005\u0006\u0007]/.test(source)) {
  35. // binary file
  36. message += "\n(Source code omitted for this binary file)";
  37. } else {
  38. const sourceLines = source.split(/\r?\n/);
  39. const start = Math.max(0, lineNumber - 3);
  40. const linesBefore = sourceLines.slice(start, lineNumber - 1);
  41. const theLine = sourceLines[lineNumber - 1];
  42. const linesAfter = sourceLines.slice(lineNumber, lineNumber + 2);
  43. message +=
  44. linesBefore.map(l => `\n| ${l}`).join("") +
  45. `\n> ${theLine}` +
  46. linesAfter.map(l => `\n| ${l}`).join("");
  47. }
  48. loc = err.loc;
  49. } else {
  50. message += "\n" + err.stack;
  51. }
  52. super(message);
  53. this.name = "ModuleParseError";
  54. this.module = module;
  55. this.loc = loc;
  56. this.error = err;
  57. Error.captureStackTrace(this, this.constructor);
  58. }
  59. }
  60. module.exports = ModuleParseError;