diff --git a/README.md b/README.md index 120629c..f4da655 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/index.js b/index.js index 39e7819..02fb8b7 100644 --- a/index.js +++ b/index.js @@ -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') @@ -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) { @@ -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 } diff --git a/package.json b/package.json index de9a641..26e1854 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" } } diff --git a/test.js b/test.js index ffa225e..7cbe2a6 100644 --- a/test.js +++ b/test.js @@ -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) { @@ -46,7 +47,7 @@ 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 ) @@ -54,12 +55,12 @@ it ('should throw error if processors are not provided', function (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 @@ -67,7 +68,7 @@ it ('should generate source maps', function (cb) { .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() }) @@ -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( @@ -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() + }) + }) +}