1- const { join, resolve } = require ( 'path' ) ;
2- const webpack = require ( 'webpack' ) ;
3- const glob = require ( 'glob' ) ;
1+ const { join, resolve } = require ( 'path' )
2+ const webpack = require ( 'webpack' )
3+ const glob = require ( 'glob' )
44
5- const HtmlWebpackPlugin = require ( 'html-webpack-plugin' ) ;
6- const ExtractTextPlugin = require ( 'extract-text-webpack-plugin' ) ;
7- const CommonsChunkPlugin = require ( 'webpack/lib/optimize/CommonsChunkPlugin' ) ;
5+ const HtmlWebpackPlugin = require ( 'html-webpack-plugin' )
6+ const ExtractTextPlugin = require ( 'extract-text-webpack-plugin' )
7+ const CommonsChunkPlugin = require ( 'webpack/lib/optimize/CommonsChunkPlugin' )
88
9- let entries = { } ;
10- let chunks = [ ] ;
11- getEntriesAndChunks ( ) ;
9+ const entries = { }
10+ const chunks = [ ]
11+ glob . sync ( './src/pages/**/*.js' ) . forEach ( function ( name ) {
12+ const n = name . slice ( name . lastIndexOf ( 'src/' ) + 10 , name . length - 3 )
13+ entries [ n ] = [ name ]
14+ chunks . push ( n )
15+ } )
1216
13- let config = {
17+ const config = {
1418 entry : entries ,
1519 output : {
1620 path : resolve ( __dirname , './dist' ) ,
1721 filename : '[name].js' ,
1822 publicPath : '/'
1923 } ,
2024 resolve : {
21- //配置别名,在项目中可缩减引用路径
25+ // 配置别名,在项目中可缩减引用路径
2226 extensions : [ '.js' , '.vue' ] ,
2327 alias : {
24- assets : join ( __dirname , '/src/assets' ) ,
25- components : join ( __dirname , '/src/components' ) ,
28+ assets : join ( __dirname , '/src/assets' ) ,
29+ components : join ( __dirname , '/src/components' ) ,
2630 root : join ( __dirname , 'node_modules' )
2731 }
2832 } ,
@@ -87,53 +91,36 @@ let config = {
8791 '/api' : {
8892 target : 'http://127.0.0.1:8080' ,
8993 changeOrigin : true ,
90- pathRewrite : { '^/api' : '' }
94+ pathRewrite : { '^/api' : '' }
9195 }
92- } ,
96+ }
9397 } ,
9498 devtool : '#eval-source-map'
95- } ;
99+ }
96100
97- const pages = getHtmls ( ) ;
98- pages . forEach ( function ( pathname ) {
101+ glob . sync ( './src/ pages/**/*.html' ) . forEach ( function ( name ) {
102+ const pathname = name . slice ( name . lastIndexOf ( 'src/' ) + 4 , name . length - 5 )
99103 // filename 用文件夹名字
100- var conf = {
101- filename : pathname . substring ( 6 , pathname . length - 4 ) + '.html' , //生成的html存放路径,相对于path
102- template : 'src/' + pathname + '.html' , //html模板路径
103- } ;
104- var chunk = pathname . substring ( 6 , pathname . length ) ;
104+ const conf = {
105+ filename : pathname . substring ( 6 , pathname . length - 4 ) + '.html' , // 生成的html存放路径,相对于path
106+ template : 'src/' + pathname + '.html' // html模板路径
107+ }
108+ const chunk = pathname . substring ( 6 , pathname . length )
105109 if ( chunks . indexOf ( chunk ) > - 1 ) {
106- conf . inject = 'body' ;
107- conf . chunks = [ 'vendors' , chunk ] ;
110+ conf . inject = 'body'
111+ conf . chunks = [ 'vendors' , chunk ]
108112 }
109113 if ( process . env . NODE_ENV === 'production' ) {
110- conf . hash = true ;
114+ conf . hash = true
111115 }
112- conf . favicon = './src/assets/img/logo.png' ;
113- config . plugins . push ( new HtmlWebpackPlugin ( conf ) ) ;
114- } ) ;
115-
116- module . exports = config ;
116+ conf . favicon = './src/assets/img/logo.png'
117+ config . plugins . push ( new HtmlWebpackPlugin ( conf ) )
118+ } )
117119
118- function getEntriesAndChunks ( ) {
119- glob . sync ( './src/pages/**/*.js' ) . forEach ( function ( name ) {
120- var n = name . slice ( name . lastIndexOf ( 'src/' ) + 10 , name . length - 3 ) ;
121- entries [ n ] = [ name ] ;
122- chunks . push ( n ) ;
123- } ) ;
124- }
125-
126- function getHtmls ( ) {
127- var htmls = [ ] ;
128- glob . sync ( './src/pages/**/*.html' ) . forEach ( function ( name ) {
129- var n = name . slice ( name . lastIndexOf ( 'src/' ) + 4 , name . length - 5 ) ;
130- htmls . push ( n ) ;
131- } ) ;
132- return htmls ;
133- }
120+ module . exports = config
134121
135122if ( process . env . NODE_ENV === 'production' ) {
136- module . exports . devtool = '#source-map' ;
123+ module . exports . devtool = '#source-map'
137124 // http://vue-loader.vuejs.org/en/workflow/production.html
138125 module . exports . plugins = ( module . exports . plugins || [ ] ) . concat ( [
139126 new webpack . DefinePlugin ( {
@@ -146,5 +133,5 @@ if (process.env.NODE_ENV === 'production') {
146133 warnings : false
147134 }
148135 } )
149- ] ) ;
136+ ] )
150137}
0 commit comments