-
Notifications
You must be signed in to change notification settings - Fork 29
/
webpack.config.opt.js
277 lines (256 loc) · 7.14 KB
/
webpack.config.opt.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
const path = require('path');
const os = require('os');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const BundlePlugin = require('webpack-bundle-analyzer');
const autoprefixer = require('autoprefixer'); // 自动加前缀的插件
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin');
const BundleAnalyzerPlugin = BundlePlugin.BundleAnalyzerPlugin;
// 判断当前运行环境是开发模式还是生产模式
const nodeEnv = process.env.NODE_ENV || 'development';
const __DEV__ = nodeEnv !== 'production';
console.log('当前运行环境:', __DEV__ ? 'development' : 'production');
// 是否使用preact
const __PREACT__ = false;
const externals = {
axios: 'axios',
react: 'React',
redux: 'Redux',
'react-dom': 'ReactDOM',
'react-redux': 'ReactRedux',
'react-router-dom': 'ReactRouterDOM',
'prop-types': 'PropTypes'
};
module.exports = {
cache: true,
context: path.resolve(__dirname, './src'),
devtool: false, // 生产环境不需要调试sourcemap
entry: {
index: [
'./index.js' // 生产环境只需要app的入口文件
],
vendor: ['history']
},
output: {
filename: 'js/[name].[hash:12].js',
// 输出的打包文件
path: path.join(__dirname, 'dist'),
// 项目输出路径
publicPath: '/dist/', // 生产环境需要制定上线的目录路径
// 从外部拉取资源
libraryTarget: 'umd'
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
use: [
'babel-loader?cacheDirectory'
],
exclude: /^node_modules$/
},
{
test: /\.css$/,
// 生产环境下将css打包成独立文件
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ['css-loader',
{
loader: 'postcss-loader',
options: {
plugins() {
return [
autoprefixer
];
}
}
}
]
}),
exclude: /^node_modules$/
},
{
test: /\.less$/,
// 生产环境下将node_modules less打包成独立文件
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ['css-loader',
{
loader: 'postcss-loader',
options: {
plugins() {
return [
autoprefixer
];
}
}
},
'less-loader'
]
}),
include: /node_modules/
},
{
// 生产环境下将工程中的less打包成独立文件
test: /\.less$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ['css-loader?modules&localIdentName=[path][name]---[local]---[hash:base64:5]',
{
loader: 'postcss-loader',
options: {
plugins() {
return [
autoprefixer
];
}
}
},
'less-loader'
]
}),
exclude: /node_modules/
},
{
test: /favicon\.png$/,
use: [
{
// 使用file-loader
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]'
}
}
],
exclude: /^node_modules$/
},
{
// 匹配.html文件
test: /\.html$/,
use: [
{
loader: 'html-loader',
options: {
attrs: ['img:src', 'link:href']
}
}
],
exclude: /^node_modules$/
},
{
// 处理静态资源
test: /\.(png|jpg|jpeg|gif|eot|ttf|woff|woff2|svg|svgz)(\?.+)?$/,
exclude: /favicon\.png$/,
use: [
{
loader: 'url-loader',
options: {
limit: 8 * 1024
}
}
]
},
{
test: /\.bundle\.js$/,
include: /(src)/,
exclude: /(node_modules|bower_components)/,
use: [{
loader: 'bundle-loader',
options: {
name: 'app-[name]',
lazy: true
}
}, {
loader: 'babel-loader'
}]
}
]
},
resolve: {
extensions: ['.jsx', '.js', '.less', '.json'],
alias: {
actions: path.resolve(__dirname, 'src/actions'),
views: path.resolve(__dirname, 'src/views'),
constant: path.resolve(__dirname, 'src/constant'),
static: path.resolve(__dirname, 'src/static'),
routes: path.resolve(__dirname, 'src/routes'),
components: path.resolve(__dirname, 'src/components'),
containers: path.resolve(__dirname, 'src/containers'),
reducers: path.resolve(__dirname, 'src/reducers'),
api: path.resolve(__dirname, 'src/api'),
utils: path.resolve(__dirname, 'src/utils'),
store: path.resolve(__dirname, 'src/store'),
react: __PREACT__ ? 'preact-compat/dist/preact-compat' : 'react',
'react-dom': __PREACT__ ? 'preact-compat/dist/preact-compat' : 'react-dom',
'create-react-class': __PREACT__ ? 'preact-compat/lib/create-react-class' : 'create-react-class'
}
},
externals: {},
plugins: [
// 添加系统全局变量
new webpack.DefinePlugin({ // 编译成生产版本
'process.env': {
NODE_ENV: JSON.stringify('production')
}
}),
new webpack.optimize.ModuleConcatenationPlugin(),
new webpack.optimize.CommonsChunkPlugin({
names: 'vendor',
minChunks: Infinity
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'manifest',
chunks: 'vendor'
}),
new webpack.DllReferencePlugin({
context: __dirname,
manifest: require('./public/lib/min/manifest.json') // eslint-disable-line
}),
new webpack.ContextReplacementPlugin(
/moment[\/\\]locale$/,
/(en-gb|zh-cn).js/
),
// 配置打包后的样式文件名称
new ExtractTextPlugin({ filename: 'css/[name].[contenthash].css', allChunks: true, disable: false }),
new UglifyJsPlugin({
uglifyOptions: {
ie8: false,
ecma: 8,
mangle: true,
output: {
comments: false
},
compress: {
warnings: false
}
},
sourceMap: false,
cache: true,
parallel: os.cpus().length * 2
}),
new AddAssetHtmlPlugin([
{
filepath: path.resolve(__dirname, './public/lib/min/lib.42b050340.js'),
outputPath: 'lib/min',
publicPath: '/dist/lib/min',
includeSourcemap: false
}
]),
new HtmlWebpackPlugin({
template: 'index.html',
hash: false,
filename: 'index.html',
inject: 'body',
chunks: ['manifest', 'vendor', 'index'],
minify: {
collapseWhitespace: true,
removeComments: true,
removeAttributeQuotes: true
}
}),
// 可视化分析工具
new BundleAnalyzerPlugin()
]
};