-
-
Notifications
You must be signed in to change notification settings - Fork 772
/
next.config.mjs
205 lines (178 loc) · 5.45 KB
/
next.config.mjs
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
import { execSync } from 'node:child_process'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import NextBundleAnalyzer from '@next/bundle-analyzer'
import { codeInspectorPlugin } from 'code-inspector-plugin'
import { config } from 'dotenv'
process.title = 'Shiro (NextJS)'
const env = config().parsed || {}
const isProd = process.env.NODE_ENV === 'production'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
let commitHash = ''
let commitUrl = ''
const repoInfo = getRepoInfo()
if (repoInfo) {
commitHash = repoInfo.hash
commitUrl = repoInfo.url
}
/** @type {import('next').NextConfig} */
let nextConfig = {
// logging: {
// fetches: {
// // fullUrl: true,
// },
// },
env: {
COMMIT_HASH: commitHash,
COMMIT_URL: commitUrl,
},
reactStrictMode: true,
productionBrowserSourceMaps: false,
output: 'standalone',
assetPrefix: isProd ? env.ASSETPREFIX || undefined : undefined,
compiler: {
// reactRemoveProperties: { properties: ['^data-id$', '^data-(\\w+)-id$'] },
},
experimental: {
serverMinification: true,
webpackBuildWorker: true,
// optimizePackageImports: ['dayjs'],
},
images: {
remotePatterns: [
{
protocol: 'https',
hostname: '**',
},
],
dangerouslyAllowSVG: true,
contentSecurityPolicy:
"default-src 'self'; script-src 'none'; sandbox; style-src 'unsafe-inline';",
},
async rewrites() {
return {
beforeFiles: [
{ source: '/atom.xml', destination: '/feed' },
{ source: '/feed.xml', destination: '/feed' },
{ source: '/sitemap.xml', destination: '/sitemap' },
],
}
},
webpack: (config) => {
config.resolve.alias['jotai'] = path.resolve(
__dirname,
'node_modules/jotai',
)
config.externals.push({
'utf-8-validate': 'commonjs utf-8-validate',
bufferutil: 'commonjs bufferutil',
})
config.plugins.push(
codeInspectorPlugin({ bundler: 'webpack', hotKeys: ['metaKey'] }),
)
return config
},
}
// if (env.SENTRY === 'true' && isProd) {
// // @ts-expect-error
// nextConfig = withSentryConfig(
// nextConfig,
// {
// // For all available options, see:
// // https://github.com/getsentry/sentry-webpack-plugin#options
//
// // Suppresses source map uploading logs during build
// silent: true,
//
// org: 'inneis-site',
// project: 'Shiro',
// },
// {
// // For all available options, see:
// // https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/
//
// // Upload a larger set of source maps for prettier stack traces (increases build time)
// widenClientFileUpload: true,
//
// // Transpiles SDK to be compatible with IE11 (increases bundle size)
// transpileClientSDK: true,
//
// // Routes browser requests to Sentry through a Next.js rewrite to circumvent ad-blockers (increases server load)
// tunnelRoute: '/monitoring',
//
// // Hides source maps from generated client bundles
// hideSourceMaps: true,
//
// // Automatically tree-shake Sentry logger statements to reduce bundle size
// disableLogger: true,
// },
// )
// }
if (process.env.ANALYZE === 'true') {
nextConfig = NextBundleAnalyzer({
enabled: true,
})(nextConfig)
}
export default nextConfig
function getRepoInfo() {
if (process.env.VERCEL) {
const { VERCEL_GIT_PROVIDER, VERCEL_GIT_REPO_SLUG, VERCEL_GIT_REPO_OWNER } =
process.env
switch (VERCEL_GIT_PROVIDER) {
case 'github': {
return {
hash: process.env.VERCEL_GIT_COMMIT_SHA,
url: `https://github.com/${VERCEL_GIT_REPO_OWNER}/${VERCEL_GIT_REPO_SLUG}/commit/${process.env.VERCEL_GIT_COMMIT_SHA}`,
}
}
}
} else {
return getRepoInfoFromGit()
}
}
function getRepoInfoFromGit() {
try {
// 获取最新的 commit hash
// 获取当前分支名称
const currentBranch = execSync('git rev-parse --abbrev-ref HEAD')
.toString()
.trim()
// 获取当前分支跟踪的远程仓库名称
const remoteName = execSync(`git config branch.${currentBranch}.remote`)
.toString()
.trim()
// 获取当前分支跟踪的远程仓库的 URL
let remoteUrl = execSync(`git remote get-url ${remoteName}`)
.toString()
.trim()
// 获取最新的 commit hash
const hash = execSync('git rev-parse HEAD').toString().trim()
// 转换 git@ 格式的 URL 为 https:// 格式
if (remoteUrl.startsWith('git@')) {
remoteUrl = remoteUrl
.replace(':', '/')
.replace('git@', 'https://')
.replace('.git', '')
} else if (remoteUrl.endsWith('.git')) {
// 对于以 .git 结尾的 https URL,移除 .git
remoteUrl = remoteUrl.slice(0, -4)
}
// 根据不同的 Git 托管服务自定义 URL 生成规则
let webUrl
if (remoteUrl.includes('github.com')) {
webUrl = `${remoteUrl}/commit/${hash}`
} else if (remoteUrl.includes('gitlab.com')) {
webUrl = `${remoteUrl}/-/commit/${hash}`
} else if (remoteUrl.includes('bitbucket.org')) {
webUrl = `${remoteUrl}/commits/${hash}`
} else {
// 对于未知的托管服务,可以返回 null 或一个默认格式
webUrl = `${remoteUrl}/commits/${hash}`
}
return { hash, url: webUrl }
} catch (error) {
console.error('Error fetching repo info:', error?.stderr?.toString())
return null
}
}