-
Notifications
You must be signed in to change notification settings - Fork 115
/
webpack.config.babel.js
261 lines (249 loc) · 9.19 KB
/
webpack.config.babel.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
import path from 'path';
import HtmlWebpackPlugin from 'html-webpack-plugin'; //html
import MiniCssExtractPlugin from 'mini-css-extract-plugin'; //css压缩
import webpack from 'webpack';
import TerserPlugin from 'terser-webpack-plugin';
import {api, devPort, dns, ip, title} from './config/common';
import CopyWebpackPlugin from 'copy-webpack-plugin'; //复制静态html
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const {CleanWebpackPlugin} = require('clean-webpack-plugin'); //清空
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin; //视图分析webpack情况
//多线程运行
const PUBLIC_PATH = `http://${ip}:${devPort}/`;
/**
* 公共打包插件
*/
const pluginsBuild = [
new CleanWebpackPlugin(),
new webpack.HotModuleReplacementPlugin() // 热更新插件
];
export default (env, argv) => {
let isDev = argv.mode
/**
* 公共插件
*/
const pluginsPublic = [
new HtmlWebpackPlugin({
template: path.join(__dirname, '/src/index.ejs'), // Load a custom template
inject: 'body', //注入到哪里
filename: 'index.html', //输出后的名称
hash: false, //为静态资源生成hash值
title: title,
ip: ip,
devPort: devPort,
dns: dns,
url: PUBLIC_PATH,
isDev
}),
new MiniCssExtractPlugin({
filename: 'css/[name].[fullhash].css',
chunkFilename: 'css/[chunkhash].css'
}),
new CopyWebpackPlugin({
patterns: [
{
from: path.resolve(__dirname, './public'),
to: path.resolve(__dirname, 'dist')
}
]
}),
new webpack.ContextReplacementPlugin(/moment[/\\]locale$/, /zh-cn/)
];
const minimize = {
//本地
development: false,
//测试
test: false,
//生产环境
production: true
};
const plugins = {
development: pluginsPublic.concat(pluginsBuild),
test: pluginsPublic.concat(pluginsBuild),
production: pluginsPublic.concat([])
};
const devtool = {
//本地
development: 'eval-source-map',
//测试
test: 'eval-source-map',
//生产环境
production: false
}
return {
optimization: {
runtimeChunk: {
name: 'manifest'
},
minimize: true,
minimizer: [
new TerserPlugin({
parallel: true,
terserOptions: {
// https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions
format: {
comments: false
}
}
})
],
splitChunks: {
chunks: 'async',
minSize: 20000,
minRemainingSize: 0,
maxSize: 0,
minChunks: 1,
maxAsyncRequests: 30,
maxInitialRequests: 30,
automaticNameDelimiter: '~',
enforceSizeThreshold: 50000,
cacheGroups: {
defaultVendors: {
test: /[\\/]node_modules[\\/]/,
priority: -10,
reuseExistingChunk: true
},
default: {
minChunks: 2,
priority: -20,
reuseExistingChunk: true
}
}
}
},
devServer: {
// contentBase: path.join(__dirname, 'dist'), //开发服务运行时的文件根目录
host: ip,
compress: true, //开发服务器是否启动gzip等压缩
port: devPort, //端口
historyApiFallback: true, //不会出现404页面,避免找不到
hot: true,
proxy: {
'/api': {
target: api.url,
changeOrigin: true,
secure: false
}
}
},
devtool: devtool[isDev], //cheap-eval-source-map 是一种比较快捷的map,没有映射列
entry: {
//入口
index: ['babel-polyfill', './src/index.js']
},
output: {
//出口
path: path.resolve(__dirname, 'dist'), //出口路径
filename: 'js/[name].[fullhash].js',
chunkFilename: 'js/[chunkhash].js', //按需加载名称
publicPath: PUBLIC_PATH //公共路径
},
resolve: {
mainFields: ['browser', 'main', 'jsnext:main'], //npm读取先后方式 jsnext:main 是采用es6模块写法
extensions: ['.ts', '.tsx', '.js'],
alias: {
//快捷入口
'@config': path.resolve(__dirname, 'config'),
'@components': path.resolve(__dirname, 'src/work/components'),
'@images': path.resolve(__dirname, 'src/work/images'),
'@style': path.resolve(__dirname, 'src/work/style'),
'@server': path.resolve(__dirname, 'src/work/server'),
'@common': path.resolve(__dirname, 'src/work/common'),
'@mobx': path.resolve(__dirname, 'src/work/mobx'),
'@page': path.resolve(__dirname, 'src/work/page'),
'@utils': path.resolve(__dirname, 'src/utils'),
'@mock': path.resolve(__dirname, 'mock'),
'@enum': path.resolve(__dirname, 'src/work/enum')
},
fallback: {'os': require.resolve('os-browserify/browser')}
},
module: {
noParse: /node_modules\/(moment|chart\.js)/, //不解析
rules: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/, //排除
//include: [path.resolve(__dirname, 'src')], //包括
// loader: 'happypack/loader?id=babel'
use: ['babel-loader']
},
{
test: /\.ts?$/,
use: ['ts-loader'],
exclude: /(node_modules|bower_components)/
},
{
test: /\.css$/,
use: [
{loader: MiniCssExtractPlugin.loader},
{
loader: 'css-loader',
options: {
minimize: minimize[isDev] //压缩
// sourceMap: minimize[dev],
}
}
]
},
{
test: /\.(html)$/,
use: {
loader: 'html-loader',
options: {
attrs: [':data-src'], //为了做图片懒加载,那些属性需要被,制定什么属性被该loader解析
minimize: false
}
}
},
{
test: /\.(png|jpg|gif|jpeg|ttf|svg)$/,
exclude: /(node_modules|bower_components)/,
include: [path.resolve(__dirname, 'src/work/images')],
use: [
{
loader: 'url-loader?limit=8024', //limit 图片大小的衡量,进行base64处理
options: {
name: '[path][name].[ext]'
}
}
]
},
{
test: /\.less$/,
use: [{loader: MiniCssExtractPlugin.loader},
{
loader: 'css-loader?importLoaders=1',
options: {}
}, {
loader: 'less-loader', options: {
lessOptions: {
javascriptEnabled: true,
modifyVars: {
'primary-color': '#1DA57A',
'link-color': '#1DA57A',
'border-radius-base': '2px'
}
}
}
}]
},
{
test: /\.scss$/,
use: [
{loader: MiniCssExtractPlugin.loader},
{
loader: 'css-loader',
options: {
// sourceMap: minimize[dev],
}
},
{
loader: 'sass-loader'
}
]
}
]
},
plugins: plugins[isDev]
}
};