-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathvite.renderer.config.ts
More file actions
139 lines (123 loc) · 3.83 KB
/
vite.renderer.config.ts
File metadata and controls
139 lines (123 loc) · 3.83 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
import { defineConfig, type Plugin, type UserConfig } from "vite";
import { spawnSync, spawn } from "node:child_process";
import { resolve } from "node:path";
import { onExit } from "@open-orpheus/lifecycle";
const GUI_DIR = resolve(__dirname, "gui");
// Port for the SvelteKit dev server spawned alongside the dummy Vite dev server.
const SVELTEKIT_DEV_PORT = 5174;
function getPackageManagerCommand() {
const npmExecPath = process.env.npm_execpath;
if (npmExecPath) {
return { command: process.execPath, baseArgs: [npmExecPath] };
}
return { command: "pnpm", baseArgs: [] };
}
function runPackageManager(args: string[]) {
const { command, baseArgs } = getPackageManagerCommand();
return spawn(command, [...baseArgs, ...args], {
cwd: GUI_DIR,
stdio: ["ignore", "pipe", "pipe"],
shell: false,
});
}
function runPackageManagerSync(args: string[]) {
const { command, baseArgs } = getPackageManagerCommand();
const result = spawnSync(command, [...baseArgs, ...args], {
cwd: GUI_DIR,
stdio: ["ignore", "inherit", "inherit"],
shell: false,
});
if (result.error) throw result.error;
if (typeof result.status === "number" && result.status !== 0) {
throw new Error(
`Package manager command failed with exit code ${result.status}`
);
}
}
/**
* Bridges Electron Forge's VitePlugin renderer lifecycle to the SvelteKit
* project in `gui/`.
*
* - Build mode: runs `pnpm run build` inside `gui/`. SvelteKit's adapter-static
* writes directly to `.vite/build/gui`, which is exactly where Forge expects
* the renderer output.
* - Dev mode (serve): spawns `pnpm run dev` inside `gui/` on a fixed port and
* proxies all requests from the Forge-managed Vite dev server to it.
*/
function svelteKitPlugin(): Plugin {
let devProcess: ReturnType<typeof spawn> | null = null;
onExit(() => {
if (!devProcess) {
return;
}
// We don't want noises when killing the dev server
devProcess.stdout?.unpipe(process.stdout);
devProcess.stderr?.unpipe(process.stderr);
devProcess.kill();
});
return {
name: "sveltekit-bridge",
config(_, { command }): UserConfig {
if (command === "serve") {
return {
server: {
proxy: {
"/": {
target: `http://localhost:${SVELTEKIT_DEV_PORT}`,
changeOrigin: true,
ws: true,
},
},
},
};
}
return {};
},
configureServer() {
devProcess = runPackageManager([
"run",
"dev",
"--port",
String(SVELTEKIT_DEV_PORT),
]);
devProcess.stdout?.pipe(process.stdout);
devProcess.stderr?.pipe(process.stderr);
},
buildStart() {
if (!this.meta.watchMode) {
runPackageManagerSync(["run", "build"]);
}
},
// Rollup requires at least one input entry; resolve this virtual module to
// an empty module so the Rollup pass produces no real output of its own.
resolveId(id) {
if (id === "virtual:sveltekit-bridge")
return "\0virtual:sveltekit-bridge";
},
load(id) {
if (id === "\0virtual:sveltekit-bridge") return "export default {}";
},
generateBundle(_, bundle) {
for (const key of Object.keys(bundle)) {
if (
(bundle[key] as { facadeModuleId?: string }).facadeModuleId ===
"\0virtual:sveltekit-bridge"
) {
delete bundle[key];
}
}
},
};
}
export default defineConfig({
plugins: [svelteKitPlugin()],
build: {
// SvelteKit writes its own output to .vite/build/gui via adapter-static.
// Vite's own Rollup pass has nothing to bundle, so keep emptyOutDir off to
// preserve what SvelteKit generated.
emptyOutDir: false,
rollupOptions: {
input: { _sveltekit: "virtual:sveltekit-bridge" },
},
},
});