|
| 1 | +// Turning one of webpack's problems into something to read. |
| 2 | +// |
| 3 | +// The middleware formats a build's errors and warnings on the server, so its |
| 4 | +// own payloads carry strings and its client never needs this. A server that |
| 5 | +// sends webpack's error objects to the browser instead — which is what |
| 6 | +// webpack-dev-server does — needs the same shape built there, and had to |
| 7 | +// write it itself. Here it is once. |
| 8 | +// |
| 9 | +// Compiled to an ES5 baseline like the rest of the browser runtime, so |
| 10 | +// nothing here is newer than that. |
| 11 | + |
| 12 | +// eslint-disable-next-line jsdoc/reject-any-type |
| 13 | +/** @typedef {any} EXPECTED_ANY */ |
| 14 | + |
| 15 | +/** |
| 16 | + * One of webpack's errors or warnings, as `stats.toJson()` reports it. Only |
| 17 | + * the fields that say something about where and what; anything else on a |
| 18 | + * `StatsError` is ignored. |
| 19 | + * @typedef {object} Problem |
| 20 | + * @property {string=} file the file the problem is in |
| 21 | + * @property {string=} moduleName the module's request, loaders and all |
| 22 | + * @property {string=} loc line and column within the module |
| 23 | + * @property {string=} message what went wrong |
| 24 | + * @property {(string[] | EXPECTED_ANY)=} stack frames webpack attached, when it did |
| 25 | + */ |
| 26 | + |
| 27 | +/** |
| 28 | + * Where a problem happened, as one line. |
| 29 | + * |
| 30 | + * A module processed by loaders reports its whole request as `moduleName` |
| 31 | + * (`babel-loader!./src/app.js`), which reads as noise where the file is what |
| 32 | + * matters — so the file comes first and the request follows it in brackets. |
| 33 | + * Empty when webpack said neither, rather than a line with nothing on it. |
| 34 | + * @param {string | Problem} item a problem, or a message on its own |
| 35 | + * @returns {string} the location, or an empty string |
| 36 | + */ |
| 37 | +export function problemLocation(item) { |
| 38 | + if (typeof item === "string") { |
| 39 | + return ""; |
| 40 | + } |
| 41 | + |
| 42 | + const file = item.file || ""; |
| 43 | + const request = item.moduleName || ""; |
| 44 | + // `indexOf`, not `includes`: this file is compiled to an ES5 baseline. |
| 45 | + const loaded = request.indexOf("!") !== -1; |
| 46 | + // The module itself, with the loaders that built it stripped off. |
| 47 | + const moduleName = loaded ? request.replace(/^(\s|\S)*!/, "") : request; |
| 48 | + |
| 49 | + if (!moduleName && !file) { |
| 50 | + return ""; |
| 51 | + } |
| 52 | + |
| 53 | + let where = moduleName || file; |
| 54 | + |
| 55 | + // The full request, when loaders made it something other than the module. |
| 56 | + if (loaded) { |
| 57 | + where += ` (${request})`; |
| 58 | + } |
| 59 | + |
| 60 | + // ... and the file, when webpack named one and it is not already what is |
| 61 | + // shown. It usually names none, and when it does it is often the module |
| 62 | + // itself — appending it either way read as `./a.js (./a.js)`. |
| 63 | + if (moduleName && file && file !== moduleName) { |
| 64 | + where += ` (${file})`; |
| 65 | + } |
| 66 | + |
| 67 | + return `${where}${item.loc ? ` ${item.loc}` : ""}`; |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * What a problem says, with any stack webpack attached under it. |
| 72 | + * @param {string | Problem} item a problem, or a message on its own |
| 73 | + * @returns {string} the message |
| 74 | + */ |
| 75 | +export function problemBody(item) { |
| 76 | + if (typeof item === "string") { |
| 77 | + return item; |
| 78 | + } |
| 79 | + |
| 80 | + let body = item.message || ""; |
| 81 | + |
| 82 | + if (Array.isArray(item.stack)) { |
| 83 | + // `for...of` needs an array iterator, which an ES5 target does not have. |
| 84 | + item.stack.forEach((frame) => { |
| 85 | + if (typeof frame === "string") { |
| 86 | + body += `\r\n${frame}`; |
| 87 | + } |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + return body; |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * A problem as one string, which is what the overlay renders: the location on |
| 96 | + * the first line, read as the heading, and the message under it. |
| 97 | + * @param {string | Problem} item a problem, or a message on its own |
| 98 | + * @returns {string} the problem |
| 99 | + */ |
| 100 | +export function problemLine(item) { |
| 101 | + const location = problemLocation(item); |
| 102 | + const body = problemBody(item); |
| 103 | + |
| 104 | + return location ? `${location}\n${body}` : body; |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * A problem split for a console, where the level belongs in the heading rather |
| 109 | + * than being drawn around it. |
| 110 | + * @param {string} type `"warning"`, or anything else for an error |
| 111 | + * @param {string | Problem} item a problem, or a message on its own |
| 112 | + * @returns {{ header: string, body: string }} the problem, in two parts |
| 113 | + */ |
| 114 | +export function formatProblem(type, item) { |
| 115 | + const location = problemLocation(item); |
| 116 | + |
| 117 | + return { |
| 118 | + header: `${type === "warning" ? "WARNING" : "ERROR"}${ |
| 119 | + location ? ` in ${location}` : "" |
| 120 | + }`, |
| 121 | + body: problemBody(item), |
| 122 | + }; |
| 123 | +} |
0 commit comments