Skip to content

Commit a9ec9eb

Browse files
committed
Refactor writing to stdout
- Can't use external sourcemaps when writing to stdout - Write to process.stdout directly, without an intermediate stream
1 parent 0fae796 commit a9ec9eb

2 files changed

Lines changed: 15 additions & 11 deletions

File tree

index.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ const fs = require('fs-promise')
44
const path = require('path')
55

66
const stdin = require('get-stdin')
7-
const Readable = require('stream').Readable
8-
97
const chalk = require('chalk')
108
const ora = require('ora')
119
const globber = require('globby')
@@ -217,6 +215,12 @@ function css (css, file) {
217215
options.to = path.resolve(options.to)
218216
}
219217

218+
// Can't use external sourcemaps when writing to stdout:
219+
if (!options.to && config.options.map && !config.options.map.inline) {
220+
spinner.fail()
221+
error('Cannot output external sourcemaps when writing to stdout')
222+
}
223+
220224
return postcss(config.plugins)
221225
.process(css, options)
222226
.then((result) => {
@@ -242,15 +246,7 @@ function css (css, file) {
242246
)
243247
)
244248
}
245-
} else {
246-
const $ = new Readable({ read: (chunk) => chunk })
247-
248-
result.map
249-
? $.push(result.css, result.map)
250-
: $.push(result.css)
251-
252-
$.pipe(process.stdout)
253-
}
249+
} else process.stdout.write(result.css, 'utf8')
254250

255251
return Promise.all(tasks)
256252
.then(() => {

test/error.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ test('multiple input files && writing to stdout', (t) => {
2424
})
2525
})
2626

27+
test('--map && writing to stdout', (t) => {
28+
return cli(['test/fixtures/a.css', '--map'])
29+
.then(({ err, code }) => {
30+
t.is(code, 1, 'expected non-zero error code')
31+
t.regex(err, /Cannot output external sourcemaps when writing to stdout/)
32+
})
33+
})
34+
2735
test('invalid --config', (t) => {
2836
return cli(
2937
[

0 commit comments

Comments
 (0)