Skip to content

Commit c8b785a

Browse files
tinchoz49ahdinosaur
authored andcommitted
added: allow set options for each postcss plugin (#3)
1 parent e30236e commit c8b785a

File tree

4 files changed

+52
-17
lines changed

4 files changed

+52
-17
lines changed

index.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,17 @@ function transform (filename, source, options, done) {
1111
const basedir = options.basedir
1212

1313
const plugins = defined(options.plugins, [])
14-
.map(plugin => resolve.sync(plugin, { basedir }))
15-
.map(require)
14+
.map(plugin => {
15+
if (typeof plugin === 'string') {
16+
plugin = [ plugin ]
17+
}
18+
19+
return {
20+
path: resolve.sync(plugin[0], { basedir }),
21+
options: plugin[1]
22+
}
23+
})
24+
.map(plugin => require(plugin.path)(plugin.options))
1625

1726
postcss(plugins)
1827
.process(source, extend({
@@ -23,10 +32,10 @@ function transform (filename, source, options, done) {
2332
console: false
2433
}
2534
}, options))
26-
.then(function (result) {
27-
done(null, result.css)
28-
})
29-
.catch(function (err) {
30-
done(err)
31-
})
35+
.then(function (result) {
36+
done(null, result.css)
37+
})
38+
.catch(function (err) {
39+
done(err)
40+
})
3241
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "postcss transform for sheetify, use all the plugins!",
55
"main": "index.js",
66
"scripts": {
7-
"test:unit": "pull-test test/*.js",
7+
"test:unit": "tape test/*.js",
88
"test:deps": "dependency-check . && dependency-check . --extra --no-dev",
99
"test:lint": "standard",
1010
"test": "npm-run-all -p test:*",

test/index.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
1-
var sheetifyPostcss
2-
3-
module.exports = {
4-
'sheetify-postcss': function (assert) {
5-
sheetifyPostcss = require('../')
6-
assert.ok(sheetifyPostcss, 'module is require-able')
7-
}
8-
}
1+
const test = require('tape')
2+
const sheetifyPostcss = require('../')
3+
4+
test(function (t) {
5+
t.test('module should work with postcss plugins without options', function (t) {
6+
t.plan(2)
7+
8+
sheetifyPostcss('test.css', '.rule {}', { basedir: __dirname, plugins: [ './stubs/postcss-plugin' ] }, (err, css) => {
9+
t.equal(err, null)
10+
t.equal(css, '.ok {}')
11+
})
12+
})
13+
14+
t.test('module should work with postcss plugins and their options', function (t) {
15+
t.plan(2)
16+
17+
sheetifyPostcss('test.css', '.rule {}', { basedir: __dirname, plugins: [ [ './stubs/postcss-plugin', { has: true } ] ] }, (err, css) => {
18+
t.equal(err, null)
19+
t.equal(css, '.ok-with-options {}')
20+
})
21+
})
22+
})
23+

test/stubs/postcss-plugin.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var postcss = require('postcss')
2+
3+
module.exports = postcss.plugin('postcss-plugin', function postcssPlugin (options) {
4+
options = options || {}
5+
6+
return function (root, result) {
7+
root.walkRules('.rule', rule => {
8+
rule.replaceWith(postcss.rule({ selector: options.has ? '.ok-with-options' : '.ok' }))
9+
})
10+
}
11+
})

0 commit comments

Comments
 (0)