Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const ConcatStream = require('concat-stream')
const isRequire = require('is-require')()
const through = require('through2')
const falafel = require('falafel')
Expand Down Expand Up @@ -25,7 +26,7 @@ function cssExtract (bundle, opts) {
bundle.pipeline.get('debug').unshift(through.obj(write, flush))
const writeStream = (typeof outFile === 'function')
? outFile()
: fs.createWriteStream(outFile)
: ConcatStream(writeOutFile)

function write (chunk, enc, cb) {
const css = extract(chunk)
Expand All @@ -39,6 +40,10 @@ function cssExtract (bundle, opts) {
cb()
}
}

function writeOutFile (buffer) {
fs.writeFileSync(outFile, buffer)
}
}

// extract css from chunks
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
],
"license": "MIT",
"dependencies": {
"concat-stream": "^1.5.1",
"falafel": "^1.2.0",
"is-require": "0.0.1",
"through2": "^2.0.1"
Expand All @@ -32,7 +33,8 @@
"istanbul": "^0.4.2",
"sheetify": "^4.1.0",
"standard": "^6.0.7",
"tape": "^4.5.0"
"tape": "^4.5.0",
"tmp": "0.0.28"
},
"files": [
"index.js",
Expand Down
25 changes: 25 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const browserify = require('browserify')
const tmpDir = require('tmp').dir
const path = require('path')
const test = require('tape')
const bl = require('bl')
Expand Down Expand Up @@ -29,4 +30,28 @@ test('css-extract', function (t) {
})
}
})

t.test('should write file', function (t) {
t.plan(3)
tmpDir({unsafeCleanup: true}, onDir)

function onDir (err, dir, cleanup) {
t.ifError(err, 'no error')
const outFile = path.join(dir, 'out.css')

browserify(path.join(__dirname, 'source.js'))
.transform('sheetify/transform')
.plugin(cssExtract, { out: outFile })
.bundle(function (err) {
t.ifError(err, 'no bundle error')

const exPath = path.join(__dirname, './expected.css')
const expected = fs.readFileSync(exPath, 'utf8').trim()
const actual = fs.readFileSync(outFile, 'utf8').trim()
t.equal(expected, actual, 'all css written to file')

cleanup()
})
}
})
})