Skip to content

Commit 9f41f4e

Browse files
committed
simple api
1 parent 447ac83 commit 9f41f4e

File tree

5 files changed

+35
-29
lines changed

5 files changed

+35
-29
lines changed

README.md

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ var plugin = require("browserify-postcss")
1010

1111
var tr = plugin("fake.css", {
1212
plugin: "postcss-simple-vars",
13-
resolve: {
14-
basedir: __dirname
15-
}
13+
basedir: __dirname
1614
})
1715
tr.end("$color: red; .fake { color: $color; }")
1816
tr.pipe(process.stdout)
@@ -37,17 +35,4 @@ Default: `null`
3735

3836
postcss plugins used to transform the content
3937

40-
#### options
41-
42-
Type: `Object`
43-
Default: `null`
44-
45-
options for postcss plugins specified by `opts.plugin`
46-
47-
#### resolve
48-
49-
Type: `Object|Function`
50-
Default: `null`
51-
52-
If `Object`, will be passed to [resolve.sync](https://github.com/substack/node-resolve#resolvesyncid-opts) to resolve the plugins.
53-
If `Function`, will be used instead of [resolve.sync](https://github.com/substack/node-resolve#resolvesyncid-opts)
38+
If `Array`, each element can be `String`, `Function`, or `Array`.

example/vars.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ var plugin = require("..")
22

33
var tr = plugin("fake.css", {
44
plugin: "postcss-simple-vars",
5-
resolve: {
6-
basedir: __dirname
7-
}
5+
basedir: __dirname
86
})
97
tr.end("$color: red; .fake { color: $color; }")
108
tr.pipe(process.stdout)

index.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,42 @@
1-
var Processor = require("postcss-processor")
21
var sink = require("sink-transform")
2+
var resolve = require("resolve")
3+
var postcss = require("postcss")
34

45
var processor
56
module.exports = function (file, opts) {
67
opts = opts || {}
78
if (!processor) {
8-
processor = Processor(opts.plugin, opts.options, opts.resolve)
9+
processor = getProcessor(opts)
910
}
10-
return sink.str(function (body, done) {
11+
var stream = sink.str(function (body, done) {
1112
var self = this
1213
processor.process(body, { from: file })
1314
.then(function (result) {
1415
self.push(result.css)
1516
done()
17+
}, function (err) {
18+
stream.emit("error", err)
1619
})
1720
})
21+
return stream
22+
}
23+
24+
function getProcessor(opts) {
25+
var plugins = [].concat(opts.plugin).filter(Boolean)
26+
plugins = plugins.map(function (p) {
27+
var op
28+
if (Array.isArray(p)) {
29+
op = p[1]
30+
p = p[0]
31+
}
32+
if (typeof p !== "function") {
33+
var pfile = resolve.sync(String(p), { basedir: opts.basedir || process.cwd() })
34+
p = require(pfile)
35+
}
36+
return p(op)
37+
})
38+
39+
return postcss(plugins.concat(
40+
opts.processor && opts.processor.plugins || []
41+
))
1842
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
},
2222
"homepage": "https://github.com/zoubin/browserify-postcss#readme",
2323
"dependencies": {
24-
"postcss-processor": "^0.1.0",
24+
"postcss": "^4.1.11",
25+
"resolve": "^1.1.6",
2526
"sink-transform": "^0.1.2"
2627
},
2728
"devDependencies": {

test/index.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
var processor = require("..")
1+
var plugin = require("..")
22
var sink = require("sink-transform")
33
var test = require("tape")
44

55
test('transform', function(t) {
66
t.plan(1)
7-
var tr = processor("fake.css", {
7+
var tr = plugin("fake.css", {
88
plugin: "postcss-simple-vars",
9-
resolve: {
10-
basedir: __dirname
11-
}
9+
basedir: __dirname
1210
})
1311
tr.end("$color: red; .fake { color: $color; }")
1412
tr.pipe(sink.str(function (body) {

0 commit comments

Comments
 (0)