Skip to content

Commit eb03d39

Browse files
committed
feat: support webpack 4
1 parent aeffd05 commit eb03d39

File tree

1 file changed

+36
-13
lines changed

1 file changed

+36
-13
lines changed

lib/index.js

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -195,21 +195,44 @@ class FastCSSSplitWebpackPlugin {
195195
* @returns {void}
196196
*/
197197
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+
}
205220
} 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)
211227
})
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+
}
213236
}
214237
}
215238
}

0 commit comments

Comments
 (0)