Skip to content

Commit

Permalink
Respect postcssrc (#7)
Browse files Browse the repository at this point in the history
* Add support for postcssrc config file

* Ensure default options are persisted

* Fix support for static postcssrc config

* Documented postcssrc usage in README
  • Loading branch information
tornqvist authored and goto-bus-stop committed Apr 18, 2018
1 parent 0c336ee commit 9199c17
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 15 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ add to your `package.json` `browserify.transform` field:
}
```

### using `.postcssrc`

Options and plugins can be defined using a config file. Uses [postcss-load-config](https://github.com/michael-ciniawsky/postcss-load-config) which supports `postcss` field in `package.json`, an external JSON or YAML (`.postcssrc`) file as well as JS (`.postcssrc.js` and `postcss.config.js`) file format.

```javascript
// .postcssrc.js
module.exports = function (ctx) {
var plugins = [require('postcss-color-function')]

if (ctx.env !== 'development') {
plugins.push(require('autoprefixer'))
}

return {
map: ctx.env === 'development' ? 'inline' : false
plugins: plugins
}
}
```

## license

The Apache License
Expand Down
37 changes: 22 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const defined = require('defined')
const postcss = require('postcss')
const extend = require('xtend')
const resolve = require('resolve')
const postcssrc = require('postcss-load-config')

module.exports = transform

Expand All @@ -23,19 +24,25 @@ function transform (filename, source, options, done) {
})
.map(plugin => require(plugin.path)(plugin.options))

postcss(plugins)
.process(source, extend({
sourcemap: true,
from: filename,
messages: {
browser: true,
console: false
}
}, options))
.then(function (result) {
done(null, result.css)
})
.catch(function (err) {
done(err)
})
const ctx = extend({
sourcemap: true,
from: filename,
messages: {
browser: true,
console: false
}
}, options)

delete ctx.plugins

postcssrc(ctx, basedir).then(compile, function () {
return compile({options: ctx})
}).then(function (result) {
done(null, result.css)
}, done)

function compile (config) {
return postcss(plugins.concat(config.plugins).filter(Boolean))
.process(source, config.options)
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"dependencies": {
"defined": "^1.0.0",
"postcss": "^5.1.2",
"postcss-load-config": "^1.2.0",
"resolve": "^1.1.7",
"xtend": "^4.0.1"
}
Expand Down
10 changes: 10 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const path = require('path')
const test = require('tape')
const sheetifyPostcss = require('../')

Expand All @@ -19,5 +20,14 @@ test(function (t) {
t.equal(css, '.ok-with-options {}')
})
})

t.test('module should respect postcssrc config file', function (t) {
t.plan(2)

sheetifyPostcss('test.css', '.rule {}', { basedir: path.join(__dirname, 'stubs') }, (err, css) => {
t.equal(err, null)
t.equal(css, '.ok-with-postcssrc {}')
})
})
})

13 changes: 13 additions & 0 deletions test/stubs/.postcssrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var postcss = require('postcss')

var plugin = postcss.plugin('postcss-plugin', function postcssPlugin () {
return function (root, result) {
root.walkRules('.rule', rule => {
rule.replaceWith(postcss.rule({ selector: '.ok-with-postcssrc' }))
})
}
})

module.exports = {
plugins: [plugin]
}

0 comments on commit 9199c17

Please sign in to comment.