Skip to content

Commit a4fba99

Browse files
committed
BREAKING: Don't exit on CssSyntaxError in watch mode (v2 compat)
v2 didn't exit on CssSyntaxError in watch mode, but v3.0.0-beta did. This commit restores the v2 behavior. Fixes postcss#105.
1 parent ce75575 commit a4fba99

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,10 @@ function css (css, file) {
310310
return result
311311
})
312312
})
313+
}).catch(err => {
314+
// Fail spinner and send error up the promise chain
315+
spinner.fail()
316+
throw err
313317
})
314318
}
315319

@@ -341,6 +345,8 @@ function error (err) {
341345
.replace(/:\s/, '] ')
342346
console.error('\n', chalk.bold.red(`[${err.message}`))
343347
console.error('\n', err.showSourceCode(), '\n')
348+
// If watch mode, don't exit the process:
349+
if (argv.watch) return
344350
} else {
345351
// JS Error
346352
// Don't use chalk here; we want a JS stack trace:

test/watch.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,62 @@ test.cb('--watch dependencies', function (t) {
227227
// Timeout:
228228
setTimeout(() => t.end('test timeout'), 50000)
229229
})
230+
231+
test.cb("--watch doesn't exit on CssSyntaxError", function (t) {
232+
t.plan(0)
233+
234+
ENV('', ['a.css'])
235+
.then((dir) => {
236+
// Init watcher:
237+
const watcher = chokidar.watch('.', {
238+
cwd: dir,
239+
ignoreInitial: true,
240+
awaitWriteFinish: true
241+
})
242+
watcher.on('add', (p) => {
243+
if (p === 'output.css') {
244+
// Change to invalid CSS
245+
fs.writeFile(path.join(dir, 'a.css'), '.a { color: red')
246+
.catch(done)
247+
}
248+
})
249+
250+
let killed = false
251+
let cp = execFile(
252+
path.resolve('bin/postcss'),
253+
[
254+
'a.css',
255+
'-o', 'output.css',
256+
'-w',
257+
'--no-map'
258+
],
259+
{ cwd: dir }
260+
)
261+
cp.on('error', t.end)
262+
cp.stderr.on('data', chunk => {
263+
// When error message is printed, kill the process after a timeout
264+
if (~chunk.indexOf('Unclosed block')) {
265+
setTimeout(() => {
266+
killed = true
267+
cp.kill()
268+
}, 1000)
269+
}
270+
})
271+
cp.on('exit', code => {
272+
if (!killed) return t.end(`Should not exit (exited with code ${code})`)
273+
done()
274+
})
275+
276+
function done (err) {
277+
try {
278+
cp.kill()
279+
} catch (e) {}
280+
281+
t.end(err)
282+
}
283+
})
284+
.catch(t.end)
285+
286+
// Timeout:
287+
setTimeout(() => t.end('test timeout'), 50000)
288+
})

0 commit comments

Comments
 (0)