Skip to content

Commit

Permalink
Merge pull request #29 from w0rm/21-support-runner-guidelines
Browse files Browse the repository at this point in the history
21 support runner guidelines
  • Loading branch information
w0rm committed Apr 12, 2015
2 parents 88191a8 + a3f2735 commit 5e4f20e
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 6 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ return gulp.src('./src/*.css')

## Changelog

* 5.1.0 PostCSS Runner Guidelines
* Set `from` and `to` processing options
* Don't output js stack trace for `CssSyntaxError`
* Display `result.warnings()` content

* 5.0.1
* Fix to support object processors

Expand Down
16 changes: 14 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ var postcss = require('postcss')
var applySourceMap = require('vinyl-sourcemaps-apply')
var gutil = require('gulp-util')
var path = require('path')
var CssSyntaxError = require('postcss/lib/css-syntax-error')


module.exports = function (processors, options) {

Expand Down Expand Up @@ -32,7 +34,7 @@ module.exports = function (processors, options) {
}
}

opts.from = file.path
opts.from = opts.to = file.path

// Generate separate source map for gulp-sourcemap
if (file.sourceMap) {
Expand All @@ -50,6 +52,7 @@ module.exports = function (processors, options) {

function handleResult (result) {
var map
var warnings = result.warnings().join('\n')

file.contents = new Buffer(result.css)

Expand All @@ -58,15 +61,24 @@ module.exports = function (processors, options) {
map = result.map.toJSON()
map.file = file.relative
map.sources = [].map.call(map.sources, function (source) {
return path.relative(file.base, source)
return path.join(path.dirname(file.relative), source)
})
applySourceMap(file, map)
}

if (warnings) {
gutil.log('gulp-postcss:', file.relative + '\n' + warnings)
}

cb(null, file)
}

function handleError (error) {
var errorOptions = { fileName: file.path }
if (error instanceof CssSyntaxError) {
error = error.message + error.showSourceCode()
errorOptions.showStack = false
}
cb(new gutil.PluginError('gulp-postcss', error))
}

Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gulp-postcss",
"version": "5.0.1",
"version": "5.1.0",
"description": "PostCSS gulp plugin",
"main": "index.js",
"scripts": {
Expand All @@ -13,6 +13,7 @@
"keywords": [
"gulpplugin",
"postcss",
"postcssrunner",
"css"
],
"author": "Andrey Kuzmin <[email protected]>",
Expand All @@ -29,6 +30,8 @@
"devDependencies": {
"es6-promise": "^2.0.1",
"gulp-sourcemaps": "^1.5.1",
"mocha": "^2.2.1"
"mocha": "^2.2.1",
"proxyquire": "^1.4.0",
"sinon": "^1.14.1"
}
}
115 changes: 113 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/* global it, Promise */
/* global it, afterEach, describe, Promise */

require('es6-promise').polyfill()
var assert = require('assert')
var gutil = require('gulp-util')
var sourceMaps = require('gulp-sourcemaps')
var postcss = require('./index')

var proxyquire = require('proxyquire')
var sinon = require('sinon')

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

Expand Down Expand Up @@ -111,6 +112,116 @@ it('should correctly generate relative source map', function (cb) {
})


describe('PostCSS Guidelines', function () {

var sandbox = sinon.sandbox.create()
var CssSyntaxError = function (message, sourceCode) {
this.message = message
this.sourceCode = sourceCode
this.showSourceCode = function () {
return this.sourceCode
}
}
var postcssStub = {
use: sandbox.stub()
, process: sandbox.stub()
}
var postcss = proxyquire('./index', {
postcss: function () {
return postcssStub
}
, 'postcss/lib/css-syntax-error': CssSyntaxError
})


afterEach(function () {
sandbox.restore()
})


it('should set `from` and `to` processing options to `file.path`', function (cb) {

var stream = postcss([ doubler ])
var cssPath = __dirname + '/src/fixture.css'
postcssStub.process.returns(Promise.resolve({
css: ''
, warnings: function () {
return []
}
}))

stream.on('data', function () {
postcssStub.process.calledWith('a {}', {from: cssPath, to: cssPath})
cb()
})

stream.write(new gutil.File({
contents: new Buffer('a {}')
, path: cssPath
}))

stream.end()

})


it('should not output js stack trace for `CssSyntaxError`', function (cb) {

var stream = postcss([ doubler ])
var cssSyntaxError = new CssSyntaxError('message', 'sourceCode')
postcssStub.process.returns(Promise.reject(cssSyntaxError))

stream.on('error', function (error) {
assert.equal(error.showStack, false)
assert.equal(error.message, 'message' + 'sourceCode')
cb()
})

stream.write(new gutil.File({
contents: new Buffer('a {}')
}))

stream.end()

})


it('should display `result.warnings()` content', function (cb) {

var stream = postcss([ doubler ])
var cssPath = __dirname + '/src/fixture.css'
function Warning (msg) {
this.toSting = function () {
return msg
}
}

sandbox.stub(gutil, 'log')
postcssStub.process.returns(Promise.resolve({
css: ''
, warnings: function () {
return [new Warning('msg1'), new Warning('msg2')]
}
}))

stream.on('data', function () {
gutil.log.calledWith('gulp-postcss:', '/src/fixture.css\nmsg1\nmsg2')
cb()
})

stream.write(new gutil.File({
contents: new Buffer('a {}')
, path: cssPath
}))

stream.end()

})


})


function doubler (css) {
css.eachDecl(function (decl) {
decl.parent.prepend(decl.clone())
Expand Down

0 comments on commit 5e4f20e

Please sign in to comment.