-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.prod.config.js
121 lines (120 loc) · 2.85 KB
/
webpack.prod.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
const { resolve } = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin')
// 定义环境变量
process.env.NODE_ENV = 'production'
// 复用loader
const commonCssLoader = [
MiniCssExtractPlugin.loader,
'css-loader',
{
loader: 'postcss-loader', // 兼容css
options: {
ident: 'postcss',
plugins: () => [require('postcss-preset-env')()]
}
}
];
module.exports = {
entry: './src/index.js',
output: {
filename: 'js/bundle.js',
path: resolve(__dirname, 'build'),
},
module: {
rules: [
{
test: /.css$/,
use: [...commonCssLoader]
},
{
test: /.less$/,
use: [
...commonCssLoader,
'less-loader'
]
},
{
// eslint
// 在package.js eslintConfig
test: /\.js$/,
exclude: /node_modules/,
// 优先执行
enforce: 'pre',
loader: 'eslint-loader',
options: {
fix: true
}
},
{
// eslint
// 在package.js eslintConfig
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: {
presets: [
[
'@babel/preset-env',
{
useBuiltIns: 'usage',
corejs: { version: 3 },
target: {
chorme: '60',
firefox: '50'
}
}
]
]
}
},
{
// 处理图片
test: /\.(jpg|png|gif)$/,
loader: 'url-loader',
options: {
// 图片大小小于8kb,就会被base64处理
// 优点: 减少请求数量(减轻服务器压力)
// 缺点: 图片体积会更大(文件请求速度更慢)
limit: 10 * 1024,
esModule: false,
// 给图片进行命名
// [hash:10] 取图片前10位
name: '[hash:10].[ext]',
outputPath: 'image'
}
},
{
test: /\.html$/,
// 处理html文件img图片
loader: 'html-loader',
},
// 打包其他资源font
{
exclude: /\.(css|js|html|less|png)$/,
loader: 'file-loader',
options: {
// [hash:10] 取图片前10位
name: '[hash:10].[ext]',
outputPath: 'font'
}
}
]
},
plugins: [
new MiniCssExtractPlugin({
filename: 'css/[name].css'
}),
new OptimizeCssAssetsWebpackPlugin(),
new HtmlWebpackPlugin({
template: './src/view/index.html',
minify: {
// 移除空格
collapseWhitespace: true,
removeComments: true // 移除注释
}
})
],
mode: 'production'
}