Skip to content

Commit dbf1312

Browse files
ajoslinyoshuawuyts
authored andcommitted
Use writeFileSync to write to out file
Using a write stream, the target file would be erased each time css-extract started. This meant that, when using livereload with CSS, there were two visible reloads: once at the start of css-extract, and once when it finished. Using writeFileSync, the file is only edited once in place. Signed-off-by: Yoshua Wuyts <[email protected]> Closes #1
1 parent 4b8b898 commit dbf1312

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const ConcatStream = require('concat-stream')
12
const isRequire = require('is-require')()
23
const through = require('through2')
34
const falafel = require('falafel')
@@ -25,7 +26,7 @@ function cssExtract (bundle, opts) {
2526
bundle.pipeline.get('debug').unshift(through.obj(write, flush))
2627
const writeStream = (typeof outFile === 'function')
2728
? outFile()
28-
: fs.createWriteStream(outFile)
29+
: ConcatStream(writeOutFile)
2930

3031
function write (chunk, enc, cb) {
3132
const css = extract(chunk)
@@ -39,6 +40,10 @@ function cssExtract (bundle, opts) {
3940
cb()
4041
}
4142
}
43+
44+
function writeOutFile (buffer) {
45+
fs.writeFileSync(outFile, buffer)
46+
}
4247
}
4348

4449
// extract css from chunks

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
],
2121
"license": "MIT",
2222
"dependencies": {
23+
"concat-stream": "^1.5.1",
2324
"falafel": "^1.2.0",
2425
"is-require": "0.0.1",
2526
"through2": "^2.0.1"
@@ -32,7 +33,8 @@
3233
"istanbul": "^0.4.2",
3334
"sheetify": "^4.1.0",
3435
"standard": "^6.0.7",
35-
"tape": "^4.5.0"
36+
"tape": "^4.5.0",
37+
"tmp": "0.0.28"
3638
},
3739
"files": [
3840
"index.js",

test/index.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const browserify = require('browserify')
2+
const tmpDir = require('tmp').dir
23
const path = require('path')
34
const test = require('tape')
45
const bl = require('bl')
@@ -29,4 +30,28 @@ test('css-extract', function (t) {
2930
})
3031
}
3132
})
33+
34+
t.test('should write file', function (t) {
35+
t.plan(3)
36+
tmpDir({unsafeCleanup: true}, onDir)
37+
38+
function onDir (err, dir, cleanup) {
39+
t.ifError(err, 'no error')
40+
const outFile = path.join(dir, 'out.css')
41+
42+
browserify(path.join(__dirname, 'source.js'))
43+
.transform('sheetify/transform')
44+
.plugin(cssExtract, { out: outFile })
45+
.bundle(function (err) {
46+
t.ifError(err, 'no bundle error')
47+
48+
const exPath = path.join(__dirname, './expected.css')
49+
const expected = fs.readFileSync(exPath, 'utf8').trim()
50+
const actual = fs.readFileSync(outFile, 'utf8').trim()
51+
t.equal(expected, actual, 'all css written to file')
52+
53+
cleanup()
54+
})
55+
}
56+
})
3257
})

0 commit comments

Comments
 (0)