Skip to content

Commit e4bc45a

Browse files
committed
fix: fix css url
2 parents d13e6c8 + 9f501cb commit e4bc45a

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

lib/index.js

+22-24
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ const path = require('path')
99
const { RawSource } = require('webpack-sources')
1010
const loaderUtils = require('loader-utils')
1111
const cssReplace = require('./css-replace')
12-
const webpack = require('webpack')
1312

14-
const webpackMajorVersion = Number(webpack.version.split('.')[0])
1513
const isCSS = (name) => /\.css$/.test(name)
1614
// const strip = (str) => str.replace(/\/$/, '')
1715

@@ -64,30 +62,30 @@ class CssUrlRelativePlugin {
6462
done()
6563
}
6664

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-
8865
apply (compiler) {
8966
// 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+
})
9189
}
9290
}
9391

0 commit comments

Comments
 (0)