Skip to content

Commit 68aaa8f

Browse files
committed
fix(pwa): workaround index sw manifest path when using relative indexPath
close vuejs#2007
1 parent 3bda824 commit 68aaa8f

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

packages/@vue/cli-service/lib/config/app.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,20 @@ module.exports = (api, options) => {
9292
}
9393
}
9494

95-
if (options.indexPath) {
96-
htmlOptions.filename = ensureRelative(outputDir, options.indexPath)
97-
}
98-
9995
if (isProd) {
96+
// handle indexPath
97+
if (options.indexPath) {
98+
// why not set filename for html-webpack-plugin?
99+
// 1. It cannot handle absolute paths
100+
// 2. Relative paths causes incorrect SW manifest to be generated (#2007)
101+
webpackConfig
102+
.plugin('move-index')
103+
.use(require('../webpack/MovePlugin'), [
104+
path.resolve(outputDir, 'index.html'),
105+
path.resolve(outputDir, options.indexPath)
106+
])
107+
}
108+
100109
Object.assign(htmlOptions, {
101110
minify: {
102111
removeComments: true,
@@ -138,6 +147,7 @@ module.exports = (api, options) => {
138147
const multiPageConfig = options.pages
139148
const htmlPath = api.resolve('public/index.html')
140149
const defaultHtmlPath = path.resolve(__dirname, 'index-default.html')
150+
const publicCopyIgnore = ['index.html', '.DS_Store']
141151

142152
if (!multiPageConfig) {
143153
// default, single page setup.
@@ -184,10 +194,21 @@ module.exports = (api, options) => {
184194
// inject entry
185195
webpackConfig.entry(name).add(api.resolve(entry))
186196

197+
// resolve page index template
198+
const hasDedicatedTemplate = fs.existsSync(api.resolve(template))
199+
if (hasDedicatedTemplate) {
200+
publicCopyIgnore.push(template)
201+
}
202+
const templatePath = hasDedicatedTemplate
203+
? template
204+
: fs.existsSync(htmlPath)
205+
? htmlPath
206+
: defaultHtmlPath
207+
187208
// inject html plugin for the page
188209
const pageHtmlOptions = Object.assign({}, htmlOptions, {
189210
chunks: chunks || ['chunk-vendors', 'chunk-common', name],
190-
template: fs.existsSync(template) ? template : (fs.existsSync(htmlPath) ? htmlPath : defaultHtmlPath),
211+
template: templatePath,
191212
filename: ensureRelative(outputDir, filename),
192213
title
193214
})
@@ -237,7 +258,7 @@ module.exports = (api, options) => {
237258
.use(require('copy-webpack-plugin'), [[{
238259
from: publicDir,
239260
to: outputDir,
240-
ignore: ['index.html', '.DS_Store']
261+
ignore: publicCopyIgnore
241262
}]])
242263
}
243264
})
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const fs = require('fs-extra')
2+
3+
module.exports = class MovePlugin {
4+
constructor (from, to) {
5+
this.from = from
6+
this.to = to
7+
}
8+
9+
apply (compiler) {
10+
compiler.hooks.done.tap('move-plugin', () => {
11+
fs.moveSync(this.from, this.to, { overwrite: true })
12+
})
13+
}
14+
}

0 commit comments

Comments
 (0)