Skip to content

Commit

Permalink
Merge pull request #22 from w0rm/20-support-async-processors
Browse files Browse the repository at this point in the history
20 support async processors
  • Loading branch information
w0rm committed Apr 1, 2015
2 parents bd9b176 + f46532a commit 1fb26f6
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 35 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ return gulp.src('./src/*.css')
## Changelog
* 5.0.0
* Use async API
* 4.0.3
* Fixed bug with relative source map
Expand Down
50 changes: 29 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var through = require('through2')
var Stream = require('stream')
var postcss = require('postcss')
var applySourceMap = require('vinyl-sourcemaps-apply')
var gutil = require('gulp-util')
Expand All @@ -10,20 +10,17 @@ module.exports = function (processors, options) {
throw new gutil.PluginError('gulp-postcss', 'Please provide array of postcss processors!')
}

return through.obj(transform)
var stream = new Stream.Transform({ objectMode: true })

function transform (file, encoding, cb) {
stream._transform = function (file, encoding, cb) {

if (file.isStream()) {
return cb(new gutil.PluginError('gulp-postcss', 'Streams are not supported!'))
return handleError('Streams are not supported!')
}

// Source map is disabled by default
var opts = { map: false }
var processor = postcss()
var result
var attr
var map

// Extend default options
if (options) {
Expand All @@ -42,25 +39,36 @@ module.exports = function (processors, options) {
}

try {
processors.forEach(processor.use.bind(processor))
result = processor.process(file.contents.toString('utf8'), opts)
} catch (err) {
return cb(new gutil.PluginError('gulp-postcss', err))
postcss(processors)
.process(file.contents, opts)
.then(handleResult, handleError)
} catch (error) {
handleError(error)
}

file.contents = new Buffer(result.css)
function handleResult (result) {
var map

// Apply source map to the chain
if (file.sourceMap) {
map = result.map.toJSON()
map.file = file.relative
map.sources = [].map.call(map.sources, function (source) {
return path.relative(file.base, source)
})
applySourceMap(file, map)
file.contents = new Buffer(result.css)

// Apply source map to the chain
if (file.sourceMap) {
map = result.map.toJSON()
map.file = file.relative
map.sources = [].map.call(map.sources, function (source) {
return path.relative(file.base, source)
})
applySourceMap(file, map)
}

cb(null, file)
}

function handleError (error) {
cb(new gutil.PluginError('gulp-postcss', error))
}

cb(null, file)
}

return stream
}
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "gulp-postcss",
"version": "4.0.3",
"version": "5.0.0",
"description": "PostCSS gulp plugin",
"main": "index.js",
"scripts": {
"test": "mocha"
"test": "mocha test.js"
},
"repository": {
"type": "git",
Expand All @@ -22,13 +22,13 @@
},
"homepage": "https://github.com/w0rm/gulp-postcss",
"dependencies": {
"gulp-util": "^3.0.0",
"postcss": "^4.0.1",
"through2": "^0.6.1",
"vinyl-sourcemaps-apply": "^0.1.1"
"gulp-util": "^3.0.4",
"postcss": "^4.1.0",
"vinyl-sourcemaps-apply": "^0.1.4"
},
"devDependencies": {
"gulp-sourcemaps": "^1.1.5",
"mocha": "^1.21.4"
"gulp-sourcemaps": "^1.5.1",
"mocha": "^2.2.1",
"promise": "^6.1.0"
}
}
22 changes: 16 additions & 6 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ var assert = require('assert')
var gutil = require('gulp-util')
var sourceMaps = require('gulp-sourcemaps')
var postcss = require('./index')
var Promise = require('promise')

it('should transform css with multiple processors', function (cb) {

var stream = postcss(
[ doubler, doubler ]
[ asyncDoubler, doubler ]
)

stream.on('data', function (file) {
Expand Down Expand Up @@ -46,28 +47,28 @@ it('should correctly wrap postcss errors', function (cb) {
})


it ('should throw error if processors are not provided', function (cb) {
it('should throw error if processors are not provided', function (cb) {
assert.throws( function () { postcss() }, gutil.PluginError )
assert.throws( function () { postcss('') }, gutil.PluginError )
assert.throws( function () { postcss({}) }, gutil.PluginError )
cb()
})


it ('should generate source maps', function (cb) {
it('should generate source maps', function (cb) {

var init = sourceMaps.init()
var write = sourceMaps.write()
var css = postcss(
[ doubler, doubler ]
[ doubler, asyncDoubler ]
)

init
.pipe(css)
.pipe(write)

write.on('data', function (file) {
assert.equal(file.sourceMap.mappings, 'AAAA,IAAI,cAAA,AAAY,cAAZ,AAAY,cAAZ,AAAY,aAAA,EAAE')
assert.equal(file.sourceMap.mappings, 'AAAA,IAAI,aAAY,CAAZ,aAAY,CAAZ,aAAY,CAAZ,YAAY,EAAE')
assert(/sourceMappingURL=data:application\/json;base64/.test(file.contents.toString()))
cb()
})
Expand All @@ -83,7 +84,7 @@ it ('should generate source maps', function (cb) {
})


it ('should correctly generate relative source map', function (cb) {
it('should correctly generate relative source map', function (cb) {

var init = sourceMaps.init()
var css = postcss(
Expand Down Expand Up @@ -114,3 +115,12 @@ function doubler (css) {
decl.parent.prepend(decl.clone())
})
}

function asyncDoubler (css) {
return new Promise(function (resolve) {
setTimeout(function () {
doubler(css)
resolve()
})
})
}

0 comments on commit 1fb26f6

Please sign in to comment.