Skip to content

Commit 63820e5

Browse files
committed
Small tweaks for style etc.
* Use bl instead of concat-stream, the former is better optimised for handling buffers. * Switch back to `debug` in the pipeline instead of `label` for now, until we determine the implications of the change. * Instead of calling write/end on the stream directly, use from2-string to keep things neat. * Remove uses of .bind() – almost entirely an aesthetic choice. * static-module's insert-css calls now coerce the first value to a string.
1 parent 8bde190 commit 63820e5

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

index.js

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
const ConcatStream = require('concat-stream')
21
const staticModule = require('static-module')
2+
const from2 = require('from2-string')
33
const through = require('through2')
44
const assert = require('assert')
5+
const bl = require('bl')
56
const fs = require('fs')
67

78
module.exports = cssExtract
@@ -21,40 +22,45 @@ function cssExtract (bundle, opts) {
2122
addHooks()
2223

2324
function addHooks () {
25+
const extractStream = through.obj(write, flush)
2426
const writeStream = (typeof outFile === 'function')
2527
? outFile()
26-
: ConcatStream(fs.writeFileSync.bind(fs, outFile))
28+
: bl(writeComplete)
2729

28-
// run before the "label" step in browserify pipeline
29-
// this makes sure insert-css requires are found before plugins like bundle-collapser run
30-
bundle.pipeline.get('label').unshift(through.obj(write, flush))
30+
// run before the "debug" step in browserify pipeline
31+
bundle.pipeline.get('debug').unshift(extractStream)
3132

3233
function write (chunk, enc, cb) {
33-
// A small performance boost: don't do ast parsing unless we know it's needed
34+
// Performance boost: don't do ast parsing unless we know it's needed
3435
if (String(chunk.source).indexOf('insert-css') === -1) {
3536
return cb(null, chunk)
3637
}
3738

38-
var sm = createStaticModule(writeStream)
39-
sm.write(chunk.source)
40-
sm.pipe(ConcatStream(function (source) {
41-
// chunk.source is expected to be a string
39+
var source = from2(chunk.source)
40+
var sm = staticModule({
41+
'insert-css': function (src) {
42+
writeStream.write(String(src))
43+
}
44+
})
45+
46+
source.pipe(sm).pipe(bl(complete))
47+
48+
function complete (err, source) {
49+
if (err) return extractStream.emit('error', err)
4250
chunk.source = String(source)
4351
cb(null, chunk)
44-
}))
45-
sm.end()
52+
}
4653
}
4754

4855
// close stream and signal end
4956
function flush (cb) {
5057
writeStream.end()
5158
cb()
5259
}
53-
}
54-
}
5560

56-
function createStaticModule (writeStream) {
57-
return staticModule({
58-
'insert-css': writeStream.write.bind(writeStream)
59-
})
61+
function writeComplete (err, buffer) {
62+
if (err) return extractStream.emit('error', err)
63+
fs.writeFileSync(outFile, buffer)
64+
}
65+
}
6066
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
],
2121
"license": "MIT",
2222
"dependencies": {
23-
"concat-stream": "^1.5.1",
23+
"bl": "^1.1.2",
24+
"from2-string": "^1.1.0",
2425
"static-module": "^1.3.0",
2526
"through2": "^2.0.1"
2627
},

0 commit comments

Comments
 (0)