Skip to content

Add capability to pass other options as function #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,25 @@ var css = require('postcss?parser=postcss-safe-parser!./broken')
```

[Safe Parser]: https://github.com/postcss/postcss-safe-parser

If you need to pass the function directly instead of a module name, you can do so through the webpack postcss option, as such:

```js
var sugarss = require('sugarss')
module.exports = {
module: {
loaders: [
{
test: /\.css$/,
loader: "style-loader!css-loader!postcss-loader"
}
]
},
postcss: function () {
return {
plugins: [autoprefixer, precss],
syntax: sugarss
};
}
}
```
32 changes: 21 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,31 @@ module.exports = function (source, map) {
if ( typeof map === 'string' ) map = JSON.parse(map);
if ( map && map.mappings ) opts.map.prev = map;

if ( params.syntax ) opts.syntax = require(params.syntax);
if ( params.parser ) opts.parser = require(params.parser);
if ( params.stringifier ) opts.stringifier = require(params.stringifier);

var plugins = this.options.postcss;
if ( typeof plugins === 'function' ) {
plugins = plugins.call(this, this);
var plugins;
var options = this.options.postcss;
if ( typeof options === 'function' ) {
options = options.call(this, this);
}

if ( typeof plugins === 'undefined' ) {
if ( typeof options === 'undefined' ) {
plugins = [];
} else if ( params.pack ) {
plugins = plugins[params.pack];
} else if ( !Array.isArray(plugins) ) {
plugins = plugins.defaults;
plugins = options[params.pack];
} else if ( !Array.isArray(options) ) {
plugins = options.plugins || options.defaults;
opts.syntax = options.syntax;
opts.parser = options.parser;
opts.stringifier = options.stringifier;
}

if ( params.syntax && !opts.syntax ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed && !opts.syntax to allow override syntax in query (somebody may need it)

opts.syntax = require(params.syntax);
}
if ( params.parser && !opts.parser ) {
opts.parser = require(params.parser);
}
if ( params.stringifier && !opts.stringifier ) {
opts.stringifier = require(params.stringifier);
}

var loader = this;
Expand Down