forked from coder/mux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
146 lines (130 loc) · 4.38 KB
/
vite.config.ts
File metadata and controls
146 lines (130 loc) · 4.38 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
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import topLevelAwait from "vite-plugin-top-level-await";
import svgr from "vite-plugin-svgr";
import tailwindcss from "@tailwindcss/vite";
import path from "path";
import { fileURLToPath } from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const disableMermaid = process.env.VITE_DISABLE_MERMAID === "1";
// Vite server configuration (for dev-server remote access)
const devServerHost = process.env.MUX_VITE_HOST ?? "127.0.0.1"; // Secure by default
const devServerPort = Number(process.env.MUX_VITE_PORT ?? "5173");
const previewPort = Number(process.env.MUX_VITE_PREVIEW_PORT ?? "4173");
const alias: Record<string, string> = {
"@": path.resolve(__dirname, "./src"),
};
if (disableMermaid) {
alias["mermaid"] = path.resolve(__dirname, "./src/mocks/mermaidStub.ts");
}
// React Compiler configuration
// Automatically optimizes React components through memoization
// See: https://react.dev/learn/react-compiler
const reactCompilerConfig = {
target: "18", // Target React 18 (requires react-compiler-runtime package)
};
// Babel plugins configuration (shared between dev and production)
const babelPlugins = [["babel-plugin-react-compiler", reactCompilerConfig]];
// Base plugins for both dev and production
const basePlugins = [
svgr(),
react({
babel: {
plugins: babelPlugins,
},
}),
tailwindcss(),
];
export default defineConfig(({ mode }) => ({
// This prevents mermaid initialization errors in production while allowing dev to work
plugins: mode === "development" ? [...basePlugins, topLevelAwait()] : basePlugins,
resolve: {
alias,
},
base: "./",
build: {
outDir: "dist",
assetsDir: ".",
emptyOutDir: false,
sourcemap: true,
minify: "esbuild",
rollupOptions: {
input: {
main: path.resolve(__dirname, "index.html"),
terminal: path.resolve(__dirname, "terminal.html"),
},
output: {
format: "es",
inlineDynamicImports: false,
sourcemapExcludeSources: false,
manualChunks(id) {
const normalizedId = id.split(path.sep).join("/");
if (normalizedId.includes("node_modules/ai-tokenizer/encoding/")) {
const chunkName = path.basename(id, path.extname(id));
return `tokenizer-encoding-${chunkName}`;
}
if (normalizedId.includes("node_modules/ai-tokenizer/")) {
return "tokenizer-base";
}
return undefined;
},
},
},
chunkSizeWarningLimit: 2000,
target: "esnext",
},
worker: {
format: "es",
plugins: () => [topLevelAwait()],
},
server: {
host: devServerHost, // Configurable via MUX_VITE_HOST (defaults to 127.0.0.1 for security)
port: devServerPort,
strictPort: true,
allowedHosts: true, // Allow all hosts for dev server (secure by default via MUX_VITE_HOST)
sourcemapIgnoreList: () => false, // Show all sources in DevTools
watch: {
// Ignore node_modules to drastically reduce file handle usage
ignored: ['**/node_modules/**', '**/dist/**', '**/.git/**'],
// Use polling on Windows to avoid file handle exhaustion
// This is slightly less efficient but much more stable
usePolling: process.platform === 'win32',
// If using polling, set a reasonable interval (in milliseconds)
interval: 1000,
// Limit the depth of directory traversal
depth: 3,
// Additional options for Windows specifically
...(process.platform === 'win32' && {
// Increase the binary interval for better Windows performance
binaryInterval: 1000,
// Use a more conservative approach to watching
awaitWriteFinish: {
stabilityThreshold: 500,
pollInterval: 100
}
})
},
hmr: {
// Configure HMR to use the correct host for remote access
host: devServerHost,
port: devServerPort,
protocol: "ws",
},
},
preview: {
host: "127.0.0.1",
port: previewPort,
strictPort: true,
allowedHosts: ["localhost", "127.0.0.1"],
},
optimizeDeps: {
esbuildOptions: {
target: "esnext",
},
// Include only what's actually imported to reduce scanning
entries: ['src/**/*.{ts,tsx}'],
// Force re-optimize dependencies
force: false,
},
assetsInclude: ["**/*.wasm"],
}));