1
1
// config that are specific to --target app
2
+ const fs = require ( 'fs' )
3
+ const path = require ( 'path' )
2
4
3
5
module . exports = ( api , options ) => {
4
6
api . chainWebpack ( webpackConfig => {
@@ -7,9 +9,16 @@ module.exports = (api, options) => {
7
9
return
8
10
}
9
11
12
+ const isProd = process . env . NODE_ENV === 'production'
13
+
10
14
// HTML plugin
11
15
const resolveClientEnv = require ( '../util/resolveClientEnv' )
16
+ const htmlPath = api . resolve ( 'public/index.html' )
12
17
const htmlOptions = {
18
+ // use default index.html
19
+ template : fs . existsSync ( htmlPath )
20
+ ? htmlPath
21
+ : path . resolve ( __dirname , 'index-default.html' ) ,
13
22
templateParameters : ( compilation , assets , pluginOptions ) => {
14
23
// enhance html-webpack-plugin's built in template params
15
24
let stats
@@ -27,10 +36,19 @@ module.exports = (api, options) => {
27
36
} , resolveClientEnv ( options . baseUrl , true /* raw */ ) )
28
37
}
29
38
}
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
+ } )
34
52
}
35
53
36
54
webpackConfig
@@ -55,31 +73,18 @@ module.exports = (api, options) => {
55
73
} ] )
56
74
57
75
// 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' ) ) ) {
68
77
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
+ }
81
85
82
- // code splitting
86
+ // code splitting
87
+ if ( isProd ) {
83
88
webpackConfig
84
89
. optimization . splitChunks ( {
85
90
chunks : 'all'
0 commit comments