Skip to content

Commit

Permalink
Merge pull request #170 from JohnAlbin/pass-options-to-plugins
Browse files Browse the repository at this point in the history
Ensure options are passed to plugins when using postcss.config.js
  • Loading branch information
w0rm committed Jan 12, 2024
2 parents bd9d4c0 + 83dcfcd commit c21fca9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
18 changes: 17 additions & 1 deletion README.md
Expand Up @@ -92,6 +92,21 @@ gulp.task('default', function () {
});
```

If you are using a `postcss.config.js` file, you can pass PostCSS options as the first argument to gulp-postcss.

This, for instance, will let PostCSS know what the final file destination path is, since it will be unaware of the path given to `gulp.dest()`:

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

gulp.task('default', function () {
return gulp.src('in.scss')
.pipe(postcss({ to: 'out/in.css' }))
.pipe(gulp.dest('out'));
});
```

## Using a custom processor

```js
Expand Down Expand Up @@ -178,9 +193,10 @@ gulp.task('css', function () {
```

```js
// postcss.config.js or .postcssrc.js
module.exports = function (ctx) {
var file = ctx.file;
var options = ctx.options;
var options = ctx;
return {
parser: file.extname === '.sss' ? : 'sugarss' : false,
plugins: {
Expand Down
8 changes: 4 additions & 4 deletions index.js
Expand Up @@ -127,11 +127,11 @@ function withConfigLoader(cb) {
} else {
configPath = file.dirname
}
// @TODO: The options property is deprecated and should be removed in 10.0.0.
contextOptions.options = Object.assign({}, contextOptions)
contextOptions.file = file
return postcssLoadConfig(
{
file: file,
options: contextOptions
},
contextOptions,
configPath
)
})
Expand Down
15 changes: 8 additions & 7 deletions test.js
Expand Up @@ -251,7 +251,7 @@ describe('PostCSS Guidelines', function () {

it('should allow override of `to` processing option', function (cb) {

var stream = postcss([ doubler ], {to: 'overriden'})
var stream = postcss([ doubler ], {to: 'overridden'})
postcssStub.process.returns(Promise.resolve({
css: '',
warnings: function () {
Expand All @@ -260,7 +260,7 @@ describe('PostCSS Guidelines', function () {
}))

stream.on('data', function () {
assert.equal(postcssStub.process.getCall(0).args[1].to, 'overriden')
assert.equal(postcssStub.process.getCall(0).args[1].to, 'overridden')
cb()
})

Expand All @@ -282,7 +282,7 @@ describe('PostCSS Guidelines', function () {
var plugins = [ doubler ]
var callback = sandbox.stub().returns({
plugins: plugins,
options: { to: 'overriden' }
options: { to: 'overridden' }
})
var stream = postcss(callback)

Expand All @@ -296,7 +296,7 @@ describe('PostCSS Guidelines', function () {
stream.on('data', function () {
assert.equal(callback.getCall(0).args[0], file)
assert.equal(postcssStub.use.getCall(0).args[0], plugins)
assert.equal(postcssStub.process.getCall(0).args[1].to, 'overriden')
assert.equal(postcssStub.process.getCall(0).args[1].to, 'overridden')
cb()
})

Expand All @@ -316,7 +316,7 @@ describe('PostCSS Guidelines', function () {

postcssLoadConfigStub.returns(Promise.resolve({
plugins: plugins,
options: { to: 'overriden' }
options: { to: 'overridden' }
}))

postcssStub.process.returns(Promise.resolve({
Expand All @@ -329,10 +329,11 @@ describe('PostCSS Guidelines', function () {
stream.on('data', function () {
assert.deepEqual(postcssLoadConfigStub.getCall(0).args[0], {
file: file,
to: 'initial',
options: { to: 'initial' }
})
assert.equal(postcssStub.use.getCall(0).args[0], plugins)
assert.equal(postcssStub.process.getCall(0).args[1].to, 'overriden')
assert.equal(postcssStub.process.getCall(0).args[1].to, 'overridden')
cb()
})

Expand Down Expand Up @@ -402,7 +403,7 @@ describe('PostCSS Guidelines', function () {
})

it('should not override `from` and `map` if using gulp-sourcemaps', function (cb) {
var stream = postcss([ doubler ], { from: 'overriden', map: 'overriden' })
var stream = postcss([ doubler ], { from: 'overridden', map: 'overridden' })
var cssPath = __dirname + '/fixture.css'
postcssStub.process.returns(Promise.resolve({
css: '',
Expand Down

0 comments on commit c21fca9

Please sign in to comment.