Skip to content

Feature/postcss option as function #12

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
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
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,43 @@ module.exports = {
}
```

## Plugins Function

If you need to link any of the postcss plugins to the webpack context, you can
define a function that returns your plugins. The function is executed with the
same context provided to postcss-loader, allowing access to webpack loader API

```js
var cssimport = require('postcss-import');
var autoprefixer = require('autoprefixer-core');

module.exports = {
module: {
loaders: [
{
test: /\.css$/,
loader: "style-loader!css-loader!postcss-loader"
}
]
},
postcss: function () {
// The context of this function is the same provided to postcss-loader
// see: http://webpack.github.io/docs/loaders.html

return [
cssimport({
// see postcss-import docs to learn about onImport param
// https://github.com/postcss/postcss-import

onImport: function (files) {
files.forEach(this.addDependency);
}.bind(this)
}),
autoprefixer
];
}
}
```

## Safe Mode

Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ module.exports = function (source, map) {
if ( params.safe ) opts.safe = true;

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

if ( typeof plugins === 'undefined' ) {
plugins = [];
} else if ( params.pack ) {
Expand Down
8 changes: 5 additions & 3 deletions test/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ module.exports = {
path: path.join(__dirname, '..', 'build'),
filename: 'test.js'
},
postcss: {
defaults: [blue, red],
blues: [blue]
postcss: function () {
return {
defaults: [blue, red],
blues: [blue]
};
}
};