Skip to content

Commit cead2c4

Browse files
authored
fix: silent error (giuseppeg#38)
This can happen with invalid postcss config Fixes giuseppeg#37 * fix error handling for css syntax error * add message about Next.js 9 non standard postcss config
1 parent 4240e4a commit cead2c4

File tree

4 files changed

+55
-19
lines changed

4 files changed

+55
-19
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
plugins: ['mock-plugin']
3+
}

index.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
const { spawnSync } = require('child_process');
32
const path = require('path');
43

@@ -7,21 +6,24 @@ module.exports = (css, settings) => {
76
.replace(/%%styled-jsx-placeholder-(\d+)%%/g, (_, id) =>
87
`/*%%styled-jsx-placeholder-${id}%%*/`
98
)
10-
const result = spawnSync("node",[path.resolve(__dirname, "processor.js")],{
9+
10+
const result = spawnSync('node', [path.resolve(__dirname, 'processor.js')], {
1111
input: JSON.stringify({
1212
css: cssWithPlaceholders,
1313
settings
1414
}),
15-
encoding: "utf8"
16-
});
15+
encoding: 'utf8'
16+
})
1717

18-
processedCss = result.stdout
18+
if (result.stderr) {
19+
if (result.stderr.includes('Invalid PostCSS Plugin')) {
20+
console.error('Next.js 9 default postcss support uses a non standard postcss config schema https://err.sh/next.js/postcss-shape, you must use the interoperable object-based format instead https://nextjs.org/docs/advanced-features/customizing-postcss-config')
21+
}
1922

20-
if (processedCss instanceof Error || processedCss.name === 'CssSyntaxError') {
21-
throw processedCss
23+
throw new Error(`postcss failed with ${result.stderr}`)
2224
}
2325

24-
return processedCss
26+
return result.stdout
2527
.replace(/\/\*%%styled-jsx-placeholder-(\d+)%%\*\//g, (_, id) =>
2628
`%%styled-jsx-placeholder-${id}%%`
2729
)

processor.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,21 @@ function processor(src, options) {
2626
.then(result => result.css)
2727
}
2828

29-
let input = "";
30-
process.stdin.on("data", (data) => {
31-
input += data.toString();
32-
});
29+
let input = ''
30+
process.stdin.on('data', data => {
31+
input += data.toString()
32+
})
3333

34-
process.stdin.on("end", () => {
34+
process.stdin.on('end', () => {
3535
const inputData = JSON.parse(input)
36-
processor(inputData.css, inputData.settings).then(function(result){
37-
process.stdout.write(result)
38-
})
36+
processor(inputData.css, inputData.settings)
37+
.then(result => {
38+
process.stdout.write(result)
39+
})
40+
.catch(err => {
41+
// NOTE: we console.erorr(err) and then process.exit(1) instead of throwing the error
42+
// to avoid the UnhandledPromiseRejectionWarning message.
43+
console.error(err)
44+
process.exit(1)
45+
})
3946
})
40-
41-
// module.exports = processor

test.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const assert = require('assert')
2+
const path = require('path')
23
const plugin = require('./')
34

45
describe('styled-jsx-plugin-postcss', () => {
@@ -27,7 +28,7 @@ describe('styled-jsx-plugin-postcss', () => {
2728
)
2829
})
2930

30-
it("works with quotes and other characters", () => {
31+
it('works with quotes and other characters', () => {
3132
assert.equal(
3233
plugin(`@import "./fixture.css"; * { color: red; font-family: 'Times New Roman'; }
3334
li:after{ content: "!@#$%^&*()_+"}
@@ -36,4 +37,29 @@ describe('styled-jsx-plugin-postcss', () => {
3637
)
3738
})
3839

40+
it('throws with invalid css', () => {
41+
assert.throws(
42+
() => {
43+
plugin('a {\n content: "\n}')
44+
},
45+
{
46+
name: 'Error',
47+
message: /postcss failed with CssSyntaxError: <css input>:2:12: Unclosed string/
48+
}
49+
)
50+
})
51+
52+
it('throws with invalid config', () => {
53+
assert.throws(
54+
() => {
55+
plugin('p { color: color-mod(red alpha(90%)); & img { display: block } }', {
56+
path: path.resolve('fixture-invalid-config')
57+
})
58+
},
59+
{
60+
name: 'Error',
61+
message: /postcss failed with TypeError: Invalid PostCSS Plugin found: \[0\]/
62+
}
63+
)
64+
})
3965
})

0 commit comments

Comments
 (0)