Skip to content

Commit e909c3b

Browse files
committed
Plugin code and simple test
1 parent 893e7d0 commit e909c3b

File tree

6 files changed

+162
-25
lines changed

6 files changed

+162
-25
lines changed

.gitignore

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,3 @@
1-
# Logs
2-
logs
3-
*.log
4-
5-
# Runtime data
6-
pids
7-
*.pid
8-
*.seed
9-
10-
# Directory for instrumented libs generated by jscoverage/JSCover
11-
lib-cov
12-
13-
# Coverage directory used by tools like istanbul
14-
coverage
15-
16-
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
17-
.grunt
18-
19-
# Compiled binary addons (http://nodejs.org/api/addons.html)
20-
build/Release
21-
22-
# Dependency directory
23-
# Deployed apps should consider commenting this line out:
24-
# see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git
251
node_modules
2+
npm-debug.log
3+
.DS_Store

.jshintrc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"asi": true,
3+
"boss": true,
4+
"browser": true,
5+
"node": true,
6+
"expr": true,
7+
"indent": 2,
8+
"laxcomma": true,
9+
"maxlen": 100,
10+
"newcap": true,
11+
"strict": false,
12+
"trailing": true,
13+
"undef": true,
14+
"unused": true,
15+
"quotmark": "single"
16+
}

README.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,36 @@
11
gulp-postcss
22
============
33

4-
PostCSS gulp plugin
4+
[PostCSS](https://github.com/postcss/postcss) gulp plugin
5+
6+
## Basic usage
7+
8+
```
9+
var postcss = require('gulp-postcss')
10+
var gulp = require('gulp')
11+
var autoprefixer = require('autoprefixer')
12+
var mqpacker = require('css-mqpacker')
13+
14+
gulp.task('css', function () {
15+
var processors = [
16+
autoprefixer('last 1 version').postcss
17+
, mqpacker.processor
18+
]
19+
return gulp.src('./src/*.css')
20+
.pipe(postcss(processors))
21+
.pipe(gulp.dest('./dest'))
22+
})
23+
```
24+
25+
## Source map support
26+
27+
Source map is inlined by default, to extract map use together
28+
with [gulp-sourcemaps](https://github.com/floridoo/gulp-sourcemaps).
29+
30+
```
31+
return gulp.src('./src/*.css')
32+
.pipe(sourcemaps.init())
33+
.pipe(postcss(processors))
34+
.pipe(sourcemaps.write('.'))
35+
.pipe(gulp.dest('./dest'))
36+
```

index.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
var through = require('through2')
2+
var postcss = require('postcss')
3+
var applySourceMap = require('vinyl-sourcemaps-apply')
4+
var _ = require('lodash')
5+
6+
7+
module.exports = function (processors, options) {
8+
9+
return through.obj(transform)
10+
11+
function transform (file, encoding, cb) {
12+
13+
// Source map is inline by default
14+
var opts = _.extend({ map: 'inline' }, options)
15+
var processor = postcss()
16+
var result
17+
18+
if (file.base && file.path) {
19+
opts.from = file.relative
20+
} else {
21+
opts.from = file.path
22+
}
23+
24+
processors.forEach(processor.use.bind(processor))
25+
26+
// Generate separate source map for gulp-sourcemap
27+
if (file.sourceMap) {
28+
opts.map = true
29+
}
30+
31+
result = processor.process(file.contents.toString('utf8'), opts)
32+
33+
file.contents = new Buffer(result.css)
34+
35+
// Apply source map to the chain
36+
if (file.sourceMap) {
37+
applySourceMap(file, result.map.toString())
38+
}
39+
40+
cb(null, file)
41+
}
42+
43+
}

package.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "gulp-postcss",
3+
"version": "0.0.1",
4+
"description": "PostCSS gulp plugin",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "mocha"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/w0rm/gulp-postcss.git"
12+
},
13+
"keywords": [
14+
"gulp",
15+
"postcss",
16+
"css"
17+
],
18+
"author": "Andrey Kuzmin <[email protected]>",
19+
"license": "MIT",
20+
"bugs": {
21+
"url": "https://github.com/w0rm/gulp-postcss/issues"
22+
},
23+
"homepage": "https://github.com/w0rm/gulp-postcss",
24+
"dependencies": {
25+
"gulp-util": "^3.0.0",
26+
"lodash": "^2.4.1",
27+
"postcss": "^2.1.2",
28+
"through2": "^0.6.1",
29+
"vinyl-sourcemaps-apply": "^0.1.1"
30+
},
31+
"devDependencies": {
32+
"mocha": "^1.21.4"
33+
}
34+
}

test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* global it */
2+
3+
var assert = require('assert')
4+
var gutil = require('gulp-util')
5+
var postcss = require('.')
6+
7+
it('should transform css with multiple processors', function (cb) {
8+
9+
var stream = postcss(
10+
[ doubler, doubler ]
11+
, { map: false } // omit source map for the test
12+
)
13+
14+
stream.on('data', function (file) {
15+
var result = file.contents.toString('utf8')
16+
var target = 'a { color: black; color: black; color: black; color: black }'
17+
assert.equal( result, target )
18+
cb()
19+
})
20+
21+
stream.write(new gutil.File({
22+
contents: new Buffer('a { color: black }')
23+
}))
24+
25+
stream.end()
26+
27+
})
28+
29+
30+
function doubler (css) {
31+
css.eachDecl(function (decl) {
32+
decl.parent.prepend(decl.clone())
33+
})
34+
}

0 commit comments

Comments
 (0)