Skip to content

Commit

Permalink
Merge branch 'master' into error_details
Browse files Browse the repository at this point in the history
  • Loading branch information
刘祺 committed Jan 23, 2017
2 parents fe8e5f5 + 604a264 commit f7535b8
Show file tree
Hide file tree
Showing 5 changed files with 352 additions and 64 deletions.
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
sudo: false
language: node_js
node_js:
- iojs
- "stable"
- "0.12"
- "0.10"
- stable
- 6
- 4
- 0.12
113 changes: 97 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# gulp-postcss [![Build Status](https://api.travis-ci.org/postcss/gulp-postcss.png)](https://travis-ci.org/postcss/gulp-postcss)

[PostCSS](https://github.com/postcss/postcss) gulp plugin to pipe CSS through
several processors, but parse CSS only once.
several plugins, but parse CSS only once.

## Install

Expand All @@ -11,19 +11,36 @@ Install required [postcss plugins](https://www.npmjs.com/browse/keyword/postcss-

## Basic usage

The configuration is loaded automatically from `postcss.config.js`
as [described here](https://www.npmjs.com/package/postcss-load-config),
so you don't have to specify any options.

```js
var postcss = require('gulp-postcss');
var gulp = require('gulp');

gulp.task('css', function () {
return gulp.src('./src/*.css')
.pipe(postcss())
.pipe(gulp.dest('./dest'));
});
```

## Passing plugins directly

```js
var postcss = require('gulp-postcss');
var gulp = require('gulp');
var autoprefixer = require('autoprefixer');
var cssnano = require('cssnano');

gulp.task('css', function () {
var processors = [
var plugins = [
autoprefixer({browsers: ['last 1 version']}),
cssnano(),
cssnano()
];
return gulp.src('./src/*.css')
.pipe(postcss(processors))
.pipe(postcss(plugins))
.pipe(gulp.dest('./dest'));
});
```
Expand All @@ -32,18 +49,18 @@ gulp.task('css', function () {

The second optional argument to gulp-postcss is passed to PostCSS.

This, for instance, may be used to enable custom syntax:
This, for instance, may be used to enable custom parser:

```js
var gulp = require('gulp');
var postcss = require('gulp-postcss');
var nested = require('postcss-nested');
var scss = require('postcss-scss');
var sugarss = require('sugarss');

gulp.task('default', function () {
var processors = [nested];
return gulp.src('in.css')
.pipe(postcss(processors, {syntax: scss}))
var plugins = [nested];
return gulp.src('in.sss')
.pipe(postcss(plugins, { parser: sugarss }))
.pipe(gulp.dest('out'));
});
```
Expand All @@ -65,12 +82,12 @@ var opacity = function (css, opts) {
};

gulp.task('css', function () {
var processors = [
var plugins = [
cssnext({browsers: ['last 1 version']}),
opacity,
opacity
];
return gulp.src('./src/*.css')
.pipe(postcss(processors))
.pipe(postcss(plugins))
.pipe(gulp.dest('./dest'));
});
```
Expand All @@ -83,13 +100,77 @@ with [gulp-sourcemaps](https://github.com/floridoo/gulp-sourcemaps).
```js
return gulp.src('./src/*.css')
.pipe(sourcemaps.init())
.pipe(postcss(processors))
.pipe(postcss(plugins))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('./dest'));
```

## Advanced usage

If you want to configure postcss on per-file-basis, you can pass a callback
that receives [vinyl file object](https://github.com/gulpjs/vinyl) and returns
`{ plugins: plugins, options: options }`. For example, when you need to
parse different extensions differntly:

```js
var gulp = require('gulp');
var postcss = require('gulp-postcss');

gulp.task('css', function () {
function callback(file) {
return {
plugins: [
require('postcss-import')({ root: file.dirname }),
require('postcss-modules')
],
options: {
parser: file.extname === '.sss' ? require('sugarss') : false
}
}
}
return gulp.src('./src/*.css')
.pipe(postcss(callback))
.pipe(gulp.dest('./dest'));
});
```

The same result may be achieved with
[`postcss-load-config`](https://www.npmjs.com/package/postcss-load-config),
because it receives `ctx` with the context options and the vinyl file.

```js
var gulp = require('gulp');
var postcss = require('gulp-postcss');

gulp.task('css', function () {
var contextOptions = { modules: true };
return gulp.src('./src/*.css')
.pipe(postcss(contextOptions))
.pipe(gulp.dest('./dest'));
});
```

```js
module.exports = function (ctx) {
var file = ctx.file;
var options = ctx.options;
return {
parser: file.extname === '.sss' ? : 'sugarss' : false,
plugins: {
'postcss-import': { root: file.dirname }
'postcss-modules': options.modules ? {} : false
}
}
})
```

## Changelog

* 6.3.0
* Integrated with postcss-load-config
* Added a callback to configure postcss on per-file-basis
* Dropped node 0.10 support

* 6.2.0
* Fix syntax error message for PostCSS 5.2 compatibility

Expand Down Expand Up @@ -124,7 +205,7 @@ return gulp.src('./src/*.css')

* 5.1.4
* Simplified error handling
* Simplified postcss execution with object processors
* Simplified postcss execution with object plugins

* 5.1.3 Updated travis banner

Expand All @@ -139,7 +220,7 @@ return gulp.src('./src/*.css')
* Display `result.warnings()` content

* 5.0.1
* Fix to support object processors
* Fix to support object plugins

* 5.0.0
* Use async API
Expand Down Expand Up @@ -173,7 +254,7 @@ return gulp.src('./src/*.css')
* Improved README

* 1.0.1
* Don't add source map comment if used with gulp-sourcemap
* Don't add source map comment if used with gulp-sourcemaps

* 1.0.0
* Initial release
92 changes: 67 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ var gutil = require('gulp-util')
var path = require('path')


module.exports = function (processors, options) {

if (!Array.isArray(processors)) {
throw new gutil.PluginError('gulp-postcss', 'Please provide array of postcss processors!')
}
module.exports = withConfigLoader(function (loadConfig) {

var stream = new Stream.Transform({ objectMode: true })

Expand All @@ -23,29 +19,36 @@ module.exports = function (processors, options) {
return handleError('Streams are not supported!')
}

// Source map is disabled by default
var opts = { map: false }
var attr
// Protect `from` and `map` if using gulp-sourcemaps
var isProtected = file.sourceMap
? { from: true, map: true }
: {}

// Extend default options
if (options) {
for (attr in options) {
if (options.hasOwnProperty(attr)) {
opts[attr] = options[attr]
}
}
var options = {
from: file.path
, to: file.path
// Generate a separate source map for gulp-sourcemaps
, map: file.sourceMap ? { annotation: false } : false
}

opts.from = file.path
opts.to = opts.to || file.path

// Generate separate source map for gulp-sourcemap
if (file.sourceMap) {
opts.map = { annotation: false }
}

postcss(processors)
.process(file.contents, opts)
loadConfig(file)
.then(function (config) {
var configOpts = config.options || {}
// Extend the default options if not protected
for (var opt in configOpts) {
if (configOpts.hasOwnProperty(opt) && !isProtected[opt]) {
options[opt] = configOpts[opt]
} else {
gutil.log(
'gulp-postcss:',
file.relative + '\nCannot override ' + opt +
' option, because it is required by gulp-sourcemaps'
)
}
}
return postcss(config.plugins || [])
.process(file.contents, options)
})
.then(handleResult, handleError)

function handleResult (result) {
Expand Down Expand Up @@ -93,4 +96,43 @@ module.exports = function (processors, options) {
}

return stream
})


function withConfigLoader(cb) {
return function (plugins, options) {
if (Array.isArray(plugins)) {
return cb(function () {
return Promise.resolve({
plugins: plugins
, options: options
})
})
} else if (typeof plugins === 'function') {
return cb(function (file) {
return Promise.resolve(plugins(file))
})
} else {
var postcssLoadConfig = require('postcss-load-config')
var contextOptions = plugins || {}
return cb(function(file) {
var configPath
if (contextOptions.config) {
if (path.isAbsolute(contextOptions.config)) {
configPath = contextOptions.config
} else {
configPath = path.join(file.base, contextOptions.config)
}
} else {
configPath = file.dirname
}
return postcssLoadConfig(
{ file: file
, options: contextOptions
},
configPath
)
})
}
}
}
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gulp-postcss",
"version": "6.2.0",
"version": "6.3.0",
"description": "PostCSS gulp plugin",
"main": "index.js",
"scripts": {
Expand All @@ -23,14 +23,14 @@
},
"homepage": "https://github.com/postcss/gulp-postcss",
"dependencies": {
"gulp-util": "^3.0.7",
"postcss": "^5.2.0",
"vinyl-sourcemaps-apply": "^0.2.0"
"gulp-util": "^3.0.8",
"postcss": "^5.2.10",
"postcss-load-config": "^1.1.0",
"vinyl-sourcemaps-apply": "^0.2.1"
},
"devDependencies": {
"es6-promise": "^3.0.2",
"gulp-sourcemaps": "^1.5.1",
"mocha": "^2.2.5",
"gulp-sourcemaps": "^1.11.0",
"mocha": "^3.2.0",
"proxyquire": "^1.7.4",
"sinon": "^1.17.3"
}
Expand Down
Loading

0 comments on commit f7535b8

Please sign in to comment.