@@ -9,9 +9,7 @@ const path = require('path')
9
9
const { RawSource } = require ( 'webpack-sources' )
10
10
const loaderUtils = require ( 'loader-utils' )
11
11
const cssReplace = require ( './css-replace' )
12
- const webpack = require ( 'webpack' )
13
12
14
- const webpackMajorVersion = Number ( webpack . version . split ( '.' ) [ 0 ] )
15
13
const isCSS = ( name ) => / \. c s s $ / . test ( name )
16
14
// const strip = (str) => str.replace(/\/$/, '')
17
15
@@ -64,30 +62,30 @@ class CssUrlRelativePlugin {
64
62
done ( )
65
63
}
66
64
67
- applyCompilation ( compilation ) {
68
- // webpack version <= 4
69
- if ( webpackMajorVersion <= 4 ) {
70
- compilation . hooks . optimizeChunkAssets . tapAsync ( this . plugin , ( chunks , done ) => {
71
- this . fixCssUrl ( compilation , chunks , done )
72
- } )
73
- return
74
- }
75
-
76
- // webpack verion = 5
77
- compilation . hooks . processAssets . tapAsync (
78
- {
79
- ...this . plugin ,
80
- stage : compilation . PROCESS_ASSETS_STAGE_SUMMARIZE
81
- } ,
82
- ( compilationAssets , done ) => {
83
- this . fixCssUrl ( compilation , compilation . chunks , done )
84
- }
85
- )
86
- }
87
-
88
65
apply ( compiler ) {
89
66
// use compilation instead of this-compilation, just like other plugins do
90
- compiler . hooks . compilation . tap ( this . plugin , this . applyCompilation . bind ( this ) )
67
+ compiler . hooks . compilation . tap ( this . plugin , ( compilation ) => {
68
+ // webpack5 新加的 hook
69
+ const isWebpack5 = compilation . hooks . processAssets !== undefined
70
+ // webpack version <= 4
71
+ if ( ! isWebpack5 ) {
72
+ compilation . hooks . optimizeChunkAssets . tapAsync ( this . plugin , ( chunks , done ) => {
73
+ this . fixCssUrl ( compilation , chunks , done )
74
+ } )
75
+ return
76
+ }
77
+
78
+ // webpack verion = 5
79
+ compilation . hooks . processAssets . tapAsync (
80
+ {
81
+ ...this . plugin ,
82
+ stage : compilation . PROCESS_ASSETS_STAGE_SUMMARIZE
83
+ } ,
84
+ ( compilationAssets , done ) => {
85
+ this . fixCssUrl ( compilation , compilation . chunks , done )
86
+ }
87
+ )
88
+ } )
91
89
}
92
90
}
93
91
0 commit comments