1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const WebpackError = require("./WebpackError");
|
---|
9 | const makeSerializable = require("./util/makeSerializable");
|
---|
10 |
|
---|
11 | const WASM_HEADER = Buffer.from([0x00, 0x61, 0x73, 0x6d]);
|
---|
12 |
|
---|
13 | class ModuleParseError extends WebpackError {
|
---|
14 | /**
|
---|
15 | * @param {string | Buffer} source source code
|
---|
16 | * @param {Error&any} 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 | let loc = undefined;
|
---|
23 |
|
---|
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 | } else if (loaders.length >= 1) {
|
---|
41 | message += `\nFile was processed with these loaders:${loaders
|
---|
42 | .map(loader => `\n * ${loader}`)
|
---|
43 | .join("")}`;
|
---|
44 | message +=
|
---|
45 | "\nYou may need an additional loader to handle the result of these loaders.";
|
---|
46 | } else {
|
---|
47 | message +=
|
---|
48 | "\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";
|
---|
49 | }
|
---|
50 |
|
---|
51 | if (
|
---|
52 | err &&
|
---|
53 | err.loc &&
|
---|
54 | typeof err.loc === "object" &&
|
---|
55 | typeof err.loc.line === "number"
|
---|
56 | ) {
|
---|
57 | var lineNumber = err.loc.line;
|
---|
58 |
|
---|
59 | if (
|
---|
60 | Buffer.isBuffer(source) ||
|
---|
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 |
|
---|
72 | message +=
|
---|
73 | linesBefore.map(l => `\n| ${l}`).join("") +
|
---|
74 | `\n> ${theLine}` +
|
---|
75 | linesAfter.map(l => `\n| ${l}`).join("");
|
---|
76 | }
|
---|
77 |
|
---|
78 | loc = { start: err.loc };
|
---|
79 | } else if (err && err.stack) {
|
---|
80 | message += "\n" + err.stack;
|
---|
81 | }
|
---|
82 |
|
---|
83 | super(message);
|
---|
84 |
|
---|
85 | this.name = "ModuleParseError";
|
---|
86 | this.loc = loc;
|
---|
87 | this.error = err;
|
---|
88 | }
|
---|
89 |
|
---|
90 | serialize(context) {
|
---|
91 | const { write } = context;
|
---|
92 |
|
---|
93 | write(this.error);
|
---|
94 |
|
---|
95 | super.serialize(context);
|
---|
96 | }
|
---|
97 |
|
---|
98 | deserialize(context) {
|
---|
99 | const { read } = context;
|
---|
100 |
|
---|
101 | this.error = read();
|
---|
102 |
|
---|
103 | super.deserialize(context);
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | makeSerializable(ModuleParseError, "webpack/lib/ModuleParseError");
|
---|
108 |
|
---|
109 | module.exports = ModuleParseError;
|
---|