Skip to content

Commit 06333d7

Browse files
committed
fix(dev-server): fix handling css parse errors
1 parent c32ad5b commit 06333d7

File tree

3 files changed

+73
-15
lines changed

3 files changed

+73
-15
lines changed

src/dev-server/dev-client/app-error.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ function appendDiagnostic(win: Window, doc: Document, config: d.DevClientConfig,
105105

106106
const tdNum = doc.createElement('td');
107107
tdNum.className = 'dev-server-diagnostic-blob-num';
108-
tdNum.setAttribute('data-line-number', l.lineNumber + '');
108+
if (l.lineNumber > -1) {
109+
tdNum.setAttribute('data-line-number', l.lineNumber + '');
110+
}
109111
tr.appendChild(tdNum);
110112

111113
const tdCode = doc.createElement('td');

src/sys/node/node-logger.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,12 @@ export class NodeLogger implements d.Logger {
331331
return;
332332
}
333333

334-
let msg = `L${l.lineNumber}: `;
334+
let msg = ``;
335+
336+
if (l.lineNumber > -1) {
337+
msg = `L${l.lineNumber}: `;
338+
}
339+
335340
while (msg.length < INDENT.length) {
336341
msg = ' ' + msg;
337342
}

src/sys/node/optimize-css-worker.ts

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,72 @@ export async function optimizeCssWorker(inputOpts: d.OptimizeCssInput) {
2424

2525
const processor = postcss(plugins);
2626

27-
const result = await processor.process(inputOpts.css, {
28-
map: null,
29-
from: inputOpts.filePath
30-
});
31-
32-
result.warnings().forEach(warning => {
33-
output.diagnostics.push({
34-
header: `Optimize CSS: ${warning.plugin}`,
35-
messageText: warning.text,
36-
level: 'warn',
37-
type: 'css'
27+
try {
28+
const result = await processor.process(inputOpts.css, {
29+
map: null,
30+
from: inputOpts.filePath
3831
});
39-
});
4032

41-
output.css = result.css;
33+
result.warnings().forEach(warning => {
34+
output.diagnostics.push({
35+
header: `Optimize CSS: ${warning.plugin}`,
36+
messageText: warning.text,
37+
level: 'warn',
38+
type: 'css',
39+
absFilePath: inputOpts.filePath
40+
});
41+
});
42+
43+
output.css = result.css;
44+
45+
} catch (e) {
46+
const diagnostic: d.Diagnostic = {
47+
header: `Optimize CSS`,
48+
messageText: `CSS Error`,
49+
level: `error`,
50+
type: `css`,
51+
absFilePath: inputOpts.filePath
52+
};
53+
54+
if (typeof e.name === 'string') {
55+
diagnostic.header = e.name;
56+
}
57+
58+
if (typeof e.reason === 'string') {
59+
diagnostic.messageText = e.reason;
60+
}
61+
62+
if (typeof e.source === 'string' && typeof e.line === 'number') {
63+
const lines = (e.source as string).replace(/\r/g, '\n').split('\n');
64+
65+
if (lines.length > 0) {
66+
const addLine = (lineNumber: number) => {
67+
const line = lines[lineNumber];
68+
if (typeof line === 'string') {
69+
const printLine: d.PrintLine = {
70+
lineIndex: -1,
71+
lineNumber: -1,
72+
text: line,
73+
errorCharStart: -1,
74+
errorLength: -1
75+
};
76+
diagnostic.lines = diagnostic.lines || [];
77+
diagnostic.lines.push(printLine);
78+
}
79+
};
80+
81+
addLine(e.line - 3);
82+
addLine(e.line - 2);
83+
addLine(e.line - 1);
84+
addLine(e.line);
85+
addLine(e.line + 1);
86+
addLine(e.line + 2);
87+
addLine(e.line + 3);
88+
}
89+
}
90+
91+
output.diagnostics.push(diagnostic);
92+
}
4293

4394
} catch (e) {
4495
catchError(output.diagnostics, e);

0 commit comments

Comments
 (0)