Skip to content

Commit 8dc282c

Browse files
authored
Merge pull request #93 from jantimon/feature/postcss-loader-before-processing-event
Add postcss-loader-before-processing event
2 parents 825010b + a570b18 commit 8dc282c

File tree

5 files changed

+49
-1
lines changed

5 files changed

+49
-1
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,15 @@ export default {
290290
}
291291
```
292292

293+
294+
### Webpack Events
295+
296+
Webpack provides webpack-plugin developers a convenient way to hook into the build pipeline.
297+
The postcss-loader makes us of this event system to allow building integrated postcss-webpack tools.
298+
299+
See the [example implementation](https://github.com/postcss/postcss-loader/blob/master/test/webpack-plugins/rewrite.js)
300+
301+
* `postcss-loader-before-processing`
302+
is fired before processing and allows to add or remove postcss plugins
303+
293304
[postcss-js]: https://github.com/postcss/postcss-js

index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ module.exports = function (source, map) {
8585
source = this.exec(source, this.resource);
8686
}
8787

88+
// Allow plugins to add or remove postcss plugins
89+
plugins = this._compilation.applyPluginsWaterfall(
90+
'postcss-loader-before-processing',
91+
[].concat(plugins),
92+
params
93+
);
94+
8895
postcss(plugins).process(source, opts)
8996
.then(function (result) {
9097
result.warnings().forEach(function (msg) {

test/test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ describe('postcss-loader', function () {
1919
expect(css).to.eql('a { color:\n}');
2020
});
2121

22+
it('lets other plugins alter the used plugins', function () {
23+
var css = require('!raw-loader!../?rewrite=true!./cases/style.css');
24+
expect(css).to.eql('a { color: black }\n');
25+
});
26+
2227
it('processes CSS-in-JS', function () {
2328
var css = require('!raw-loader!' +
2429
'../?parser=postcss-js!' +

test/webpack-plugins/rewrite.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function RewritePlugin() {
2+
3+
}
4+
5+
RewritePlugin.prototype.apply = function (compiler) {
6+
function rewrite(postcssPlugins, loaderParams) {
7+
// Just for the demo and test we remove all plugins if
8+
// the 'rewrite' parameter is set
9+
if (loaderParams.rewrite) {
10+
return [];
11+
}
12+
// If the rewrite parameter isn't set we don't change the modules
13+
return postcssPlugins;
14+
}
15+
16+
compiler.plugin('compilation', function (compilation) {
17+
compilation.plugin('postcss-loader-before-processing', rewrite);
18+
});
19+
};
20+
21+
module.exports = RewritePlugin;

test/webpack.config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var path = require('path');
22

33
var blue = require('./plugins/blue');
44
var red = require('./plugins/red');
5+
var RewritePlugin = require('./webpack-plugins/rewrite.js');
56

67
module.exports = {
78
target: 'node',
@@ -16,5 +17,8 @@ module.exports = {
1617
defaults: [blue, red],
1718
blues: [blue]
1819
};
19-
}
20+
},
21+
plugins: [
22+
new RewritePlugin()
23+
]
2024
};

0 commit comments

Comments
 (0)