Skip to content

Commit e7304c5

Browse files
committed
Merge pull request #12 from cybercase/feature/postcss_option_as_function
Feature/postcss option as function
2 parents c6f4180 + 54b4bf8 commit e7304c5

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,43 @@ module.exports = {
7070
}
7171
```
7272

73+
## Plugins Function
74+
75+
If you need to link any of the postcss plugins to the webpack context, you can
76+
define a function that returns your plugins. The function is executed with the
77+
same context provided to postcss-loader, allowing access to webpack loader API
78+
79+
```js
80+
var cssimport = require('postcss-import');
81+
var autoprefixer = require('autoprefixer-core');
82+
83+
module.exports = {
84+
module: {
85+
loaders: [
86+
{
87+
test: /\.css$/,
88+
loader: "style-loader!css-loader!postcss-loader"
89+
}
90+
]
91+
},
92+
postcss: function () {
93+
// The context of this function is the same provided to postcss-loader
94+
// see: http://webpack.github.io/docs/loaders.html
95+
96+
return [
97+
cssimport({
98+
// see postcss-import docs to learn about onImport param
99+
// https://github.com/postcss/postcss-import
100+
101+
onImport: function (files) {
102+
files.forEach(this.addDependency);
103+
}.bind(this)
104+
}),
105+
autoprefixer
106+
];
107+
}
108+
}
109+
```
73110

74111
## Safe Mode
75112

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ module.exports = function (source, map) {
1818
if ( params.safe ) opts.safe = true;
1919

2020
var plugins = this.options.postcss;
21+
if ( typeof plugins === 'function' ) {
22+
plugins = plugins.call(this);
23+
}
24+
2125
if ( typeof plugins === 'undefined' ) {
2226
plugins = [];
2327
} else if ( params.pack ) {

test/webpack.config.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ module.exports = {
1111
path: path.join(__dirname, '..', 'build'),
1212
filename: 'test.js'
1313
},
14-
postcss: {
15-
defaults: [blue, red],
16-
blues: [blue]
14+
postcss: function () {
15+
return {
16+
defaults: [blue, red],
17+
blues: [blue]
18+
};
1719
}
1820
};

0 commit comments

Comments
 (0)