From 6d1716e875eeaadf7f931152451f41bc02926804 Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 8 May 2018 17:12:17 -0400 Subject: [PATCH] fix: pwa plugin compat with webpack 4 --- .../cli-plugin-pwa/__tests__/pwaPlugin.spec.js | 14 +++++++------- packages/@vue/cli-plugin-pwa/index.js | 1 + packages/@vue/cli-plugin-pwa/lib/HtmlPwaPlugin.js | 10 ++++++---- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/packages/@vue/cli-plugin-pwa/__tests__/pwaPlugin.spec.js b/packages/@vue/cli-plugin-pwa/__tests__/pwaPlugin.spec.js index 679825b47b..bad46ce0d0 100644 --- a/packages/@vue/cli-plugin-pwa/__tests__/pwaPlugin.spec.js +++ b/packages/@vue/cli-plugin-pwa/__tests__/pwaPlugin.spec.js @@ -28,17 +28,17 @@ test('pwa', async () => { // Make sure the base preload/prefetch are not affected const index = await project.read('dist/index.html') + // should split and preload app.js & vendor.js - expect(index).toMatch(/]+app[^>]+\.js>/) - expect(index).toMatch(/]+vendor[^>]+\.js>/) - // should not preload manifest because it's inlined - expect(index).not.toMatch(/]+manifest[^>]+\.js>/) - // should inline manifest and webpack runtime - expect(index).toMatch('webpackJsonp') + expect(index).toMatch(/]+js\/app[^>]+\.js rel=preload>/) + expect(index).toMatch(/]+js\/vendors~app[^>]+\.js rel=preload>/) + // should preload css + expect(index).toMatch(/]+app[^>]+\.css rel=preload>/) // PWA specific directives expect(index).toMatch(``) - expect(index).toMatch(``) + // favicon is not minified because it's technically a comment + expect(index).toMatch(``) expect(index).toMatch(``) // should import service worker script diff --git a/packages/@vue/cli-plugin-pwa/index.js b/packages/@vue/cli-plugin-pwa/index.js index 6f76d4004b..f2f418bfac 100644 --- a/packages/@vue/cli-plugin-pwa/index.js +++ b/packages/@vue/cli-plugin-pwa/index.js @@ -10,6 +10,7 @@ module.exports = (api, options) => { .use(require('./lib/HtmlPwaPlugin'), [Object.assign({ name }, userOptions)]) + .after('html') // generate /service-worker.js in production mode if (process.env.NODE_ENV === 'production') { diff --git a/packages/@vue/cli-plugin-pwa/lib/HtmlPwaPlugin.js b/packages/@vue/cli-plugin-pwa/lib/HtmlPwaPlugin.js index 661bc335fb..b54ac84d4a 100644 --- a/packages/@vue/cli-plugin-pwa/lib/HtmlPwaPlugin.js +++ b/packages/@vue/cli-plugin-pwa/lib/HtmlPwaPlugin.js @@ -1,3 +1,5 @@ +const ID = 'vue-cli:pwa-html-plugin' + const defaults = { name: 'PWA app', themeColor: '#4DBA87', // The Vue color @@ -12,14 +14,14 @@ module.exports = class HtmlPwaPlugin { } apply (compiler) { - compiler.plugin('compilation', compilation => { - compilation.plugin('html-webpack-plugin-before-html-processing', (data, cb) => { + compiler.hooks.compilation.tap(ID, compilation => { + compilation.hooks.htmlWebpackPluginBeforeHtmlProcessing.tapAsync(ID, (data, cb) => { // wrap favicon in the base template with IE only comment - data.html = data.html.replace(/]+>/, '') + data.html = data.html.replace(/]+>/, '') cb(null, data) }) - compilation.plugin('html-webpack-plugin-alter-asset-tags', (data, cb) => { + compilation.hooks.htmlWebpackPluginAlterAssetTags.tapAsync(ID, (data, cb) => { const { name, themeColor, msTileColor, appleMobileWebAppCapable, appleMobileWebAppStatusBarStyle } = this.options const { publicPath } = compiler.options.output