@@ -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 } )
0 commit comments