-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvite.config.ts
More file actions
136 lines (124 loc) · 4.49 KB
/
vite.config.ts
File metadata and controls
136 lines (124 loc) · 4.49 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
import prerender from '@prerenderer/rollup-plugin';
import react from '@vitejs/plugin-react';
import axios from 'axios';
import { visualizer } from 'rollup-plugin-visualizer';
import { defineConfig, loadEnv, type PluginOption } from 'vite';
import svgr from 'vite-plugin-svgr';
import { generateDynamicRoutes } from './src/utils/generateDynamicRoute';
function extractPostId(url: string): string {
const lastIndex = url.lastIndexOf('/');
return lastIndex !== -1 ? url.substring(lastIndex + 1) : url;
}
export default defineConfig(async ({ mode }) => {
const env = loadEnv(mode, process.cwd(), '');
const isVercel = env.VERCEL === '1' || env.CI === 'true';
const shouldPrerender = env.ENABLE_PRERENDER === 'true' && !isVercel;
const dynamicRoutes = shouldPrerender ? await generateDynamicRoutes(env.VITE_DEV_BASE_URL) : [];
return {
esbuild: {
include: /\.(ts|jsx|tsx)$/,
},
build: {
target: 'es2015',
outDir: 'dist',
sourcemap: false,
minify: false,
rollupOptions: {
input: ['/index.html'],
output: {
manualChunks: {
vendor: ['react', 'react-dom', 'react-router-dom'],
},
},
},
},
plugins: [
react({
jsxImportSource: '@emotion/react',
babel: {
plugins: [
[
'@emotion/babel-plugin',
{
autoLabel: 'dev-only',
labelFormat: '[filename]__[local]',
},
],
],
},
}),
...(shouldPrerender
? [
prerender({
routes: dynamicRoutes,
renderer: '@prerenderer/renderer-puppeteer',
server: {
host: 'localhost',
port: 5173,
},
rendererOptions: {
maxConcurrentRoutes: 1,
launchOptions: {
// chrome 버전 이슈 해결을 위한 env 설정
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
`--chrome-version=${process.env.CHROME_VERSION}`,
],
ignoreDefaultArgs: ['--disable-extensions'],
ignoreHTTPSErrors: true,
headless: true,
},
},
postProcess: async (renderRoute) => {
const postId = extractPostId(renderRoute.route);
try {
const { data } = await axios.get(`${env.VITE_DEV_BASE_URL}/api/post/${postId}`);
const title = data?.data?.title;
const imageUrl = data?.data?.imageUrl;
// 기존 메타 태그 제거
renderRoute.html = renderRoute.html
.replace(/http:/i, 'https:')
.replace(
/(https:\/\/)?(localhost|127\.0\.0\.1):\d*/i,
'https://www.milewriting.com',
)
.replace(/<meta property="og:title".*?>/i, '')
.replace(/<meta property="og:image".*?>/i, '')
.replace(/<meta property="og:description".*?>/i, '')
.replace(/<meta property="og:url".*?>/i, '')
.replace(
/<\/head>/i,
`
<meta property="og:title" content="${title || 'MILE'}" />
<meta property="og:image" content="${
imageUrl ||
'https://github.com/user-attachments/assets/52ce1a54-3429-4d0d-9801-e7cda913596f'
}" />
<meta name="keywords" content="글쓰기, 글모임, 글, 커뮤니티, 아티클" />
<meta property="og:description" content="링크를 클릭해 마일의 글을 만나보세요!" />
<meta property="og:url" content="${env.VITE_CLIENT_URL}${renderRoute.route}" />
</head>
`,
);
} catch {
renderRoute.html = renderRoute.html.replace(
/<\/head>/i,
`
<meta property="og:title" content="MILE" />
<meta name="keywords" content="글쓰기, 글모임, 글, 커뮤니티, 아티클" />
<meta property="og:description" content="링크를 클릭해 마일의 글을 만나보세요!" />
<meta property="og:url" content="${env.VITE_CLIENT_URL}${renderRoute.route}" />
</head>
`,
);
}
},
}),
]
: []),
svgr(),
visualizer() as PluginOption,
],
};
});