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