1 |
|
2 |
|
3 |
|
4 |
|
5 | "use strict";
|
6 |
|
7 | const WebpackError = require("./WebpackError");
|
8 |
|
9 |
|
10 |
|
11 | class ModuleParseError extends WebpackError {
|
12 | |
13 |
|
14 |
|
15 |
|
16 |
|
17 | constructor(module, source, err) {
|
18 | let message = "Module parse failed: " + err.message;
|
19 | let loc = undefined;
|
20 | message += "\nYou may need an appropriate loader to handle this file type.";
|
21 | if (
|
22 | err.loc &&
|
23 | typeof err.loc === "object" &&
|
24 | typeof err.loc.line === "number"
|
25 | ) {
|
26 | var lineNumber = err.loc.line;
|
27 | if (/[\0\u0001\u0002\u0003\u0004\u0005\u0006\u0007]/.test(source)) {
|
28 |
|
29 | message += "\n(Source code omitted for this binary file)";
|
30 | } else {
|
31 | const sourceLines = source.split("\n");
|
32 | const start = Math.max(0, lineNumber - 3);
|
33 | const linesBefore = sourceLines.slice(start, lineNumber - 1);
|
34 | const theLine = sourceLines[lineNumber - 1];
|
35 | const linesAfter = sourceLines.slice(lineNumber, lineNumber + 2);
|
36 | message +=
|
37 | linesBefore.map(l => `\n| ${l}`).join("") +
|
38 | `\n> ${theLine}` +
|
39 | linesAfter.map(l => `\n| ${l}`).join("");
|
40 | }
|
41 | loc = err.loc;
|
42 | } else {
|
43 | message += "\n" + err.stack;
|
44 | }
|
45 |
|
46 | super(message);
|
47 |
|
48 | this.name = "ModuleParseError";
|
49 | this.module = module;
|
50 | this.loc = loc;
|
51 | this.error = err;
|
52 |
|
53 | Error.captureStackTrace(this, this.constructor);
|
54 | }
|
55 | }
|
56 |
|
57 | module.exports = ModuleParseError;
|