@@ -195,21 +195,44 @@ class FastCSSSplitWebpackPlugin {
195
195
* @returns {void }
196
196
*/
197
197
apply ( compiler ) {
198
- if ( this . options . defer ) {
199
- // Run on `emit` when user specifies the compiler phase
200
- // Due to the incorrect css split + optimization behavior
201
- // Expected: css split should happen after optimization
202
- compiler . plugin ( 'emit' , ( compilation , done ) => {
203
- return this . chunksMapping ( compilation , compilation . chunks , done )
204
- } )
198
+ // for webpack 4
199
+ if ( compiler . hooks ) {
200
+ const plugin = {
201
+ name : 'FastCssSplitPlugin'
202
+ }
203
+
204
+ if ( this . options . defer ) {
205
+ // Run on `emit` when user specifies the compiler phase
206
+ // Due to the incorrect css split + optimization behavior
207
+ // Expected: css split should happen after optimization
208
+ compiler . hooks . emit . tapAsync ( plugin , ( compilation , done ) => {
209
+ this . chunksMapping ( compilation , compilation . chunks , done )
210
+ } )
211
+ } else {
212
+ // use compilation instead of this-compilation, just like other plugins do
213
+ compiler . hooks . compilation . tap ( plugin , compilation => {
214
+ compilation . hooks . optimizeChunkAssets . tapAsync ( plugin , ( chunks , done ) => {
215
+ console . log ( '----- fast-css-split-plugin' )
216
+ this . chunksMapping ( compilation , chunks , done )
217
+ } )
218
+ } )
219
+ }
205
220
} else {
206
- // Only run on `this-compilation` to avoid injecting the plugin into
207
- // sub-compilers as happens when using the `extract-text-webpack-plugin`.
208
- compiler . plugin ( 'this-compilation' , ( compilation ) => {
209
- compilation . plugin ( 'optimize-chunk-assets' , ( chunks , done ) => {
210
- return this . chunksMapping ( compilation , chunks , done )
221
+ if ( this . options . defer ) {
222
+ // Run on `emit` when user specifies the compiler phase
223
+ // Due to the incorrect css split + optimization behavior
224
+ // Expected: css split should happen after optimization
225
+ compiler . plugin ( 'emit' , ( compilation , done ) => {
226
+ return this . chunksMapping ( compilation , compilation . chunks , done )
211
227
} )
212
- } )
228
+ } else {
229
+ // use compilation instead of this-compilation, just like other plugins do
230
+ compiler . plugin ( 'compilation' , ( compilation ) => {
231
+ compilation . plugin ( 'optimize-chunk-assets' , ( chunks , done ) => {
232
+ return this . chunksMapping ( compilation , chunks , done )
233
+ } )
234
+ } )
235
+ }
213
236
}
214
237
}
215
238
}
0 commit comments