-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
203 lines (195 loc) · 7.35 KB
/
Copy pathvite.config.ts
File metadata and controls
203 lines (195 loc) · 7.35 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
import { resolve } from "node:path";
import { defineConfig, loadEnv, type Plugin } from "vite";
import babel from "@rolldown/plugin-babel";
import react, { reactCompilerPreset } from "@vitejs/plugin-react";
import { lingui } from "@lingui/vite-plugin";
const compilerPreset = reactCompilerPreset();
const DEFAULT_POSTHOG_HOST = "https://us.i.posthog.com";
function readEnvValue(env: Record<string, string>, key: string): string {
return (env[key] ?? process.env[key] ?? "").trim();
}
function buildPostHogEnvDefines(mode: string): Record<string, string> {
const env = loadEnv(mode, process.cwd(), "");
const posthogKey = readEnvValue(env, "POSTHOG_KEY");
const posthogHost = readEnvValue(env, "POSTHOG_HOST") || DEFAULT_POSTHOG_HOST;
const posthogEnabled = readEnvValue(env, "POSTHOG_ENABLED") || "1";
const posthogEnableDev = readEnvValue(env, "POSTHOG_ENABLE_DEV") || "0";
return {
"import.meta.env.VITE_POSTHOG_ENABLE_DEV": JSON.stringify(posthogEnableDev),
"import.meta.env.VITE_POSTHOG_ENABLED": JSON.stringify(posthogEnabled),
"import.meta.env.VITE_POSTHOG_HOST": JSON.stringify(posthogHost),
"import.meta.env.VITE_POSTHOG_KEY": JSON.stringify(posthogKey),
};
}
// Inline ternary instead of importing src/shared/channel.normalizeChannel —
// keeps config loading uniform with tsdown.config.ts. Parity is pinned by
// src/shared/channel.config-parity.test.ts.
const lightcodeChannel = process.env.LIGHTCODE_CHANNEL === "nightly" ? "nightly" : "stable";
// Dev-only: connect the renderer to the standalone React DevTools app for
// inspecting/profiling rerenders. The React DevTools *browser extension* uses
// `chrome.scripting` (Manifest V3), which Electron doesn't implement — under
// Electron the extension panels load but never find the React tree
// (facebook/react#25843). The supported alternative is the standalone
// `react-devtools` app (run via `pnpm devtools`), which serves a backend on
// :8097 that the page connects to. The hook must be installed *before* React
// loads, so we inject a classic <script> at the top of <head>; the deferred
// `main.tsx` module script runs after it. Opt-in via LIGHTCODE_REACT_DEVTOOLS=1
// (set by the `dev:devtools` script) so a normal `pnpm dev` stays noise-free
// when the standalone app isn't running.
function reactDevtoolsStandalone(): Plugin {
return {
name: "lightcode:react-devtools-standalone",
apply: "serve",
transformIndexHtml() {
if (process.env.LIGHTCODE_REACT_DEVTOOLS !== "1") {
return;
}
return [
{
tag: "script",
attrs: { src: "http://localhost:8097" },
injectTo: "head-prepend",
},
];
},
};
}
function resizeObserverLoopErrorFilter(): Plugin {
return {
name: "lightcode:resize-observer-loop-error-filter",
apply: "serve",
transformIndexHtml() {
return [
{
tag: "script",
children: `
(function () {
var resizeObserverLoopMessages = {
"ResizeObserver loop completed with undelivered notifications.": true,
"ResizeObserver loop limit exceeded": true
};
window.addEventListener("error", function (event) {
var message =
event && event.error && typeof event.error.message === "string"
? event.error.message
: event && typeof event.message === "string"
? event.message
: "";
if (!resizeObserverLoopMessages[message]) return;
event.preventDefault();
event.stopImmediatePropagation();
}, { capture: true });
})();
`.trim(),
injectTo: "head-prepend",
},
];
},
};
}
export default defineConfig(({ mode }) => ({
plugins: [
resizeObserverLoopErrorFilter(),
reactDevtoolsStandalone(),
react(),
// The Lingui macro must expand BEFORE the React Compiler. Babel applies
// `plugins` ahead of `presets`, so listing the macro as a plugin (with the
// compiler as a preset) guarantees that order within this single Babel pass.
// We use the Babel macro (not the SWC plugin) because this project transforms
// with Babel, sidestepping the SWC-plugin/runtime version-matching pitfalls.
babel({ plugins: ["@lingui/babel-plugin-lingui-macro"], presets: [compilerPreset] }),
// Compiles `.po` catalog imports into runtime message modules on the fly,
// so we never need a separate `lingui compile` step for the app build.
lingui(),
],
base: "./",
define: {
...buildPostHogEnvDefines(mode),
__LIGHTCODE_CHANNEL__: JSON.stringify(lightcodeChannel),
},
resolve: {
tsconfigPaths: true,
alias: {
"~file-icons": resolve(__dirname, "node_modules/material-icon-theme/icons"),
},
},
build: {
outDir: "dist/renderer",
emptyOutDir: true,
sourcemap: "hidden",
// Filter modulePreload so the heaviest async chunks (shiki grammars,
// @git-diff-view, xterm) are not parsed by V8 at startup. They load on
// demand when the code path that needs them runs (first code block,
// first git overlay open, first terminal).
modulePreload: {
resolveDependencies: (_filename, deps) =>
deps.filter((dep) => !/(?:^|\/)(shiki-|git-diff-|xterm-|vendor-)/.test(dep)),
},
rolldownOptions: {
output: {
minify: {
compress: {
dropConsole: true,
dropDebugger: true,
},
},
codeSplitting: {
groups: [
{
name: "xterm",
test: /[\\/]node_modules[\\/]@xterm[\\/]/,
priority: 50,
},
{
name: "git-diff",
test: /[\\/]node_modules[\\/]@git-diff-view[\\/]/,
priority: 45,
},
{
name: "monaco",
test: /[\\/]node_modules[\\/](@monaco-editor|monaco-editor)[\\/]/,
priority: 40,
},
{
// Shiki engine + bundle-full glue, BUT not its grammars/themes.
// shiki/bundle-full uses per-language dynamic imports
// (`() => import("@shikijs/langs/typescript")`); leaving
// langs/themes out of any group lets rolldown emit them as
// separate per-language chunks, so V8 only parses the grammars
// actually rendered.
name: "shiki",
test: /[\\/]node_modules[\\/](shiki[\\/]|@shikijs[\\/](?:core|engine-|types|vscode-))/,
priority: 38,
},
{
name: "ui",
test: /[\\/]node_modules[\\/](@heroui|react-aria|@react-stately|@react-types|tailwind-merge|tailwind-variants)[\\/]/,
priority: 35,
},
{
name: "framework",
test: /[\\/]node_modules[\\/](react|react-dom|scheduler|zustand|zod)[\\/]/,
priority: 30,
},
{
// Catch-all for everything not handled above. Excludes
// @shikijs/langs and @shikijs/themes so each grammar/theme
// becomes its own auto-chunk (one per file actually used).
name: "vendor",
test: (id: string) =>
/[\\/]node_modules[\\/]/.test(id) &&
!/[\\/]@shikijs[\\/](?:langs|themes)[\\/]/.test(id),
priority: 10,
},
],
},
},
},
},
server: {
forwardConsole: true,
host: "127.0.0.1",
port: 3100,
strictPort: true,
},
}));