-
Notifications
You must be signed in to change notification settings - Fork 723
/
vite.config.ts
275 lines (259 loc) · 7.9 KB
/
vite.config.ts
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
import { resolve } from "node:path"
import { fileURLToPath } from "node:url"
import legacy from "@vitejs/plugin-legacy"
import { minify as htmlMinify } from "html-minifier-terser"
import { cyan, dim, green } from "kolorist"
import type { PluginOption, ViteDevServer } from "vite"
import { defineConfig, loadEnv } from "vite"
import { analyzer } from "vite-bundle-analyzer"
import mkcert from "vite-plugin-mkcert"
import { VitePWA } from "vite-plugin-pwa"
import { viteRenderBaseConfig } from "./configs/vite.render.config"
import type { env as EnvType } from "./packages/shared/src/env"
import { createDependencyChunksPlugin } from "./plugins/vite/deps"
import { htmlInjectPlugin } from "./plugins/vite/html-inject"
import manifestPlugin from "./plugins/vite/manifest"
import { createPlatformSpecificImportPlugin } from "./plugins/vite/specific-import"
const __dirname = fileURLToPath(new URL(".", import.meta.url))
const isCI = process.env.CI === "true" || process.env.CI === "1"
const ROOT = "./apps/renderer"
const devPrint = (): PluginOption => ({
name: "dev-print",
configureServer(server: ViteDevServer) {
const _printUrls = server.printUrls
server.printUrls = () => {
_printUrls()
console.info(
` ${green("➜")} ${dim("Production debug")}: ${cyan(
"https://app.follow.is/__debug_proxy",
)}`,
)
console.info(
` ${green("➜")} ${dim("Development debug")}: ${cyan(
"https://dev.follow.is/__debug_proxy",
)}`,
)
}
},
})
export default ({ mode }) => {
const env = loadEnv(mode, process.cwd())
const typedEnv = env as typeof EnvType
return defineConfig({
...viteRenderBaseConfig,
root: ROOT,
envDir: resolve(__dirname, "."),
build: {
outDir: resolve(__dirname, "out/web"),
target: "ES2022",
sourcemap: isCI,
rollupOptions: {
input: {
main: resolve(ROOT, "/index.html"),
__debug_proxy: resolve(ROOT, "/__debug_proxy.html"),
},
},
},
server: {
port: 2233,
watch: {
ignored: ["**/dist/**", "**/out/**", "**/public/**", ".git/**"],
},
...(env.VITE_DEV_PROXY
? {
proxy: {
[env.VITE_DEV_PROXY]: {
target: env.VITE_DEV_PROXY_TARGET,
changeOrigin: true,
rewrite: (path) => path.replace(new RegExp(`^${env.VITE_DEV_PROXY}`), ""),
},
},
}
: {}),
},
resolve: {
alias: {
...viteRenderBaseConfig.resolve?.alias,
"@follow/logger": resolve(__dirname, "./packages/logger/web.ts"),
},
},
plugins: [
...((viteRenderBaseConfig.plugins ?? []) as any),
VitePWA({
strategies: "injectManifest",
srcDir: "src",
filename: "sw.ts",
registerType: "prompt",
injectRegister: false,
injectManifest: {
globPatterns: [
"**/*.{js,json,css,html,txt,svg,png,ico,webp,woff,woff2,ttf,eot,otf,wasm}",
],
manifestTransforms: [
(manifest) => {
return {
manifest,
warnings: [],
additionalManifestEntries: [
{
url: "/sw.js?pwa=true",
revision: null,
},
],
}
},
],
},
manifest: {
theme_color: "#000000",
name: "Follow",
display: "standalone",
background_color: "#ffffff",
icons: [
{
src: "pwa-64x64.png",
sizes: "64x64",
type: "image/png",
},
{
src: "pwa-192x192.png",
sizes: "192x192",
type: "image/png",
},
{
src: "pwa-512x512.png",
sizes: "512x512",
type: "image/png",
},
{
src: "maskable-icon-512x512.png",
sizes: "512x512",
type: "image/png",
purpose: "maskable",
},
],
},
devOptions: {
enabled: false,
navigateFallback: "index.html",
suppressWarnings: true,
type: "module",
},
}),
mode !== "development" &&
legacy({
targets: "defaults",
renderLegacyChunks: false,
modernTargets: ">0.3%, last 2 versions, Firefox ESR, not dead",
modernPolyfills: [
// https://unpkg.com/browse/[email protected]/modules/
"es.promise.with-resolvers",
],
}),
htmlInjectPlugin(typedEnv),
mkcert(),
devPrint(),
createDependencyChunksPlugin([
// React framework
["react", "react-dom"],
["react-error-boundary", "react-dom/server", "react-router-dom"],
// Data Statement
["zustand", "jotai", "use-context-selector", "immer", "dexie"],
// Remark
[
"remark-directive",
"remark-gfm",
"remark-parse",
"remark-stringify",
"remark-rehype",
"@microflash/remark-callout-directives",
"remark-gh-alerts",
],
// Rehype
[
"rehype-parse",
"rehype-sanitize",
"rehype-stringify",
"rehype-infer-description-meta",
"hast-util-to-jsx-runtime",
"hast-util-to-text",
"react-shadow",
],
["vfile", "unified"],
["es-toolkit/compat"],
["framer-motion"],
["clsx", "tailwind-merge", "class-variance-authority"],
[
"@radix-ui/react-dialog",
"@radix-ui/react-avatar",
"@radix-ui/react-checkbox",
"@radix-ui/react-context",
"@radix-ui/react-dropdown-menu",
"@radix-ui/react-hover-card",
"@radix-ui/react-label",
"@radix-ui/react-popover",
"@radix-ui/react-radio-group",
"@radix-ui/react-scroll-area",
"@radix-ui/react-select",
"@radix-ui/react-slider",
"@radix-ui/react-slot",
"@radix-ui/react-switch",
"@radix-ui/react-tabs",
"@radix-ui/react-toast",
"@radix-ui/react-tooltip",
"@headlessui/react",
],
["i18next", "i18next-browser-languagedetector", "react-i18next"],
// Data query
[
"@tanstack/react-query",
"@tanstack/react-query-persist-client",
"@tanstack/query-sync-storage-persister",
],
["tldts"],
["@sentry/react", "@openpanel/web"],
["zod", "react-hook-form", "@hookform/resolvers"],
]),
createPlatformSpecificImportPlugin(false),
manifestPlugin(),
htmlPlugin(typedEnv),
process.env.analyzer && analyzer(),
],
define: {
...viteRenderBaseConfig.define,
ELECTRON: "false",
},
})
}
function checkBrowserSupport() {
if (!("findLastIndex" in Array.prototype) || !("structuredClone" in window)) {
window.alert(
"Follow is not compatible with your browser because your browser version is too old. You can download and use the Follow app or continue using it with the latest browser.",
)
window.location.href = "https://follow.is/download"
}
}
const htmlPlugin: (env: any) => PluginOption = () => {
return {
name: "html-transform",
enforce: "post",
transformIndexHtml(html) {
return htmlMinify(
html.replace(
"<!-- Check Browser Script Inject -->",
`<script>${checkBrowserSupport.toString()}; checkBrowserSupport()</script>`,
),
{
removeComments: true,
html5: true,
minifyJS: true,
minifyCSS: true,
removeTagWhitespace: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
collapseInlineTagWhitespace: true,
},
)
},
}
}