-
Notifications
You must be signed in to change notification settings - Fork 217
Expand file tree
/
Copy pathwebpack.common.js
More file actions
350 lines (339 loc) · 10.4 KB
/
webpack.common.js
File metadata and controls
350 lines (339 loc) · 10.4 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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import path, { dirname } from 'path'
import HtmlWebpackPlugin from 'html-webpack-plugin'
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer'
import AntdDayjsWebpackPlugin from 'antd-dayjs-webpack-plugin'
import Dotenv from 'dotenv-webpack'
import CaseSensitivePathsPlugin from 'case-sensitive-paths-webpack-plugin'
// import CircularDependencyPlugin from 'circular-dependency-plugin'
import NodePolyfillPlugin from 'node-polyfill-webpack-plugin'
import WebpackBar from 'webpackbar'
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin'
import ESLintWebpackPlugin from 'eslint-webpack-plugin'
import { codeInspectorPlugin } from 'code-inspector-plugin'
import paths from './paths.js'
import { fileURLToPath } from 'url'
import dotenv from 'dotenv'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const isDev = process.env.NODE_ENV === 'development'
const isAnalyze = Boolean(Number(process.env.USE_ANALYZE || 0))
const UNABLE_ANALYZE = 0
const USE_ANALYZE = process.env.USE_ANALYZE || UNABLE_ANALYZE
let dotEnv = ''
switch (process.env.BUILD_GOAL) {
case 'development':
dotEnv = '.env.development'
break
case 'production':
dotEnv = '.env.production'
break
case 'dev':
dotEnv = '.env.dev'
break
case 'test':
dotEnv = '.env.test'
break
default:
dotEnv = '.env.development'
}
// Ensure PUBLIC_URL (and other env vars) are available to this webpack config file.
// Note: dotenv-webpack injects env vars into the bundle, but it doesn't affect the
// Node.js process.env used while generating the webpack configuration.
dotenv.config({ path: path.resolve(__dirname, '..', dotEnv) })
// GitHub Pages typically serves the site under "/<repo>/".
// When building in GitHub Actions and PUBLIC_URL isn't explicitly provided,
// infer it from GITHUB_REPOSITORY to avoid broken asset paths.
const inferredGhPagesPublicUrl = (() => {
try {
const repo = process.env.GITHUB_REPOSITORY
if (!repo) return ''
const parts = repo.split('/')
const repoName = parts[1]
return repoName ? `/${repoName}/` : ''
} catch {
return ''
}
})()
const normalizePublicPath = (value) => {
const raw = (value ?? '').toString().trim()
if (!raw) return '/'
if (raw === '.') return '/'
if (raw === './') return './'
if (raw.startsWith('http://') || raw.startsWith('https://')) {
return raw.endsWith('/') ? raw : `${raw}/`
}
// Prefer absolute paths so chunks load correctly on deep-link routes.
const withLeading = raw.startsWith('/') ? raw : `/${raw}`
return withLeading.endsWith('/') ? withLeading : `${withLeading}/`
}
const rawPublicUrl = process.env.PUBLIC_URL
const prodPublicPath = normalizePublicPath(
rawPublicUrl ||
(process.env.GITHUB_ACTIONS === 'true' || process.env.GITHUB_ACTIONS === '1' ? inferredGhPagesPublicUrl : '')
)
const config = {
entry: {
app: paths.entry,
},
output: {
path: paths.build,
publicPath: isDev ? '/' : prodPublicPath,
filename: isDev ? 'static/js/[name].js' : 'static/js/[name].[contenthash].js',
chunkFilename: isDev ? 'static/js/[name].js' : 'static/js/[name].[contenthash].js',
// library: '',
// libraryTarget: 'umd',
// chunkLoadingGlobal: '',
clean: true,
// contentBase: path.join(__dirname, "public"), // 配置额外的静态文件内容的访问路径
},
resolve: {
extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '...'],
alias: {
'@assets/audio': path.resolve('./src/assets/audio'),
'@assets/video': path.resolve('./src/assets/video'),
'@': path.resolve('./src'),
'@src': path.resolve('./src'),
'@app': paths.appDir,
'@stateless': path.resolve('./src/components/stateless'),
'@stateful': path.resolve('./src/components/stateful'),
'@hooks': path.resolve('./src/components/hooks'),
'@app-hooks': path.resolve('./src/app-hooks'),
'@container': path.resolve('./src/components/container'),
'@assets': path.resolve('./src/assets'),
'@pages': path.resolve('./src/pages'),
'@routers': paths.routersDir,
'@utils': path.resolve('./src/utils'),
'@theme': path.resolve('./src/theme'),
// 确保关键依赖只有一个实例,避免 zustand middleware 错误
zustand: path.resolve('./node_modules/zustand'),
immer: path.resolve('./node_modules/immer'),
react: path.resolve('./node_modules/react'),
'react-dom': path.resolve('./node_modules/react-dom'),
},
symlinks: false,
},
plugins: [
new Dotenv({
path: path.resolve(__dirname, '..', dotEnv),
}),
codeInspectorPlugin({
bundler: 'webpack',
}),
new HtmlWebpackPlugin({
title:
(isDev ? 'Pro React Dev' : 'Pro React Admin') +
(paths.projectName && paths.projectName !== 'default' ? ` (${paths.projectName})` : ''),
template: paths.htmlTemplate,
favicon: paths.favicon,
filename: 'index.html',
inject: 'body',
hash: true,
cache: false,
minify: isDev
? false
: {
removeAttributeQuotes: true,
collapseWhitespace: true,
removeComments: true,
collapseBooleanAttributes: true,
collapseInlineTagWhitespace: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
minifyCSS: true,
minifyJS: true,
minifyURLs: true,
useShortDoctype: true,
},
}),
new AntdDayjsWebpackPlugin(),
new CaseSensitivePathsPlugin(),
// new CircularDependencyPlugin({
// exclude: /node_modules/,
// include: /src/,
// failOnError: true,
// allowAsyncCycles: false,
// cwd: process.cwd(),
// }),
new NodePolyfillPlugin(),
new WebpackBar(),
new ForkTsCheckerWebpackPlugin({
async: true,
}),
new ESLintWebpackPlugin({
context: path.resolve(__dirname, '../src'),
exclude: 'node_modules', // 默认值
cache: false, // 禁用/开启缓存
cacheLocation: path.resolve(__dirname, '../node_modules/.cache/.eslintcache'), // 缓存目录
}),
],
module: {
// 将缺失的导出提示成错误而不是警告
strictExportPresence: true,
rules: [
{
test: /\.m?js$/,
resolve: {
fullySpecified: false,
},
},
{
test: /\.module\.css$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
sourceMap: true,
modules: {
mode: 'local',
auto: true,
exportGlobals: true,
localIdentName: isDev ? '[path][name]__[local]--[hash:base64:5]' : '[local]--[hash:base64:5]',
localIdentContext: paths.src,
namedExport: false,
exportLocalsConvention: 'camelCase',
},
importLoaders: 1,
},
},
'postcss-loader',
],
},
{
test: /\.css$/,
exclude: /\.module\.css$/,
use: ['style-loader', 'css-loader', 'postcss-loader'],
},
{
test: /\.less$/i,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
sourceMap: true,
modules: {
mode: 'local',
auto: true,
exportGlobals: true,
localIdentName: isDev ? '[path][name]__[local]--[hash:base64:5]' : '[local]--[hash:base64:5]',
localIdentContext: paths.src,
namedExport: false,
exportLocalsConvention: 'camelCase',
},
importLoaders: 2,
},
},
{
loader: 'postcss-loader',
options: {
postcssOptions: {
ident: 'postcss',
config: false,
plugins: [
'postcss-flexbugs-fixes',
[
'postcss-preset-env',
{
autoprefixer: {
flexbox: 'no-2009',
},
stage: 3,
},
],
'postcss-normalize',
],
},
sourceMap: true,
},
},
],
},
{
test: /\.(js|jsx|ts|tsx)$/,
exclude: /node_modules/,
use: [
{
loader: 'esbuild-loader',
options: {
// loader: 'tsx',
target: 'es2020',
},
},
{
loader: 'babel-loader',
options: {
presets: [['@babel/preset-env', { modules: false }], '@babel/preset-react'],
plugins: ['@babel/plugin-transform-object-rest-spread', '@babel/plugin-transform-runtime'],
},
},
],
},
{
test: /\.(png|jpe?g|gif|webp|eot|ttf|woff|woff2|mp4|mp3|mkv|pdf)$/i,
type: 'asset',
parser: {
// Conditions for converting to base64
dataUrlCondition: {
maxSize: 25 * 1024, // 25kb
},
},
generator: {
filename: 'images/[contenthash][ext][query]',
},
},
{
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: 'babel-loader',
},
{
loader: '@svgr/webpack',
options: {
babel: false,
icon: true,
},
},
],
},
],
},
stats: {
...(isAnalyze
? {
preset: 'verbose',
assets: true,
chunks: true,
chunkModules: true,
chunkOrigins: true,
chunkRelations: true,
entrypoints: true,
modules: true,
nestedModules: true,
dependentModules: true,
reasons: true,
ids: true,
errorDetails: true,
}
: {
all: false,
errors: true,
warnings: true,
errorDetails: true,
moduleTrace: true,
excludeAssets: /node_modules/,
}),
},
// 性能提示
performance: {
hints: isDev ? false : 'warning',
maxEntrypointSize: 512000,
maxAssetSize: 512000,
},
}
if (USE_ANALYZE) {
config.plugins.push(new BundleAnalyzerPlugin())
}
export default config