11// config that are specific to --target app
2+ const fs = require ( 'fs' )
3+ const path = require ( 'path' )
24
35module . exports = ( api , options ) => {
46 api . chainWebpack ( webpackConfig => {
@@ -7,9 +9,16 @@ module.exports = (api, options) => {
79 return
810 }
911
12+ const isProd = process . env . NODE_ENV === 'production'
13+
1014 // HTML plugin
1115 const resolveClientEnv = require ( '../util/resolveClientEnv' )
16+ const htmlPath = api . resolve ( 'public/index.html' )
1217 const htmlOptions = {
18+ // use default index.html
19+ template : fs . existsSync ( htmlPath )
20+ ? htmlPath
21+ : path . resolve ( __dirname , 'index-default.html' ) ,
1322 templateParameters : ( compilation , assets , pluginOptions ) => {
1423 // enhance html-webpack-plugin's built in template params
1524 let stats
@@ -27,10 +36,19 @@ module.exports = (api, options) => {
2736 } , resolveClientEnv ( options . baseUrl , true /* raw */ ) )
2837 }
2938 }
30- // only set template path if index.html exists
31- const htmlPath = api . resolve ( 'public/index.html' )
32- if ( require ( 'fs' ) . existsSync ( htmlPath ) ) {
33- htmlOptions . template = htmlPath
39+
40+ if ( isProd ) {
41+ Object . assign ( htmlOptions , {
42+ minify : {
43+ removeComments : true ,
44+ collapseWhitespace : true ,
45+ removeAttributeQuotes : true
46+ // more options:
47+ // https://github.com/kangax/html-minifier#options-quick-reference
48+ } ,
49+ // necessary to consistently work with multiple chunks via CommonsChunkPlugin
50+ chunksSortMode : 'dependency'
51+ } )
3452 }
3553
3654 webpackConfig
@@ -55,31 +73,18 @@ module.exports = (api, options) => {
5573 } ] )
5674
5775 // copy static assets in public/
58- webpackConfig
59- . plugin ( 'copy' )
60- . use ( require ( 'copy-webpack-plugin' ) , [ [ {
61- from : api . resolve ( 'public' ) ,
62- to : api . resolve ( options . outputDir ) ,
63- ignore : [ 'index.html' , '.DS_Store' ]
64- } ] ] )
65-
66- if ( process . env . NODE_ENV === 'production' ) {
67- // minify HTML
76+ if ( fs . existsSync ( api . resolve ( 'public' ) ) ) {
6877 webpackConfig
69- . plugin ( 'html' )
70- . tap ( ( [ options ] ) => [ Object . assign ( options , {
71- minify : {
72- removeComments : true ,
73- collapseWhitespace : true ,
74- removeAttributeQuotes : true
75- // more options:
76- // https://github.com/kangax/html-minifier#options-quick-reference
77- } ,
78- // necessary to consistently work with multiple chunks via CommonsChunkPlugin
79- chunksSortMode : 'dependency'
80- } ) ] )
78+ . plugin ( 'copy' )
79+ . use ( require ( 'copy-webpack-plugin' ) , [ [ {
80+ from : api . resolve ( 'public' ) ,
81+ to : api . resolve ( options . outputDir ) ,
82+ ignore : [ 'index.html' , '.DS_Store' ]
83+ } ] ] )
84+ }
8185
82- // code splitting
86+ // code splitting
87+ if ( isProd ) {
8388 webpackConfig
8489 . optimization . splitChunks ( {
8590 chunks : 'all'
0 commit comments