@@ -92,11 +92,20 @@ module.exports = (api, options) => {
92
92
}
93
93
}
94
94
95
- if ( options . indexPath ) {
96
- htmlOptions . filename = ensureRelative ( outputDir , options . indexPath )
97
- }
98
-
99
95
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
+
100
109
Object . assign ( htmlOptions , {
101
110
minify : {
102
111
removeComments : true ,
@@ -138,6 +147,7 @@ module.exports = (api, options) => {
138
147
const multiPageConfig = options . pages
139
148
const htmlPath = api . resolve ( 'public/index.html' )
140
149
const defaultHtmlPath = path . resolve ( __dirname , 'index-default.html' )
150
+ const publicCopyIgnore = [ 'index.html' , '.DS_Store' ]
141
151
142
152
if ( ! multiPageConfig ) {
143
153
// default, single page setup.
@@ -184,10 +194,21 @@ module.exports = (api, options) => {
184
194
// inject entry
185
195
webpackConfig . entry ( name ) . add ( api . resolve ( entry ) )
186
196
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
+
187
208
// inject html plugin for the page
188
209
const pageHtmlOptions = Object . assign ( { } , htmlOptions , {
189
210
chunks : chunks || [ 'chunk-vendors' , 'chunk-common' , name ] ,
190
- template : fs . existsSync ( template ) ? template : ( fs . existsSync ( htmlPath ) ? htmlPath : defaultHtmlPath ) ,
211
+ template : templatePath ,
191
212
filename : ensureRelative ( outputDir , filename ) ,
192
213
title
193
214
} )
@@ -237,7 +258,7 @@ module.exports = (api, options) => {
237
258
. use ( require ( 'copy-webpack-plugin' ) , [ [ {
238
259
from : publicDir ,
239
260
to : outputDir ,
240
- ignore : [ 'index.html' , '.DS_Store' ]
261
+ ignore : publicCopyIgnore
241
262
} ] ] )
242
263
}
243
264
} )
0 commit comments