forked from caize/backend-by-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.prod.js
101 lines (97 loc) · 3.25 KB
/
webpack.config.prod.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
const path = require('path')
const webpack = require('webpack')
const autoprefixer = require('autoprefixer')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const ExtractTextPlugin = require("extract-text-webpack-plugin")
const themeConfig = require('./src/theme')
module.exports = {
entry: {
main: './src/index.js',//入口文件
vendor: [
'react',
'react-dom'
]//分离第三方库
},
output: {
filename: '[name].[chunkhash:5].js',//打包后的文件名
chunkFilename: '[name].[chunkhash:5].js',
path: path.join(__dirname, 'dist'),//打包后的文件存储位置
publicPath: '/'
},
resolve: {
modules: [path.resolve(__dirname, 'node_modules')],//优化webpack文件搜索范围
mainFields: ['jsnext:main', 'main'],//优化支持tree-shaking的库
extensions: ['.web.js', '.jsx', '.js', '.json']
},
//devtool: 'cheap-module-eval-source-map',//生产环境需关闭该功能,否则打包后体积会变大
module: {
rules: [
{
test: /\.js$/,
use: 'babel-loader',//编译js文件的loader,
exclude: /node_modules/
},
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ['css-loader?minimize=true&modules&localIdentName=[local]-[hash:base64:5]', 'postcss-loader']
})
},
{
test: /\.less$/,
include: /node_modules/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ['css-loader?minimize=true', 'postcss-loader', `less-loader?{"modifyVars":${JSON.stringify(themeConfig)}}`]
})
},
{
test: /\.less$/,
exclude: /node_modules/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ['css-loader?minimize=true&modules&localIdentName=[local]-[hash:base64:5]', 'postcss-loader', 'less-loader']
})
},
{
test: /\.(jpe?g|png|gif|mp4|webm|woff|otf|webp|svg)$/,
use: 'url-loader'
}
],
},
plugins: [
new webpack.NoEmitOnErrorsPlugin(),//报错时不退出webpack进程
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production')
}),
new webpack.optimize.UglifyJsPlugin({
beautify: false,//最紧凑的输出
comments: false,//删除注释
compress: {
warnings: false,//在UglifyJs删除没有用到的代码时不输出警告
drop_console: true,//删除所有console语句
collapse_vars: true,//内嵌定义了但是只用到一次的变量
reduce_vars: true,// 提取出出现多次但是没有定义成变量去引用的静态值
}
}),
new HtmlWebpackPlugin({//自动生成html
template: './src/views/index.html'
}),
new ExtractTextPlugin({
filename: (getPath) => {
return getPath('[name].css').replace('dist/js', 'css')
},
allChunks: true
}),//提取css文件
new webpack.LoaderOptionsPlugin({
options: {
postcss: () => [autoprefixer],//css前缀补全以及兼容
url: { limit: 10240 }
}
}),
new webpack.optimize.CommonsChunkPlugin({
names: ['vendor', 'manifest'] // 指定公共 bundle 的名字,加manifest防止vendor的hash值改变。
})
],
}