-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathvite.config.ts
More file actions
207 lines (195 loc) · 6.2 KB
/
Copy pathvite.config.ts
File metadata and controls
207 lines (195 loc) · 6.2 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
import { mkdirSync, readFileSync, realpathSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import react from "@vitejs/plugin-react";
import { defineConfig, type PluginOption } from "vite";
import { compression } from "vite-plugin-compression2";
import { ViteImageOptimizer as images } from "vite-plugin-image-optimizer";
import { Mode, plugin as markdown } from "vite-plugin-markdown";
import { surreal, version } from "./package.json";
/** Project root (directory containing this config). */
const projectDir = dirname(fileURLToPath(import.meta.url));
// bun link: share one copy of @surrealdb/ui peers (react, mantine, codemirror, …)
const uiRoot = realpathSync(resolve(projectDir, "node_modules/@surrealdb/ui"));
const uiPeers = Object.keys(
JSON.parse(readFileSync(resolve(uiRoot, "package.json"), "utf8")).peerDependencies ?? {},
);
/**
* Some dependencies import Node builtins in code paths that still run in the
* browser (`dom-to-svg` → PostCSS, `@surrealdb/ql-wasm` → optional `node:crypto`).
* Vite replaces them with stubs that log on access; alias small browser-safe
* implementations instead.
* @see https://github.com/vitejs/vite/issues/9200
*/
const browserNodeBuiltinAliases = {
path: resolve(projectDir, "node_modules/path-browserify/index.js"),
"node:path": resolve(projectDir, "node_modules/path-browserify/index.js"),
fs: resolve(projectDir, "shims/node-fs-postcss-shim.ts"),
"node:fs": resolve(projectDir, "shims/node-fs-postcss-shim.ts"),
url: resolve(projectDir, "shims/node-url-postcss-shim.ts"),
"node:url": resolve(projectDir, "shims/node-url-postcss-shim.ts"),
"source-map-js": resolve(projectDir, "node_modules/source-map-js/source-map.js"),
"node:crypto": resolve(projectDir, "shims/node-crypto-web-shim.ts"),
} as const;
const isTauri = !!process.env.TAURI_ENV_PLATFORM;
const isCompress = process.env.VITE_SURREALIST_COMPRESS !== "false";
const isPreview = process.env.VITE_SURREALIST_PREVIEW === "true";
const isDocker = process.env.VITE_SURREALIST_DOCKER === "true";
const ENTRYPOINTS: Record<string, string> = {
surrealist: "/index.html",
mini_embed: "/tools/mini-embed.html",
auth_return: "/tools/auth-return.html",
auth_launch: "/tools/auth-launch.html",
cloud_callback: "/tools/cloud-callback.html",
cloud_referral: "/tools/cloud-referral.html",
};
const TOOLS: Record<string, string> = {
"tools/mini-embed.html": "mini/run/index.html",
"tools/auth-return.html": "auth/return/index.html",
"tools/auth-launch.html": "auth/launch/index.html",
"tools/cloud-callback.html": "cloud/callback/index.html",
"tools/cloud-referral.html": "cloud/referral/index.html",
};
const REWRITES: Record<string, string> = {
"/auth/return": "/tools/auth-return.html",
"/auth/launch": "/tools/auth-launch.html",
"/cloud/callback": "/tools/cloud-callback.html",
};
export default defineConfig(({ mode }) => {
// Required because we cannot pass a custom mode to tauri build
mode = isPreview ? "preview" : mode;
// Define base plugins
const plugins: PluginOption[] = [
images(),
react(),
markdown({
mode: [Mode.HTML],
}),
{
name: "rename-html",
enforce: "post",
generateBundle(_, bundle) {
for (const chunk of Object.values(bundle)) {
if (TOOLS[chunk.fileName]) {
const endpoint = TOOLS[chunk.fileName];
const target = dirname(resolve("dist", endpoint));
mkdirSync(target, { recursive: true });
chunk.fileName = endpoint;
}
}
},
},
{
name: "rewrite-routes",
configureServer(server) {
server.middlewares.use((req, _res, next) => {
if (req.url) {
const [pathname, query] = req.url.split("?", 2);
const target = REWRITES[pathname];
if (target) {
req.url = query ? `${target}?${query}` : target;
}
}
next();
});
},
},
];
// Configure compression for web builds
if (!isTauri && isCompress) {
console.log("Compressing assets...");
plugins.push(
compression({
deleteOriginalAssets: true,
threshold: isDocker ? 100 : undefined,
filename: isDocker ? undefined : (id) => id,
include: isDocker
? /assets\/.+\.(html|xml|css|json|js|mjs|svg|wasm)$/
: /\.(wasm)$/,
}),
);
} else {
console.log("Skipping compression for build");
}
return {
plugins,
clearScreen: false,
envPrefix: ["VITE_", "TAURI_"],
server: {
port: 1420,
strictPort: true,
fs: {
allow: [projectDir, uiRoot],
},
},
build: {
target: "es2020",
minify: process.env.TAURI_DEBUG ? false : "esbuild",
sourcemap: !!process.env.TAURI_DEBUG,
rollupOptions: {
input: !isTauri ? ENTRYPOINTS : undefined,
output: {
experimentalMinChunkSize: 5000,
manualChunks: {
react: ["react", "react-dom"],
codemirror: [
"codemirror",
"@surrealdb/codemirror",
"@surrealdb/lezer",
"@replit/codemirror-indentation-markers",
],
mantime: ["@mantine/core", "@mantine/hooks", "@mantine/notifications"],
surreal: [
"surrealdb",
"@surrealdb/wasm",
"@surrealdb/ql-wasm-2",
"@surrealdb/ql-wasm-3",
],
},
},
},
},
esbuild: {
supported: {
"top-level-await": true, //browsers can handle top-level-await features
},
},
resolve: {
dedupe: ["react", "react-dom", ...uiPeers],
alias: {
...browserNodeBuiltinAliases,
"~": fileURLToPath(new URL("src", import.meta.url)),
},
},
css: {
modules: {
localsConvention: "dashesOnly",
},
preprocessorOptions: {
scss: {
additionalData: "@use '@surrealdb/ui/mixins' as *;",
api: "modern-compiler",
},
},
},
define: {
"import.meta.env.DATE": JSON.stringify(new Date()),
"import.meta.env.VERSION": JSON.stringify(version),
"import.meta.env.SDB_VERSION": JSON.stringify(surreal),
"import.meta.env.MODE": JSON.stringify(mode),
"import.meta.env.GTM_ID": JSON.stringify("G-PVD8NEJ3Z2"),
},
optimizeDeps: {
include: ["path-browserify", "@surrealdb/ui"],
exclude: ["@surrealdb/wasm", "@surrealdb/ql-wasm-2", "@surrealdb/ql-wasm-3"],
esbuildOptions: {
target: "esnext",
},
},
assetsInclude: [
"**/@surrealdb/wasm/dist/*.wasm",
"**/@surrealdb/ql-wasm-2/dist/*.wasm",
"**/@surrealdb/ql-wasm-3/dist/*.wasm",
],
};
});