Skip to content

Commit 3e52969

Browse files
fix: warning and error serialization (#1523)
1 parent ed77720 commit 3e52969

10 files changed

+934
-489
lines changed

package-lock.json

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"icss-utils": "^5.1.0",
5050
"postcss": "^8.4.21",
5151
"postcss-modules-extract-imports": "^3.0.0",
52-
"postcss-modules-local-by-default": "^4.0.1",
52+
"postcss-modules-local-by-default": "^4.0.3",
5353
"postcss-modules-scope": "^3.0.0",
5454
"postcss-modules-values": "^4.0.0",
5555
"postcss-value-parser": "^4.2.0",

src/CssSyntaxError.js

-29
This file was deleted.

src/Warning.js

-22
This file was deleted.

src/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import postcss from "postcss";
77
import postcssPkg from "postcss/package.json";
88
import { satisfies } from "semver";
99

10-
import CssSyntaxError from "./CssSyntaxError";
11-
import Warning from "./Warning";
1210
import schema from "./options.json";
1311
import { icssParser, importParser, urlParser } from "./plugins";
1412
import {
@@ -27,6 +25,8 @@ import {
2725
sort,
2826
combineRequests,
2927
stringifyRequest,
28+
warningFactory,
29+
syntaxErrorFactory,
3030
} from "./utils";
3131

3232
export default async function loader(content, map, meta) {
@@ -189,14 +189,14 @@ export default async function loader(content, map, meta) {
189189
}
190190

191191
callback(
192-
error.name === "CssSyntaxError" ? new CssSyntaxError(error) : error
192+
error.name === "CssSyntaxError" ? syntaxErrorFactory(error) : error
193193
);
194194

195195
return;
196196
}
197197

198198
for (const warning of result.warnings()) {
199-
this.emitWarning(new Warning(warning));
199+
this.emitWarning(warningFactory(warning));
200200
}
201201

202202
const imports = []

src/utils.js

+53
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,57 @@ function combineRequests(preRequest, url) {
12811281
: preRequest + url;
12821282
}
12831283

1284+
function warningFactory(obj) {
1285+
let message = "";
1286+
1287+
if (typeof obj.line !== "undefined") {
1288+
message += `(${obj.line}:${obj.column}) `;
1289+
}
1290+
1291+
if (typeof obj.plugin !== "undefined") {
1292+
message += `from "${obj.plugin}" plugin: `;
1293+
}
1294+
1295+
message += obj.text;
1296+
1297+
if (obj.node) {
1298+
message += `\n\nCode:\n ${obj.node.toString()}\n`;
1299+
}
1300+
1301+
const warning = new Error(message);
1302+
1303+
warning.stack = null;
1304+
1305+
return warning;
1306+
}
1307+
1308+
function syntaxErrorFactory(obj) {
1309+
let message = "\nSyntaxError\n\n";
1310+
1311+
if (typeof obj.line !== "undefined") {
1312+
message += `(${obj.line}:${obj.column}) `;
1313+
}
1314+
1315+
if (typeof obj.plugin !== "undefined") {
1316+
message += `from "${obj.plugin}" plugin: `;
1317+
}
1318+
1319+
message += obj.file ? `${obj.file} ` : "<css input> ";
1320+
message += `${obj.reason}`;
1321+
1322+
const code = obj.showSourceCode();
1323+
1324+
if (code) {
1325+
message += `\n\n${code}\n`;
1326+
}
1327+
1328+
const error = new Error(message);
1329+
1330+
error.stack = null;
1331+
1332+
return error;
1333+
}
1334+
12841335
export {
12851336
normalizeOptions,
12861337
shouldUseModulesPlugins,
@@ -1306,4 +1357,6 @@ export {
13061357
stringifyRequest,
13071358
isDataUrl,
13081359
defaultGetLocalIdent,
1360+
warningFactory,
1361+
syntaxErrorFactory,
13091362
};

test/__snapshots__/exportType.test.js.snap

+65-32
Original file line numberDiff line numberDiff line change
@@ -2202,68 +2202,101 @@ a {
22022202
exports[`'exportType' option should work with 'css-style-sheet' value and urls: warnings 1`] = `
22032203
Array [
22042204
"ModuleWarning: Module Warning (from \`replaced original path\`):
2205-
Warning
2205+
(120:3) from \\"postcss-url-parser\\" plugin: Unable to find uri in 'background: green url() xyz'
22062206

2207-
(120:3) Unable to find uri in 'background: green url() xyz'",
2207+
Code:
2208+
background: green url() xyz
2209+
",
22082210
"ModuleWarning: Module Warning (from \`replaced original path\`):
2209-
Warning
2211+
(124:3) from \\"postcss-url-parser\\" plugin: Unable to find uri in 'background: green url('') xyz'
22102212

2211-
(124:3) Unable to find uri in 'background: green url('') xyz'",
2213+
Code:
2214+
background: green url('') xyz
2215+
",
22122216
"ModuleWarning: Module Warning (from \`replaced original path\`):
2213-
Warning
2217+
(128:3) from \\"postcss-url-parser\\" plugin: Unable to find uri in 'background: green url(\\"\\") xyz'
22142218

2215-
(128:3) Unable to find uri in 'background: green url(\\"\\") xyz'",
2219+
Code:
2220+
background: green url(\\"\\") xyz
2221+
",
22162222
"ModuleWarning: Module Warning (from \`replaced original path\`):
2217-
Warning
2223+
(132:3) from \\"postcss-url-parser\\" plugin: Unable to find uri in 'background: green url(' ') xyz'
22182224

2219-
(132:3) Unable to find uri in 'background: green url(' ') xyz'",
2225+
Code:
2226+
background: green url(' ') xyz
2227+
",
22202228
"ModuleWarning: Module Warning (from \`replaced original path\`):
2221-
Warning
2229+
(136:3) from \\"postcss-url-parser\\" plugin: Unable to find uri in 'background: green url(
2230+
) xyz'
22222231

2223-
(136:3) Unable to find uri in 'background: green url(
2224-
) xyz'",
2232+
Code:
2233+
background: green url(
2234+
) xyz
2235+
",
22252236
"ModuleWarning: Module Warning (from \`replaced original path\`):
2226-
Warning
2237+
(216:3) from \\"postcss-url-parser\\" plugin: Unable to find uri in 'background-image: -webkit-image-set('')'
22272238

2228-
(216:3) Unable to find uri in 'background-image: -webkit-image-set('')'",
2239+
Code:
2240+
background-image: -webkit-image-set('')
2241+
",
22292242
"ModuleWarning: Module Warning (from \`replaced original path\`):
2230-
Warning
2243+
(218:3) from \\"postcss-url-parser\\" plugin: Unable to find uri in 'background-image: image-set('')'
22312244

2232-
(218:3) Unable to find uri in 'background-image: image-set('')'",
2245+
Code:
2246+
background-image: image-set('')
2247+
",
22332248
"ModuleWarning: Module Warning (from \`replaced original path\`):
2234-
Warning
2249+
(219:3) from \\"postcss-url-parser\\" plugin: Unable to find uri in 'background-image: image-set(\\"\\")'
22352250

2236-
(219:3) Unable to find uri in 'background-image: image-set(\\"\\")'",
2251+
Code:
2252+
background-image: image-set(\\"\\")
2253+
",
22372254
"ModuleWarning: Module Warning (from \`replaced original path\`):
2238-
Warning
2255+
(220:3) from \\"postcss-url-parser\\" plugin: Unable to find uri in 'background-image: image-set(\\"\\" 1x)'
22392256

2240-
(220:3) Unable to find uri in 'background-image: image-set(\\"\\" 1x)'",
2257+
Code:
2258+
background-image: image-set(\\"\\" 1x)
2259+
",
22412260
"ModuleWarning: Module Warning (from \`replaced original path\`):
2242-
Warning
2261+
(221:3) from \\"postcss-url-parser\\" plugin: Unable to find uri in 'background-image: image-set(url())'
22432262

2244-
(221:3) Unable to find uri in 'background-image: image-set(url())'",
2263+
Code:
2264+
background-image: image-set(url())
2265+
",
22452266
"ModuleWarning: Module Warning (from \`replaced original path\`):
2246-
Warning
2267+
(222:3) from \\"postcss-url-parser\\" plugin: Unable to find uri in 'background-image: image-set(
2268+
url()
2269+
)'
22472270

2248-
(222:3) Unable to find uri in 'background-image: image-set(
2271+
Code:
2272+
background-image: image-set(
22492273
url()
2250-
)'",
2274+
)
2275+
",
22512276
"ModuleWarning: Module Warning (from \`replaced original path\`):
2252-
Warning
2277+
(225:3) from \\"postcss-url-parser\\" plugin: Unable to find uri in 'background-image: image-set(URL())'
22532278

2254-
(225:3) Unable to find uri in 'background-image: image-set(URL())'",
2279+
Code:
2280+
background-image: image-set(URL())
2281+
",
22552282
"ModuleWarning: Module Warning (from \`replaced original path\`):
2256-
Warning
2283+
(226:3) from \\"postcss-url-parser\\" plugin: Unable to find uri in 'background-image: image-set(url(''))'
22572284

2258-
(226:3) Unable to find uri in 'background-image: image-set(url(''))'",
2285+
Code:
2286+
background-image: image-set(url(''))
2287+
",
22592288
"ModuleWarning: Module Warning (from \`replaced original path\`):
2260-
Warning
2289+
(227:3) from \\"postcss-url-parser\\" plugin: Unable to find uri in 'background-image: image-set(url(\\"\\"))'
22612290

2262-
(227:3) Unable to find uri in 'background-image: image-set(url(\\"\\"))'",
2291+
Code:
2292+
background-image: image-set(url(\\"\\"))
2293+
",
22632294
"ModuleWarning: Module Warning (from \`replaced original path\`):
2264-
Warning
2295+
(228:3) from \\"postcss-url-parser\\" plugin: Unable to find uri in 'background-image: image-set(url('') 1x)'
22652296

2266-
(228:3) Unable to find uri in 'background-image: image-set(url('') 1x)'",
2297+
Code:
2298+
background-image: image-set(url('') 1x)
2299+
",
22672300
]
22682301
`;
22692302

0 commit comments

Comments
 (0)