Skip to content

Commit 0231cba

Browse files
committed
Merge pull request webpack-contrib#59 from jenius/master
Add capability to pass other options as function
2 parents 8e7ae53 + 523249a commit 0231cba

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
@@ -211,3 +211,25 @@ var css = require('postcss?parser=postcss-safe-parser!./broken')
211211
```
212212

213213
[Safe Parser]: https://github.com/postcss/postcss-safe-parser
214+
215+
If you need to pass the function directly instead of a module name, you can do so through the webpack postcss option, as such:
216+
217+
```js
218+
var sugarss = require('sugarss')
219+
module.exports = {
220+
module: {
221+
loaders: [
222+
{
223+
test: /\.css$/,
224+
loader: "style-loader!css-loader!postcss-loader"
225+
}
226+
]
227+
},
228+
postcss: function () {
229+
return {
230+
plugins: [autoprefixer, precss],
231+
syntax: sugarss
232+
};
233+
}
234+
}
235+
```

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)