Skip to content

Commit

Permalink
add postcss adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
DiogoDoreto committed Apr 28, 2015
1 parent cf22dbd commit 2362ca4
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 1 deletion.
33 changes: 33 additions & 0 deletions docs/postcss.md
@@ -0,0 +1,33 @@
# PostCSS
This is the compiler interface to [PostCSS](https://github.com/postcss/postcss). PostCSS is a tool for transforming CSS with JS plugins. These plugins can support variables and mixins, transpile future CSS syntax, inline images, and more.

## Supported Methods
- render

## Source Maps

Source maps are supported by the PostCSS adapter. Just pass in a `map` option [as described in the docs](https://github.com/postcss/postcss#source-map), and it will come back as `sourcemap` on the response object.

## Additional Options
### [Use](https://github.com/postcss/postcss/blob/master/API.md#processoruseplugin)

- key: `use`
- type: `Array|Function|Object`
- default: `[]`

Adds one or more plugins to be used as a CSS processor.

```coffee
pcss = accord.load('postcss')
varPlugin = require('postcss-simple-vars')

pcss.render(
'$color: green;\n\n.test { background: $color; }'
use: [varPlugin]
).catch(
(err) -> console.error err
).done(
(res) -> console.log res.result
)
```

21 changes: 21 additions & 0 deletions lib/adapters/postcss.coffee
@@ -0,0 +1,21 @@
Adapter = require '../adapter_base'
W = require 'when'
_ = require 'lodash'
sourcemaps = require '../sourcemaps'

class PostCSS extends Adapter
name: 'postcss'
extensions: ['css']
output: 'css'

_render: (str, options) ->
use = options.use ? []
processor = @engine(use)

options.map = {inline: false} if options.map is true

W(processor.process(str, options))
.then (res) ->
{ result: res.css, sourcemap: res.map }

module.exports = PostCSS
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -17,7 +17,9 @@
"lodash": "3.x",
"resolve": "1.x",
"uglify-js": "2.x",
"when": "3.x"
"when": "3.x",
"postcss": "~4.1.5",
"postcss-simple-vars": "~0.2.4"
},
"devDependencies": {
"acorn": "latest",
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Expand Up @@ -104,6 +104,7 @@ It's also important to note that accord returns an object rather than a string f
- [scss](https://github.com/andrew/node-sass)
- [less](https://github.com/less/less.js/)
- [myth](https://github.com/segmentio/myth)
- [postcss](https://github.com/postcss/postcss)

#### JavaScript

Expand Down Expand Up @@ -156,6 +157,7 @@ Accord now supports source map generation for any language that also supports so
- coffeescript
- minify-js
- 6to5
- postcss

Accord returns all source maps as javascript objects, and if available will prefer a [v3 sourcemap](https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit) over any other format. You can find the primary sourcemap on the object returned from accord under the `sourcemap` key. If there are multiple sourcemaps generated, alternate ones will be avaiable under different keys, which you can find on the object returned from accord after a compile.

Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/postcss/basic.css
@@ -0,0 +1,3 @@
.test2 {
color: blue;
}
3 changes: 3 additions & 0 deletions test/fixtures/postcss/expected/basic.css
@@ -0,0 +1,3 @@
.test2 {
color: blue;
}
1 change: 1 addition & 0 deletions test/fixtures/postcss/expected/string.css
@@ -0,0 +1 @@
.test { color: green; }
3 changes: 3 additions & 0 deletions test/fixtures/postcss/expected/var.css
@@ -0,0 +1,3 @@
.test {
background: green;
}
5 changes: 5 additions & 0 deletions test/fixtures/postcss/var.css
@@ -0,0 +1,5 @@
$color: green;

.test {
background: $color;
}
28 changes: 28 additions & 0 deletions test/test.coffee
Expand Up @@ -1162,3 +1162,31 @@ describe 'cjsx', ->
it 'should not be able to compile', (done) ->
@cjsx.compile()
.done(((r) -> should.not.exist(r); done()), ((r) -> should.exist(r); done()))

describe 'postcss', ->

before ->
@postcss = accord.load('postcss')
@path = path.join(__dirname, 'fixtures', 'postcss')

it 'should expose name, extensions, output, and engine', ->
@postcss.extensions.should.be.an.instanceOf(Array)
@postcss.output.should.be.a('string')
@postcss.engine.should.be.ok
@postcss.name.should.be.ok

it 'should render a string', (done) ->
@postcss.render('.test { color: green; }')
.done((res) => should.match_expected(@postcss, res.result, path.join(@path, 'string.css'), done))

it 'should render a file', (done) ->
lpath = path.join(@path, 'basic.css')
@postcss.renderFile(lpath)
.done((res) => should.match_expected(@postcss, res.result, lpath, done))

it 'should render a file with plugin', (done) ->
lpath = path.join(@path, 'var.css')
varsPlugin = require('postcss-simple-vars')
@postcss.renderFile(lpath, {use: [varsPlugin]})
.done((res) => should.match_expected(@postcss, res.result, lpath, done))

0 comments on commit 2362ca4

Please sign in to comment.