-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathwebpack.config.prod.js
93 lines (89 loc) · 2.78 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
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: ['node_modules'],
// 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({//压缩文件
compress: { warnings: false }
}),
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值改变。
})
],
}