forked from remmlqw/exam-teacher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.production.config.js
131 lines (121 loc) · 3.71 KB
/
webpack.production.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
var path = require('path')
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var autoprefixer = require('autoprefixer');
var OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
module.exports = {
entry: {
app: path.resolve(__dirname, './src/index.js'),
// 将 第三方依赖 单独打包
vendor: [
'react',
'react-dom',
'antd',
'axios',
'prop-types',
'react-mixin',
'react-redux',
'react-responsive',
'react-router-dom',
'redux'
]
},
output: {
path: __dirname + "/dist",
filename: "js/[name].[chunkhash:8].js",
publicPath: './'
},
resolve: {
//自动扩展文件后缀名,意味着我们require模块可以省略不写后缀名
extensions: ['*', '.js', '.json', '.less','.jsx'],
//模块别名定义,方便后续直接引用别名,无须多写长长的地址
alias: {
'@components': path.resolve(__dirname,'src/components'),
'@assets':path.resolve(__dirname,'src/assets')
}
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: ['es2015', 'react']
}
}
}, {
test: /\.less$/,
exclude: /node_modules/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ['css-loader', 'postcss-loader', 'less-loader']
})
}, {
test: /\.css$/,
// exclude: /node_modules/, 删掉次行 不然打包会报错 因为antd.css 在node_modules中
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ['css-loader', 'postcss-loader']
})
}, {
test: /\.(png|gif|jpg|jpeg|bmp)$/i,
use: {
loader: 'url-loader',
options: {
limit: '8192',
outputPath: 'images/',
publicPath : '/'
}
}
}, {
test: /\.(woff|woff2|svg|ttf|eot)($|\?)/i,
use: {
loader: 'url-loader',
options: {
limit: '8192',
outputPath: 'font/'
}
}
}
]
},
plugins: [
// webpack 内置的 banner-plugin
new webpack.BannerPlugin("Copyright by [email protected]"),
// html 模板插件
new HtmlWebpackPlugin({
template: __dirname + '/index.html'
}),
// 定义为生产环境,编译 React 时压缩到最小
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify(process.env.NODE_ENV)
}
}),
// 为组件分配ID,通过这个插件webpack可以分析和优先考虑使用最多的模块,并为它们分配最小的ID
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin({
compress: {
//supresses warnings, usually from module minification
warnings: false
}
}),
// 分离CSS和JS文件
new ExtractTextPlugin('css/[name].[chunkhash:8].css'),
// 提供公共代码
new webpack.optimize.CommonsChunkPlugin({name: 'vendor', filename: 'js/[name].[chunkhash:8].js'}),
// 可在业务 js 代码中使用 __DEV__ 判断是否是dev模式(dev模式下可以提示错误、测试报告等, production模式不提示)
new webpack.DefinePlugin({
__DEV__: JSON.stringify(JSON.parse((process.env.NODE_ENV == 'dev') || 'false'))
}),
new OptimizeCssAssetsPlugin({
assetNameRegExp: /\.css$/g,
cssProcessor: require('cssnano'),
cssProcessorOptions: { discardComments: {removeAll: true } },
canPrint: true
})
]
}