@@ -33,7 +33,6 @@ module.exports = (api, options) => {
3333 const isProduction = process . env . NODE_ENV === 'production'
3434
3535 const url = require ( 'url' )
36- const path = require ( 'path' )
3736 const chalk = require ( 'chalk' )
3837 const webpack = require ( 'webpack' )
3938 const WebpackDevServer = require ( 'webpack-dev-server' )
@@ -139,9 +138,7 @@ module.exports = (api, options) => {
139138 clientLogLevel : 'none' ,
140139 historyApiFallback : {
141140 disableDotRule : true ,
142- rewrites : [
143- { from : / ./ , to : path . posix . join ( options . publicPath , 'index.html' ) }
144- ]
141+ rewrites : genHistoryApiFallbackRewrites ( options . publicPath , options . pages )
145142 } ,
146143 contentBase : api . resolve ( 'public' ) ,
147144 watchContentBase : ! isProduction ,
@@ -302,6 +299,23 @@ function checkInContainer () {
302299 }
303300}
304301
302+ function genHistoryApiFallbackRewrites ( baseUrl , pages = { } ) {
303+ const path = require ( 'path' )
304+ const multiPageRewrites = Object
305+ . keys ( pages )
306+ // sort by length in reversed order to avoid overrides
307+ // eg. 'page11' should appear in front of 'page1'
308+ . sort ( ( a , b ) => b . length - a . length )
309+ . map ( name => ( {
310+ from : new RegExp ( `^/${ name } ` ) ,
311+ to : path . posix . join ( baseUrl , pages [ name ] . filename || `${ name } .html` )
312+ } ) )
313+ return [
314+ ...multiPageRewrites ,
315+ { from : / ./ , to : path . posix . join ( baseUrl , 'index.html' ) }
316+ ]
317+ }
318+
305319module . exports . defaultModes = {
306320 serve : 'development'
307321}
0 commit comments