11const path = require ( 'path' ) ;
22const UglifyJsPlugin = require ( 'uglifyjs-webpack-plugin' ) ;
3-
3+ const PrerenderSPAPlugin = require ( 'prerender-spa-plugin' ) ;
44const pkg = require ( './package.json' ) ;
55
66const prod = process . env . NODE_ENV === 'production' ;
7+ const cache = process . env . CACHE === 'true' ;
78const buildForGitPage = process . env . GIT_PAGE ;
89
10+ function resolve ( dir ) {
11+ return path . join ( __dirname , dir ) ;
12+ }
13+
14+ const splitChunks = {
15+ chunks : 'all' ,
16+ cacheGroups : {
17+ libs : {
18+ name : 'chunkLibs' ,
19+ test : / [ \\ / ] n o d e _ m o d u l e s [ \\ / ] / ,
20+ priority : 10 ,
21+ chunks : 'initial' ,
22+ } ,
23+ elementUI : {
24+ name : 'chunkElementUI' ,
25+ priority : 20 ,
26+ test : / [ \\ / ] n o d e _ m o d u l e s [ \\ / ] e l e m e n t - u i [ \\ / ] / ,
27+ } ,
28+ } ,
29+ } ;
30+
931module . exports = {
1032 parallel : true ,
1133 publicPath : buildForGitPage ? '/x-chart/' : '/' ,
1234 runtimeCompiler : true ,
1335 configureWebpack : {
1436 optimization : {
37+ splitChunks,
1538 minimizer : prod
1639 ? [
1740 new UglifyJsPlugin ( {
@@ -24,32 +47,39 @@ module.exports = {
2447 ]
2548 : [ ] ,
2649 } ,
50+ plugins : [
51+ new PrerenderSPAPlugin ( {
52+ staticDir : resolve ( 'dist' ) ,
53+ routes : [ '/' ] ,
54+ } ) ,
55+ ] ,
2756 } ,
2857 chainWebpack : ( config ) => {
2958 // modify html-webpack-html configure
3059 config . plugin ( 'html' ) . tap ( ( options ) => {
3160 options [ 0 ] . title = pkg . name ;
32- options [ 0 ] . template = path . join ( __dirname , 'src/template/index.html' ) ;
33- options [ 0 ] . favicon = path . join ( __dirname , 'public/favicon.ico' ) ;
34- if ( prod ) {
61+ options [ 0 ] . template = resolve ( 'src/template/index.html' ) ;
62+ options [ 0 ] . favicon = resolve ( 'public/favicon.ico' ) ;
63+
64+ // only set cache = true and prod mode will use cache template
65+ if ( prod && cache ) {
3566 options [ 0 ] . inject = false ;
36- options [ 0 ] . template = path . join (
37- __dirname ,
38- 'src/template/index.cache.html' ,
39- ) ;
67+ options [ 0 ] . template = resolve ( 'src/template/index.cache.html' ) ;
4068 options [ 0 ] . gitPage = buildForGitPage ;
4169 }
4270 return options ;
4371 } ) ;
4472
4573 // modify url-loader for fonts
46- config . module . rule ( 'fonts' ) . use ( 'url-loader' ) . loader ( 'url-loader' ) . tap ( ( options ) => {
47- options . limit = 10000 ;
48- // modify publicPath for git-pages
49- options . publicPath = buildForGitPage
50- ? '/x-chart/'
51- : '/' ;
52- return options ;
53- } ) ;
74+ config . module
75+ . rule ( 'fonts' )
76+ . use ( 'url-loader' )
77+ . loader ( 'url-loader' )
78+ . tap ( ( options ) => {
79+ options . limit = 10000 ;
80+ // modify publicPath for git-pages
81+ options . publicPath = buildForGitPage ? '/x-chart/' : '/' ;
82+ return options ;
83+ } ) ;
5484 } ,
5585} ;
0 commit comments