-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
281 lines (272 loc) · 7.2 KB
/
webpack.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
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
278
279
280
281
const path = require('path');
const webpack = require('webpack');
const fs = require('fs');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const StyleLintPlugin = require('stylelint-webpack-plugin');
const AutoDllPlugin = require('autodll-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const nodeEnv = process.env.NODE_ENV || 'development';
const isDev = nodeEnv !== 'production';
const ASSET_PATH = process.env.ASSET_PATH || '/';
const ANALYZER = process.env.ANALYZER || false;
// 环境
const PORT_ENV = process.env.PORT || 3000;
const HOST_ENV = process.env.HOST || '';
const APIPORT_ENV = process.env.APIPORT || 18081;
const PREVIEW = process.env.PREVIEW || false;
const isAutoDll = isDev; // 是否开启 autodll
const eslint = true;
const stylelint = false;
// 获取主题颜色
const pkgPath = path.resolve(__dirname, './package.json');
const pkg = fs.existsSync(pkgPath) ? require(pkgPath) : {};
let theme = {};
if (pkg.theme && typeof pkg.theme === 'string') {
let cfgPath = pkg.theme;
// relative path
if (cfgPath.charAt(0) === '.') {
cfgPath = path.resolve(__dirname, cfgPath);
}
const getThemeConfig = require(cfgPath);
theme = getThemeConfig();
} else if (pkg.theme && typeof pkg.theme === 'object') {
theme = pkg.theme;
}
const vendor = [
'react',
'react-dom',
'mockjs',
'redbox-react',
'axios'
];
console.log(isDev ? `开发模式: ${HOST_ENV}:${PORT_ENV}` : `发布模式: ${HOST_ENV}:${PORT_ENV}`);
// 设置插件环境 development/prodcution
const getPlugins = () => {
// Common
const plugins = [
new ExtractTextPlugin({
filename: '[name].[contenthash:8].css',
allChunks: true,
disable: isDev
}),
new HtmlWebpackPlugin({
title: 'webpack-antd',
template: path.join(process.cwd(), '/public/index.html')
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks: Infinity
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'manifest'
}),
new webpack.EnvironmentPlugin({ NODE_ENV: JSON.stringify(nodeEnv) }),
new webpack.DefinePlugin({
'process.env.ASSET_PATH': JSON.stringify(ASSET_PATH),
__DEV__: isDev,
__PORT__: PORT_ENV,
__HOST__: JSON.stringify(HOST_ENV),
__APIPORT__: JSON.stringify(APIPORT_ENV),
__PREVIEW__: PREVIEW
}),
new webpack.NoEmitOnErrorsPlugin()
];
if (isDev) {
plugins.push(
new webpack.HotModuleReplacementPlugin(),
new StyleLintPlugin({ failOnError: stylelint }),
new webpack.NamedModulesPlugin()
);
} else {
plugins.push(
new CleanWebpackPlugin(['public/dist']),
new webpack.HashedModuleIdsPlugin(),
new webpack.optimize.ModuleConcatenationPlugin(),
new CopyWebpackPlugin([{
from: 'assets/*', context: 'public/'
}]),
new UglifyJsPlugin({
uglifyOptions: {
beautify: true, // 最紧凑的输出
comments: true, // 删除所有的注释
compress: {
warnings: false,
drop_console: !PREVIEW, // 删除所有的 `console` 语句
collapse_vars: true,
reduce_vars: true, // 提取出出现多次但是没有定义成变量去引用的静态值
}
}
}),
);
}
if (isAutoDll) {
plugins.push(
new AutoDllPlugin({
context: path.resolve(process.cwd()),
inject: true,
filename: '[name].dll.js',
entry: {
vendor
}
})
)
}
if (ANALYZER) {
plugins.push(
new BundleAnalyzerPlugin(),
);
}
return plugins;
};
// 获取loaders
const webpackLoaders = () => {
const loaders = [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
cacheDirectory: isDev
}
},
{
test: /\.css$/,
include: /node_modules/,
use: ExtractTextPlugin.extract(
{
fallback: 'style-loader',
use:['css-loader', 'postcss-loader']
},
)
},
{
test: /\.css$/,
exclude: /node_modules/,
use: ExtractTextPlugin.extract(
{
fallback: 'style-loader',
use:['css-loader', 'postcss-loader']
},
)
}, {
test: /\.less$/,
include: /node_modules/,
use: ExtractTextPlugin.extract(
{
fallback: 'style-loader',
use:[
'css-loader', 'postcss-loader',
{
loader: 'less-loader',
options: {
javascriptEnabled: true,
modifyVars: theme
}
}
]
}
)
}, {
test: /\.less$/,
exclude: /node_modules/,
use: ExtractTextPlugin.extract(
{
fallback: 'style-loader',
use:[
{
loader: 'css-loader',
options: {
modules: true,
import: true,
importLoaders: 1,
localIdentName: '[path]__[name]__[local]__[hash:base64:5]',
sourceMap: true
}
},
{ loader: 'postcss-loader', options: { sourceMap: true } },
{
loader: 'less-loader',
options: {
outputStyle: 'expanded',
sourceMap: true,
javascriptEnabled: true,
sourceMapContents: !isDev
}
}
]
}
)
},
{
test: /\.(png|svg|jpg|gif)$/,
use: [
{
loader: 'url-loader',
options: {
limit: 10240
}
}
]
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/,
use: [
'file-loader'
]
}
];
if (eslint) {
loaders.unshift({
test: /\.(js|jsx)?$/,
enforce: 'pre',
exclude: /node_modules/,
loader: 'eslint-loader'
});
}
return loaders;
};
module.exports = {
name: 'client',
target: 'web',
cache: isDev,
profile: isDev, // 是否捕捉 Webpack 构建的性能信息
context: path.resolve(process.cwd()),
entry: {
index: './src/index.js',
vendor
},
devtool: isDev ? 'inline-source-map' : 'hidden-source-map',
output: {
path: path.join(__dirname, 'public/dist'),
publicPath: ASSET_PATH,
filename: isDev ? '[name].js' : '[name].[chunkhash:8].js',
chunkFilename: isDev ? '[name].chunk.js' : '[name].[chunkhash:8].chunk.js',
pathinfo: isDev
},
devServer: {
contentBase: path.join(__dirname, "public"),
historyApiFallback: false, // 如果使用 createBrowserHistory 那么应该设置为 true
overlay: true,
hot: true,
port: PORT_ENV,
stats: {
modules: false,
colors: true
},
headers: {
'maby': 'demo'
}
},
plugins: getPlugins(),
module: {
loaders: webpackLoaders()
},
resolve: {
modules: [path.resolve(__dirname, 'src/'), 'node_modules'],
extensions: ['.js', '.jsx', '.json']
}
};