ModuleParseError.js 1.1KB

1234567891011121314151617181920212223242526272829303132333435
  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. class ModuleParseError extends WebpackError {
  8. constructor(module, source, err) {
  9. super();
  10. this.name = "ModuleParseError";
  11. this.message = "Module parse failed: " + err.message;
  12. this.message += "\nYou may need an appropriate loader to handle this file type.";
  13. if(err.loc && typeof err.loc === "object" && typeof err.loc.line === "number") {
  14. var lineNumber = err.loc.line;
  15. if(/[\0\u0001\u0002\u0003\u0004\u0005\u0006\u0007]/.test(source)) { // binary file
  16. this.message += "\n(Source code omitted for this binary file)";
  17. } else {
  18. source = source.split("\n");
  19. this.message += "\n| " + source.slice(Math.max(0, lineNumber - 3), lineNumber + 2).join("\n| ");
  20. }
  21. } else {
  22. this.message += "\n" + err.stack;
  23. }
  24. this.module = module;
  25. this.error = err;
  26. Error.captureStackTrace(this, this.constructor);
  27. }
  28. }
  29. module.exports = ModuleParseError;