-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathvite.config.ts
147 lines (140 loc) · 4.05 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
import { mkdirSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import legacy from "@vitejs/plugin-legacy";
import react from "@vitejs/plugin-react";
import { type PluginOption, defineConfig } 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";
const isTauri = !!process.env.TAURI_ENV_PLATFORM;
const isPreview = process.env.VITE_SURREALIST_PREVIEW === "true";
const isInstance = process.env.VITE_SURREALIST_INSTANCE === "true";
const ENTRYPOINTS = {
surrealist: "/index.html",
mini_run: "/tools/mini-run.html",
mini_new: "/tools/mini-new.html",
auth_callback: "/tools/auth-callback.html",
cloud_referral: "/tools/cloud-referral.html",
};
const TOOLS = {
"tools/mini-run.html": "mini/run/index.html",
"tools/mini-new.html": "mini/new/index.html",
"tools/auth-callback.html": "cloud/callback/index.html", // TODO rename to cloud/callback
"tools/cloud-referral.html": "cloud/referral/index.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],
}),
legacy({
modernTargets: "since 2021-01-01, not dead",
modernPolyfills: true,
renderLegacyChunks: false,
}),
{
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;
}
}
},
},
];
// Configure compression for web builds
if (!isTauri) {
plugins.push(
compression({
deleteOriginalAssets: true,
threshold: isInstance ? 100 : undefined,
filename: isInstance ? undefined : (id) => id,
include: isInstance
? /assets\/.+\.(html|xml|css|json|js|mjs|svg|wasm)$/
: /\.(wasm)$/,
}),
);
}
return {
plugins,
clearScreen: false,
envPrefix: ["VITE_", "TAURI_"],
server: {
port: 1420,
strictPort: true,
},
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"],
posthog: ["posthog-js"],
codemirror: [
"codemirror",
"@surrealdb/codemirror",
"@surrealdb/lezer",
"@replit/codemirror-indentation-markers",
],
mantime: ["@mantine/core", "@mantine/hooks", "@mantine/notifications"],
surreal: ["surrealdb", "@surrealdb/wasm", "@surrealdb/ql-wasm"],
},
},
},
},
esbuild: {
supported: {
"top-level-await": true, //browsers can handle top-level-await features
},
},
resolve: {
alias: {
"~": fileURLToPath(new URL("src", import.meta.url)),
},
},
css: {
modules: {
localsConvention: "dashesOnly",
},
preprocessorOptions: {
scss: {
additionalData: '@use "~/assets/styles/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.POSTHOG_URL": JSON.stringify("https://eu.i.posthog.com"),
"import.meta.env.POSTHOG_KEY": JSON.stringify(
"phc_BWVuHaJuhnFi3HthLhb9l8opktRrNeFHVnisZdQ5404",
),
},
optimizeDeps: {
exclude: ["@surrealdb/wasm", "@surrealdb/ql-wasm"],
esbuildOptions: {
target: "esnext",
},
},
assetsInclude: ["**/@surrealdb/wasm/dist/*.wasm", "**/@surrealdb/ql-wasm/dist/*.wasm"],
};
});