File tree Expand file tree Collapse file tree 5 files changed +49
-1
lines changed
Expand file tree Collapse file tree 5 files changed +49
-1
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 ) {
Original file line number Diff line number Diff 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!' +
Original file line number Diff line number Diff line change 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 ;
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ var path = require('path');
22
33var blue = require ( './plugins/blue' ) ;
44var red = require ( './plugins/red' ) ;
5+ var RewritePlugin = require ( './webpack-plugins/rewrite.js' ) ;
56
67module . 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} ;
You can’t perform that action at this time.
0 commit comments