Skip to content

Commit 52412f6

Browse files
feat: improve error message
1 parent 0f95841 commit 52412f6

File tree

5 files changed

+22
-8
lines changed

5 files changed

+22
-8
lines changed

src/CssSyntaxError.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export default class CssSyntaxError extends Error {
22
constructor(error) {
33
super(error);
44

5-
const { reason, line, column } = error;
5+
const { reason, line, column, file } = error;
66

77
this.name = 'CssSyntaxError';
88

@@ -14,6 +14,7 @@ export default class CssSyntaxError extends Error {
1414
this.message += `(${line}:${column}) `;
1515
}
1616

17+
this.message += file ? `${file} ` : '<css input> ';
1718
this.message += `${reason}`;
1819

1920
const code = error.showSourceCode();

test/__snapshots__/loader.test.js.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ Array [
131131
"ModuleBuildError: Module build failed (from \`replaced original path\`):
132132
CssSyntaxError
133133
134-
(1:8) Unknown word
134+
(1:8) /test/fixtures/invisible-space.css Unknown word
135135
136136
> 1 | a { 

 color: red; 

 }
137137
| ^
@@ -146,7 +146,7 @@ Array [
146146
"ModuleBuildError: Module build failed (from \`replaced original path\`):
147147
CssSyntaxError
148148
149-
(2:3) Unknown word
149+
(2:3) /test/fixtures/error.css Unknown word
150150
151151
1 | .some {
152152
> 2 | invalid css;

test/helpers/getErrors.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import normalizeErrors from './normalizeErrors';
22

3-
export default (stats, shortError) => {
4-
return normalizeErrors(stats.compilation.errors, shortError).sort();
3+
export default (stats, shortError, type) => {
4+
return normalizeErrors(stats.compilation.errors, shortError, type).sort();
55
};

test/helpers/normalizeErrors.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,27 @@ function removeCWD(str) {
2727
.replace(new RegExp(cwd, 'g'), '');
2828
}
2929

30-
export default (errors, shortError) => {
30+
export default (errors, shortError, type) => {
3131
return errors.map((error) => {
3232
let errorMessage = error.toString();
3333

3434
if (shortError) {
3535
errorMessage = errorMessage.split('\n').slice(0, 2).join('\n');
3636
}
3737

38+
if (type === 'postcss') {
39+
errorMessage = errorMessage
40+
.split('\n')
41+
.map((str) => {
42+
if (/^\(/i.test(str)) {
43+
return removeCWD(str);
44+
}
45+
46+
return str;
47+
})
48+
.join('\n');
49+
}
50+
3851
return removeCWD(errorMessage.split('\n').slice(0, 12).join('\n'));
3952
});
4053
};

test/loader.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ describe('loader', () => {
132132
)
133133
).toBe(true);
134134
expect(getWarnings(stats)).toMatchSnapshot('warnings');
135-
expect(getErrors(stats)).toMatchSnapshot('errors');
135+
expect(getErrors(stats, false, 'postcss')).toMatchSnapshot('errors');
136136
});
137137

138138
it('should reuse `ast` from "postcss-loader"', async () => {
@@ -360,7 +360,7 @@ describe('loader', () => {
360360
const stats = await compile(compiler);
361361

362362
expect(getWarnings(stats)).toMatchSnapshot('warnings');
363-
expect(getErrors(stats)).toMatchSnapshot('errors');
363+
expect(getErrors(stats, false, 'postcss')).toMatchSnapshot('errors');
364364
});
365365

366366
it('should work with the "modules.auto" option and the "importLoaders" option', async () => {

0 commit comments

Comments
 (0)