-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
124 lines (122 loc) · 3.95 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
const path = require('path');
const webpack = require('webpack');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
entry: {
// 主文件入口
main: './src/index.js',
// vendors.js用于引入依赖包,将第三方依赖打包到vendors中
vendors: './src/vendors.js'
},
output: {
// webpack-dev-server在开发环境时读取静态文件包的路径
publicPath: '/dist/',
// 用于存储打包后文件路径
path: path.resolve(__dirname, 'dist'),
filename: '[name].js'
},
module: {
rules: [{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: 'css-loader'
})
}, {
test: /\.(png|svg|jpg|gif)$/,
use: [
'file-loader'
]
}, {
test: /\.(eot|svg|ttf|woff|woff2)$/,
loader: 'file-loader',
}, {
test: /\.vue$/,
use: [
{
loader: 'vue-loader',
options: {
loaders: {
less: ExtractTextPlugin.extract({
use: ['css-loader?minimize', 'autoprefixer-loader', 'less-loader'],
fallback: 'vue-style-loader'
}),
css: ExtractTextPlugin.extract({
use: ['css-loader', 'autoprefixer-loader', 'less-loader'],
fallback: 'vue-style-loader'
})
}
}
},
{
loader: 'iview-loader',
options: {
prefix: true
}
}
]
}, {
test: /iview\/.*?js$/,
loader: 'babel-loader'
}, {
test: /\.(html|tpl)$/,
loader: 'html-loader'
}],
// 对所有出去node_modules以外的js都使用babel-loader解析
loaders: [{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
}]
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
// 将样式文件单独抽出,打包至style.css中使用
new ExtractTextPlugin({
filename: 'style.css',
allChunks: true
})
],
resolve: {
alias: {
// 为文件配置快速访问入口
vue: 'vue/dist/vue.js',
'src': path.resolve(__dirname, './src'),
'js': path.resolve(__dirname, './src/js'),
'api': path.resolve(__dirname, './src/js/api'),
'pages': path.resolve(__dirname, './src/js/pages'),
'component': path.resolve(__dirname, './src/js/component'),
'image': path.resolve(__dirname, './src/iamge'),
// vue: 'vue/dist/vue.runtime.js'
}
},
devServer: {
port: 8887,
historyApiFallback: true,
inline: true,
hot: true
},
devtool: 'cheap-module-source-map'
}
// 如果为生产环境,则进行代码压缩
if (process.env.NODE_ENV === 'production') {
module.exports.plugins = (module.exports.plugins || []).concat([
// 在编译时配置全局变量NODE_ENV
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
// 使用UglifyJS压缩代码
new UglifyJSPlugin({
uglifyOptions: {
warnings: false
}
}),
// 根据模块的引用次数设置模块ids,使得ids可预测减小总文件大小
new webpack.optimize.OccurrenceOrderPlugin()
])
}