Skip to content

Commit a1023ad

Browse files
committed
fix: fix webpack 4 compatibility of HtmlPwaPlugin
1 parent c3284f6 commit a1023ad

File tree

1 file changed

+38
-25
lines changed

1 file changed

+38
-25
lines changed

packages/@vue/cli-plugin-pwa/lib/HtmlPwaPlugin.js

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const HtmlWebpackPlugin = require('html-webpack-plugin')
2+
const { semver } = require('@vue/cli-shared-utils')
23

34
const ID = 'vue-cli:pwa-html-plugin'
45

@@ -183,31 +184,43 @@ module.exports = class HtmlPwaPlugin {
183184
})
184185

185186
if (!isHrefAbsoluteUrl(this.options.manifestPath)) {
186-
compiler.hooks.compilation.tap(ID, compilation => {
187-
compilation.hooks.processAssets.tap(
188-
{ name: ID, stage: 'PROCESS_ASSETS_STAGE_ADDITIONS' },
189-
assets => {
190-
const {
191-
name,
192-
themeColor,
193-
manifestPath,
194-
manifestOptions
195-
} = this.options
196-
const publicOptions = {
197-
name,
198-
short_name: name,
199-
theme_color: themeColor
200-
}
201-
const outputManifest = JSON.stringify(
202-
Object.assign(publicOptions, defaultManifest, manifestOptions)
203-
)
204-
assets[manifestPath] = {
205-
source: () => outputManifest,
206-
size: () => outputManifest.length
207-
}
208-
}
209-
)
210-
})
187+
const {
188+
name,
189+
themeColor,
190+
manifestPath,
191+
manifestOptions
192+
} = this.options
193+
const publicOptions = {
194+
name,
195+
short_name: name,
196+
theme_color: themeColor
197+
}
198+
const outputManifest = JSON.stringify(
199+
Object.assign(publicOptions, defaultManifest, manifestOptions)
200+
)
201+
const manifestAsset = {
202+
source: () => outputManifest,
203+
size: () => outputManifest.length
204+
}
205+
206+
let webpackMajor = 4
207+
if (compiler.webpack) {
208+
webpackMajor = semver.major(compiler.webpack.version)
209+
}
210+
211+
if (webpackMajor === 4) {
212+
compiler.hooks.emit.tapAsync(ID, (data, cb) => {
213+
data.assets[manifestPath] = manifestAsset
214+
cb(null, data)
215+
})
216+
} else {
217+
compiler.hooks.compilation.tap(ID, compilation => {
218+
compilation.hooks.processAssets.tap(
219+
{ name: ID, stage: 'PROCESS_ASSETS_STAGE_ADDITIONS' },
220+
assets => { assets[manifestPath] = manifestAsset }
221+
)
222+
})
223+
}
211224
}
212225
}
213226
}

0 commit comments

Comments
 (0)