-
Notifications
You must be signed in to change notification settings - Fork 31
/
webpack.config.js
239 lines (236 loc) · 8 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
const path = require('path');
const webpack = require('webpack');
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const SanLoaderPlugin = require('san-loader/lib/plugin');
const TerserPlugin = require('terser-webpack-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const MiniCssExportPlugin = require('mini-css-extract-plugin');
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
const resolve = p => path.resolve(__dirname, './src', p);
const pkg = require('./package.json');
const isProduction = process.env.NODE_ENV === 'production';
const plugins = [
new SanLoaderPlugin(),
new webpack.BannerPlugin({
banner: `${pkg.name} v${pkg.version}\nby ${pkg.author}\nCreated at ${dateFormat(
new Date(),
'yyyy-MM-dd hh:mm:ss'
)}`
}),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
'process.env.DEBUG': JSON.stringify(process.env.DEBUG)
}),
new HtmlWebpackPlugin({
filename: 'index.html',
chunks: ['home'],
template: './pages/home.html'
}),
new HtmlWebpackPlugin({
inject: 'head',
scriptLoading: 'blocking',
filename: 'demo.html',
chunks: ['backend'],
template: './pages/demo.html'
}),
new NodePolyfillPlugin({
excludeAliases: ['console']
})
];
if (isProduction) {
plugins.push(new MiniCssExportPlugin(), new CleanWebpackPlugin());
}
module.exports = {
mode: isProduction ? 'production' : 'development',
devtool: isProduction ? false : 'eval-cheap-source-map',
output: {
filename: '[name].js',
libraryTarget: 'umd',
libraryExport: 'default',
umdNamedDefine: true,
path: path.resolve('./dist/')
},
devServer: {
static: {
directory: './dist'
},
hot: true
},
resolve: {
alias: {
'@': resolve('/'),
'@lib': resolve('lib'),
'@components': resolve('components')
}
},
optimization: isProduction
? {
minimize: true,
splitChunks: {
cacheGroups: {
santd: {
test(module, chunks) {
const test = /[\\/]node_modules[\\/]santd/;
if (module.nameForCondition && test.test(module.nameForCondition())) {
return module.type === 'javascript/auto';
}
for (const chunk of module.chunksIterable) {
if (chunk.name && test.test(chunk.name)) {
return module.type === 'javascript/auto';
}
}
return false;
},
name: 'santd',
chunks(chunk) {
// exclude index chunk, 这个 chunk 用于外部直接使用 frontend
return chunk.name !== 'index';
}
}
}
},
minimizer: [
new TerserPlugin({
terserOptions: {
format: {
comments: false
}
},
extractComments: false
}),
new CssMinimizerPlugin()
]
}
: undefined,
entry: {
backend: './src/backend.js',
home: './src/home.js'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
},
{
oneOf: [
{
test: /\.svg$/,
use: [
{
loader: 'url-loader'
},
{
loader: 'svgo-loader'
}
]
},
{
test: /\.(png|jpg|gif)$/,
use: [
{
loader: 'url-loader',
options: {
limit: 4000,
name: 'assets/[name]-[hash:8].[ext]'
}
}
]
},
{
test: /\.(eot|woff|woff2|ttf|svg)$/,
loader: 'file-loader',
options: {
limit: 6000,
name: 'icons/[name]-[hash:8].[ext]'
}
},
{
test: /\.less$/,
use: [
isProduction ? MiniCssExportPlugin.loader : 'style-loader',
'css-loader',
{
loader: 'postcss-loader'
},
{
loader: 'less-loader',
options: {
lessOptions: {
// strictMath: true,
javascriptEnabled: true
}
}
}
]
},
{
test: /\.css$/,
use: [
isProduction ? MiniCssExportPlugin.loader : 'style-loader',
'css-loader',
{
loader: 'postcss-loader'
}
]
}
]
},
{
test: /\.san$/,
use: 'san-loader'
},
{
test: /\.html$/,
use: [
{
loader: 'html-loader',
options: {
esModule: false,
minimize: false,
sources: {
list: [
{
tag: 'img',
attribute: 'src',
type: 'src'
},
{
tag: 'san-avatar',
attribute: 'src',
type: 'src'
}
]
}
}
}
]
}
]
},
plugins
};
function dateFormat(d, pattern = 'yyyy-MM-dd') {
if (!d) {
d = new Date();
}
let y = d.getFullYear().toString();
let o = {
M: d.getMonth() + 1, // month
d: d.getDate(), // day
h: d.getHours(), // hour
m: d.getMinutes(), // minute
s: d.getSeconds() // second
};
pattern = pattern.replace(/(y+)/gi, (a, b) => y.substr(4 - Math.min(4, b.length)));
Object.keys(o).forEach(i => {
pattern = pattern.replace(new RegExp('(' + i + '+)', 'g'), (a, b) =>
o[i] < 10 && b.length > 1 ? '0' + o[i] : o[i]
);
});
return pattern;
}