Skip to content

Commit 523249a

Browse files
author
Jeff Escalante
committed
add capability to pass other options as function
syntax, parser, and stringifier can now be returned from an object in options.postcss and therefore passed as function, not strings only.
1 parent f533137 commit 523249a

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,25 @@ var css = require('postcss?parser=postcss-safe-parser!./broken')
198198
```
199199

200200
[Safe Parser]: https://github.com/postcss/postcss-safe-parser
201+
202+
If you need to pass the function directly instead of a module name, you can do so through the webpack postcss option, as such:
203+
204+
```js
205+
var sugarss = require('sugarss')
206+
module.exports = {
207+
module: {
208+
loaders: [
209+
{
210+
test: /\.css$/,
211+
loader: "style-loader!css-loader!postcss-loader"
212+
}
213+
]
214+
},
215+
postcss: function () {
216+
return {
217+
plugins: [autoprefixer, precss],
218+
syntax: sugarss
219+
};
220+
}
221+
}
222+
```

index.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,31 @@ module.exports = function (source, map) {
1919
if ( typeof map === 'string' ) map = JSON.parse(map);
2020
if ( map && map.mappings ) opts.map.prev = map;
2121

22-
if ( params.syntax ) opts.syntax = require(params.syntax);
23-
if ( params.parser ) opts.parser = require(params.parser);
24-
if ( params.stringifier ) opts.stringifier = require(params.stringifier);
25-
26-
var plugins = this.options.postcss;
27-
if ( typeof plugins === 'function' ) {
28-
plugins = plugins.call(this, this);
22+
var plugins;
23+
var options = this.options.postcss;
24+
if ( typeof options === 'function' ) {
25+
options = options.call(this, this);
2926
}
3027

31-
if ( typeof plugins === 'undefined' ) {
28+
if ( typeof options === 'undefined' ) {
3229
plugins = [];
3330
} else if ( params.pack ) {
34-
plugins = plugins[params.pack];
35-
} else if ( !Array.isArray(plugins) ) {
36-
plugins = plugins.defaults;
31+
plugins = options[params.pack];
32+
} else if ( !Array.isArray(options) ) {
33+
plugins = options.plugins || options.defaults;
34+
opts.syntax = options.syntax;
35+
opts.parser = options.parser;
36+
opts.stringifier = options.stringifier;
37+
}
38+
39+
if ( params.syntax && !opts.syntax ) {
40+
opts.syntax = require(params.syntax);
41+
}
42+
if ( params.parser && !opts.parser ) {
43+
opts.parser = require(params.parser);
44+
}
45+
if ( params.stringifier && !opts.stringifier ) {
46+
opts.stringifier = require(params.stringifier);
3747
}
3848

3949
var loader = this;

0 commit comments

Comments
 (0)