-
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathwebpack.config.js
More file actions
238 lines (236 loc) · 7.14 KB
/
webpack.config.js
File metadata and controls
238 lines (236 loc) · 7.14 KB
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
const path = require('path');
const webpack = require('webpack');
const TerserPlugin = require('terser-webpack-plugin');
const CompressionPlugin = require('compression-webpack-plugin');
const { GenerateSW } = require('workbox-webpack-plugin');
const { sentryWebpackPlugin } = require('@sentry/webpack-plugin');
// Sentry 配置(仅生产环境且配置完整时启用)
const sentryAuthToken = process.env.SENTRY_AUTH_TOKEN;
const sentryOrg = process.env.SENTRY_ORG;
const sentryProject = process.env.SENTRY_PROJECT;
const isSentryEnabled = sentryAuthToken && sentryOrg && sentryProject && process.env.NODE_ENV === 'production';
module.exports = {
entry: {
main: './src/js/main.js',
giffgaff: './src/js/giffgaff.js',
simyo: './src/js/simyo.js',
footer: './src/js/bootstrap-footer.js'
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'js/[name].[contenthash].js',
clean: true
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
},
{
test: /\.css$/,
use: [
'style-loader',
'css-loader',
'postcss-loader'
]
}
]
},
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
terserOptions: {
compress: {
drop_console: true,
drop_debugger: true,
pure_funcs: ['console.log', 'console.info', 'console.debug'], // Remove specific console methods
passes: 2 // Run compression twice for better results
},
mangle: {
safari10: true // Fix Safari 10 loop iterator bug
}
},
parallel: true,
extractComments: false // Don't create separate license files
})
],
splitChunks: {
chunks: 'all',
maxInitialRequests: 5,
maxAsyncRequests: 5,
minSize: 10000, // 10KB minimum
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
priority: 10,
reuseExistingChunk: true
},
common: {
minChunks: 2,
priority: 5,
reuseExistingChunk: true,
name: 'common'
}
}
},
runtimeChunk: {
name: 'runtime' // Extract webpack runtime for better caching
},
moduleIds: 'deterministic', // Stable module IDs for better long-term caching
chunkIds: 'deterministic'
},
plugins: [
new webpack.DefinePlugin({
'process.env.ACCESS_KEY': JSON.stringify(process.env.ACCESS_KEY || ''),
'process.env.TURNSTILE_SITE_KEY': JSON.stringify(process.env.TURNSTILE_SITE_KEY || ''),
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'production'),
'process.env.SENTRY_DSN': JSON.stringify(process.env.SENTRY_DSN || ''),
'process.env.SENTRY_ENVIRONMENT': JSON.stringify(process.env.SENTRY_ENVIRONMENT || 'production'),
// SENTRY_RELEASE: 优先使用环境变量,其次使用 Netlify COMMIT_REF,最后使用 package.json 版本
'process.env.SENTRY_RELEASE': JSON.stringify(
process.env.SENTRY_RELEASE ||
(process.env.COMMIT_REF ? `esim-tools@${process.env.COMMIT_REF.slice(0, 7)}` : null) ||
`esim-tools@${require('./package.json').version}`
),
}),
new CompressionPlugin({
test: /\.(js|css|html|svg)$/,
algorithm: 'gzip',
threshold: 10240, // Only compress files > 10KB
minRatio: 0.8,
deleteOriginalAssets: false
}),
// Add Brotli compression for better compression ratio
new CompressionPlugin({
filename: '[path][base].br',
algorithm: 'brotliCompress',
test: /\.(js|css|html|svg)$/,
compressionOptions: {
params: {
[require('zlib').constants.BROTLI_PARAM_QUALITY]: 11
}
},
threshold: 10240,
minRatio: 0.8,
deleteOriginalAssets: false
}),
new GenerateSW({
swDest: 'sw.js',
clientsClaim: true,
skipWaiting: true,
cleanupOutdatedCaches: true,
maximumFileSizeToCacheInBytes: 5 * 1024 * 1024, // 5MB limit
runtimeCaching: [
{
urlPattern: /^https:\/\/api\.qrserver\.com/,
handler: 'CacheFirst',
options: {
cacheName: 'qr-cache',
expiration: {
maxEntries: 50,
maxAgeSeconds: 24 * 60 * 60 // 24 hours
},
cacheableResponse: {
statuses: [0, 200]
}
}
},
{
urlPattern: /^https:\/\/qrcode\.show/,
handler: 'CacheFirst',
options: {
cacheName: 'qr-cache',
expiration: {
maxEntries: 50,
maxAgeSeconds: 24 * 60 * 60
},
cacheableResponse: {
statuses: [0, 200]
}
}
},
{
urlPattern: /^https:\/\/api\.giffgaff\.com/,
handler: 'NetworkFirst',
options: {
cacheName: 'giffgaff-api',
expiration: {
maxEntries: 100,
maxAgeSeconds: 5 * 60 // 5 minutes
},
networkTimeoutSeconds: 10,
cacheableResponse: {
statuses: [0, 200]
}
}
},
{
urlPattern: /^https:\/\/appapi\.simyo\.nl/,
handler: 'NetworkFirst',
options: {
cacheName: 'simyo-api',
expiration: {
maxEntries: 100,
maxAgeSeconds: 5 * 60 // 5 minutes
},
networkTimeoutSeconds: 10,
cacheableResponse: {
statuses: [0, 200]
}
}
}
]
}),
// Sentry Source Maps 上传(仅生产环境且配置完整时启用)
...(isSentryEnabled ? [sentryWebpackPlugin({
authToken: sentryAuthToken,
org: sentryOrg,
project: sentryProject,
release: {
name: process.env.SENTRY_RELEASE ||
(process.env.COMMIT_REF ? `esim-tools@${process.env.COMMIT_REF.slice(0, 7)}` : null) ||
`esim-tools@${require('./package.json').version}`,
// 自动关联 Git commits(需要在 Git 仓库中运行)
setCommits: {
auto: true,
ignoreMissing: true
}
},
sourcemaps: {
// 上传 dist 目录下的 source maps
assets: './dist/**/*.js.map',
// 上传后删除本地 source maps(安全考虑)
deleteFilesAfterUpload: './dist/**/*.js.map'
},
// 静默模式,减少构建日志
silent: false,
errorHandler: (err) => {
console.warn('[Sentry] Source maps upload failed:', err.message);
// 不阻止构建
}
})] : [])
],
resolve: {
extensions: ['.js', '.css'],
alias: {
'@': path.resolve(__dirname, 'src'),
'@modules': path.resolve(__dirname, 'src/js/modules'),
'@utils': path.resolve(__dirname, 'src/js/modules/utils')
}
},
performance: {
hints: 'warning',
maxEntrypointSize: 512000, // 500KB
maxAssetSize: 512000
},
devtool: 'source-map'
};