Skip to content

Commit c21fca9

Browse files
authored
Merge pull request #170 from JohnAlbin/pass-options-to-plugins
Ensure options are passed to plugins when using postcss.config.js
2 parents bd9d4c0 + 83dcfcd commit c21fca9

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,21 @@ gulp.task('default', function () {
9292
});
9393
```
9494

95+
If you are using a `postcss.config.js` file, you can pass PostCSS options as the first argument to gulp-postcss.
96+
97+
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()`:
98+
99+
```js
100+
var gulp = require('gulp');
101+
var postcss = require('gulp-postcss');
102+
103+
gulp.task('default', function () {
104+
return gulp.src('in.scss')
105+
.pipe(postcss({ to: 'out/in.css' }))
106+
.pipe(gulp.dest('out'));
107+
});
108+
```
109+
95110
## Using a custom processor
96111

97112
```js
@@ -178,9 +193,10 @@ gulp.task('css', function () {
178193
```
179194

180195
```js
196+
// postcss.config.js or .postcssrc.js
181197
module.exports = function (ctx) {
182198
var file = ctx.file;
183-
var options = ctx.options;
199+
var options = ctx;
184200
return {
185201
parser: file.extname === '.sss' ? : 'sugarss' : false,
186202
plugins: {

index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ function withConfigLoader(cb) {
127127
} else {
128128
configPath = file.dirname
129129
}
130+
// @TODO: The options property is deprecated and should be removed in 10.0.0.
131+
contextOptions.options = Object.assign({}, contextOptions)
132+
contextOptions.file = file
130133
return postcssLoadConfig(
131-
{
132-
file: file,
133-
options: contextOptions
134-
},
134+
contextOptions,
135135
configPath
136136
)
137137
})

test.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ describe('PostCSS Guidelines', function () {
251251

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

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

262262
stream.on('data', function () {
263-
assert.equal(postcssStub.process.getCall(0).args[1].to, 'overriden')
263+
assert.equal(postcssStub.process.getCall(0).args[1].to, 'overridden')
264264
cb()
265265
})
266266

@@ -282,7 +282,7 @@ describe('PostCSS Guidelines', function () {
282282
var plugins = [ doubler ]
283283
var callback = sandbox.stub().returns({
284284
plugins: plugins,
285-
options: { to: 'overriden' }
285+
options: { to: 'overridden' }
286286
})
287287
var stream = postcss(callback)
288288

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

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

317317
postcssLoadConfigStub.returns(Promise.resolve({
318318
plugins: plugins,
319-
options: { to: 'overriden' }
319+
options: { to: 'overridden' }
320320
}))
321321

322322
postcssStub.process.returns(Promise.resolve({
@@ -329,10 +329,11 @@ describe('PostCSS Guidelines', function () {
329329
stream.on('data', function () {
330330
assert.deepEqual(postcssLoadConfigStub.getCall(0).args[0], {
331331
file: file,
332+
to: 'initial',
332333
options: { to: 'initial' }
333334
})
334335
assert.equal(postcssStub.use.getCall(0).args[0], plugins)
335-
assert.equal(postcssStub.process.getCall(0).args[1].to, 'overriden')
336+
assert.equal(postcssStub.process.getCall(0).args[1].to, 'overridden')
336337
cb()
337338
})
338339

@@ -402,7 +403,7 @@ describe('PostCSS Guidelines', function () {
402403
})
403404

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

0 commit comments

Comments
 (0)