forked from tangly1024/NotionNext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnext.config.js
More file actions
270 lines (244 loc) · 6.83 KB
/
next.config.js
File metadata and controls
270 lines (244 loc) · 6.83 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
const { THEME } = require('./blog.config')
const fs = require('fs')
const path = require('path')
const BLOG = require('./blog.config')
const { extractLangPrefix } = require('./lib/utils/pageId')
// 打包时代码分析工具 (如果不需要分析,也不会报错)
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: process.env.ANALYZE === 'true'
})
// 扫描主题目录
const themes = scanSubdirectories(path.resolve(__dirname, 'themes'))
// 检测用户开启的多语言
const locales = (function () {
const langs = [BLOG.LANG]
if (BLOG.NOTION_PAGE_ID.indexOf(',') > 0) {
const siteIds = BLOG.NOTION_PAGE_ID.split(',')
for (let index = 0; index < siteIds.length; index++) {
const siteId = siteIds[index]
const prefix = extractLangPrefix(siteId)
if (prefix) {
if (!langs.includes(prefix)) {
langs.push(prefix)
}
}
}
}
return langs
})()
// 编译前执行的任务
// eslint-disable-next-line no-unused-vars
const preBuild = (function () {
if (
!process.env.npm_lifecycle_event === 'export' &&
!process.env.npm_lifecycle_event === 'build'
) {
return
}
// 清理旧的 sitemap,避免冲突
const sitemapPath = path.resolve(__dirname, 'public', 'sitemap.xml')
if (fs.existsSync(sitemapPath)) {
fs.unlinkSync(sitemapPath)
console.log('Deleted existing sitemap.xml from public directory')
}
const sitemap2Path = path.resolve(__dirname, 'sitemap.xml')
if (fs.existsSync(sitemap2Path)) {
fs.unlinkSync(sitemap2Path)
console.log('Deleted existing sitemap.xml from root directory')
}
})()
/**
* 扫描指定目录下的文件夹名
*/
function scanSubdirectories(directory) {
const subdirectories = []
if(fs.existsSync(directory)){
fs.readdirSync(directory).forEach(file => {
const fullPath = path.join(directory, file)
const stats = fs.statSync(fullPath)
if (stats.isDirectory()) {
subdirectories.push(file)
}
})
}
return subdirectories
}
/** @type {import('next').NextConfig} */
const nextConfig = {
// 忽略 ESLint 错误,防止构建失败
eslint: {
ignoreDuringBuilds: true
},
// 输出模式配置
output: process.env.EXPORT
? 'export'
: process.env.NEXT_BUILD_STANDALONE === 'true'
? 'standalone'
: undefined,
staticPageGenerationTimeout: 120,
// 性能压缩
compress: true,
poweredByHeader: false,
generateEtags: true,
// 编译器优化
swcMinify: true,
// 模块化导入优化
modularizeImports: {
'@heroicons/react/24/outline': {
transform: '@heroicons/react/24/outline/{{member}}'
},
'@heroicons/react/24/solid': {
transform: '@heroicons/react/24/solid/{{member}}'
}
},
// 多语言配置 (Export 模式下禁用)
i18n: process.env.EXPORT
? undefined
: {
defaultLocale: BLOG.LANG,
locales: locales
},
images: {
formats: ['image/avif', 'image/webp'],
deviceSizes: [640, 750, 828, 1080, 1200, 1920, 2048, 3840],
imageSizes: [16, 32, 48, 64, 96, 128, 256, 384],
domains: [
'gravatar.com',
'www.notion.so',
'avatars.githubusercontent.com',
'images.unsplash.com',
'source.unsplash.com',
'p1.qhimg.com',
'webmention.io',
'ko-fi.com'
],
loader: 'default',
minimumCacheTTL: 60 * 60 * 24 * 7,
dangerouslyAllowSVG: true,
contentSecurityPolicy: "default-src 'self'; script-src 'none'; sandbox;"
},
// 重定向配置
redirects: process.env.EXPORT
? undefined
: async () => {
return [
{
source: '/feed',
destination: '/rss/feed.xml',
permanent: true
}
]
},
// URL 重写配置
rewrites: process.env.EXPORT
? undefined
: async () => {
const langsRewrites = []
if (BLOG.NOTION_PAGE_ID.indexOf(',') > 0) {
const siteIds = BLOG.NOTION_PAGE_ID.split(',')
const langs = []
for (let index = 0; index < siteIds.length; index++) {
const siteId = siteIds[index]
const prefix = extractLangPrefix(siteId)
if (prefix) {
langs.push(prefix)
}
}
langsRewrites.push(
{
source: `/:locale(${langs.join('|')})/:path*`,
destination: '/:path*'
},
{
source: `/:locale(${langs.join('|')})`,
destination: '/'
},
{
source: `/:locale(${langs.join('|')})/`,
destination: '/'
}
)
}
return [
...langsRewrites,
{
source: '/:path*.html',
destination: '/:path*'
}
]
},
// Header 配置
headers: process.env.EXPORT
? undefined
: async () => {
return [
{
source: '/:path*{/}?',
headers: [
{ key: 'Access-Control-Allow-Credentials', value: 'true' },
{ key: 'Access-Control-Allow-Origin', value: '*' },
{
key: 'Access-Control-Allow-Methods',
value: 'GET,OPTIONS,PATCH,DELETE,POST,PUT'
},
{
key: 'Access-Control-Allow-Headers',
value:
'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version'
}
]
},
]
},
// Webpack 配置
webpack: (config, { dev, isServer }) => {
config.resolve.alias['@'] = path.resolve(__dirname)
if (!isServer) {
// 仅在客户端构建时打印
// console.log('[默认主题]', path.resolve(__dirname, 'themes', THEME))
}
config.resolve.alias['@theme-components'] = path.resolve(
__dirname,
'themes',
THEME
)
if (!dev) {
config.optimization = {
...config.optimization,
splitChunks: {
chunks: 'all',
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all',
},
common: {
name: 'common',
minChunks: 2,
chunks: 'all',
enforce: true,
},
},
},
}
}
config.resolve.modules = [
path.resolve(__dirname, 'node_modules'),
'node_modules'
]
return config
},
// 实验性配置
experimental: {
scrollRestoration: true,
optimizePackageImports: ['@heroicons/react', 'lodash', 'react-icons', '@use-gesture/react']
},
publicRuntimeConfig: {
THEMES: themes
}
}
// 导出配置 (使用 bundle analyzer 包裹)
module.exports = process.env.ANALYZE === 'true'
? withBundleAnalyzer(nextConfig)
: nextConfig