-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
192 lines (185 loc) · 4.91 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const { join, resolve } = require('path')
const webpack = require('webpack')
const glob = require('glob')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const CommonsChunkPlugin = require('webpack/lib/optimize/CommonsChunkPlugin')
const extractCSS = new ExtractTextPlugin({
filename: 'assets/css/[name].css',
allChunks: true
})
const extractLESS = new ExtractTextPlugin({
filename: 'assets/css/[name].css',
allChunks: true
})
const entries = {}
const chunks = []
glob.sync('./src/pages/**/app.js').forEach(path => {
const chunk = path.split('./src/pages/')[1].split('/app.js')[0]
entries[chunk] = path
chunks.push(chunk)
})
const config = {
entry: entries,
output: {
path: resolve(__dirname, './dist'),
filename: 'assets/js/[name].js',
publicPath: '/'
},
resolve: {
extensions: ['.js', '.vue'],
alias: {
assets: join(__dirname, '/src/assets'),
components: join(__dirname, '/src/components'),
root: join(__dirname, 'node_modules')
}
},
module: {
rules: [
{
test: /\.vue$/,
use: [
'cache-loader',
{
loader: 'vue-loader',
options: {
loaders: {
css: ExtractTextPlugin.extract({
use: ['cache-loader', 'css-loader'],
fallback: 'style-loader'
}),
less: ExtractTextPlugin.extract({
use: ['cache-loader', 'css-loader', 'postcss-loader', 'less-loader'],
fallback: 'style-loader'
}),
postcss: ExtractTextPlugin.extract({
use: ['cache-loader', 'css-loader', 'postcss-loader'],
fallback: 'style-loader'
})
}
}
}
]
},
{
test: /\.js$/,
use: [
'cache-loader',
'babel-loader'
],
include: resolve('src'),
exclude: /node_modules/
},
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
use: ['cache-loader', 'css-loader', 'postcss-loader'],
fallback: 'style-loader'
})
},
{
test: /\.less$/,
use: ExtractTextPlugin.extract({
use: ['cache-loader', 'css-loader', 'postcss-loader', 'less-loader'],
fallback: 'style-loader'
})
},
{
test: /\.html$/,
use: [
'cache-loader',
{
loader: 'html-loader',
options: {
root: resolve(__dirname, 'src'),
attrs: ['img:src', 'link:href']
}
}
]
},
{
test: /\.(eot|ttf|woff|woff2|svg|svgz)(\?.+)?$/,
exclude: /favicon\.png$/,
use: [
'cache-loader',
{
loader: 'url-loader',
options: {
limit: 10000,
name:'fonts/[name].[md5:hash:hex:7].[ext]'
}
}
]
},
{
test: /\.(png|jpg|jpeg|gif)(\?.+)?$/,
exclude: /favicon\.png$/,
use: [
'cache-loader',
{
loader: 'url-loader',
options: {
limit: 10000,
name:'img/[name].[md5:hash:hex:7].[ext]'
}
}
]
}
]
},
plugins: [
new CommonsChunkPlugin({
name: 'vendors',
filename: 'assets/js/vendors.js',
chunks: chunks,
minChunks: chunks.length
}),
extractLESS,
extractCSS
],
devServer: {
host: 'localhost',//实际开发host建议用自己的IP,方便同事访问。用localhost的话其他电脑无法访问,只能用localhost访问。
port: 8080,
historyApiFallback: false,
noInfo: true,
disableHostCheck: true,
proxy: {
'/api': {
target: 'http://127.0.0.1:8080',//后台服务器的IP地址,用代理可以解决跨域问题。
changeOrigin: true,
pathRewrite: { '^/api': '' }
}
}
},
devtool: '#eval-source-map'
}
glob.sync('./src/pages/**/*.html').forEach(path => {
const chunk = path.split('./src/pages/')[1].split('/app.html')[0]
const filename = chunk + '.html'
const htmlConf = {
filename: filename,
template: path,
inject: 'body',
favicon: './src/assets/img/logo.jpg',
hash: process.env.NODE_ENV === 'production',
chunks: ['vendors', chunk]
}
config.plugins.push(new HtmlWebpackPlugin(htmlConf))
})
module.exports = config
if (process.env.NODE_ENV === 'production') {
module.exports.devtool = '#source-map'
// http://vue-loader.vuejs.org/en/workflow/production.html
module.exports.plugins = (module.exports.plugins || []).concat([
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
})
])
}