Open History to see the last commit
1import * as NodeChildProcess from "node:child_process";
2import * as NodeFSP from "node:fs/promises";
3import * as NodeOS from "node:os";
4import * as NodePath from "node:path";
5import * as NodeVM from "node:vm";
6import * as NodeURL from "node:url";
7import { build } from "vite-plus/pack";
8import { assert, it } from "vite-plus/test";
9
10import desktopConfig from "../vite.config.ts";
11
12it("keeps lazy Linux imports and worker bundles from executing desktop startup twice", async () => {
13 const directory = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-desktop-bundle-"));
14 try {
15 const workerEntries = [
16 "src/backend/DesktopBackendRecoveryWorker.ts",
17 "src/electron/WindowsForegroundFocusWorker.ts",
18 "src/snapShot/GlobalShiftShortcutWorker.ts",
19 "src/snapShot/RegionSnapShotWorker.ts",
20 "src/snapShot/SnapShotAccessibilityWorker.ts",
21 ];
22 await Promise.all([
23 NodeFSP.mkdir(NodePath.join(directory, "src/backend"), { recursive: true }),
24 NodeFSP.mkdir(NodePath.join(directory, "src/electron"), { recursive: true }),
25 NodeFSP.mkdir(NodePath.join(directory, "src/snapShot"), { recursive: true }),
26 ]);
27 await Promise.all([
28 NodeFSP.writeFile(
29 NodePath.join(directory, "src/main.ts"),
30 `import { shared } from "./shared.ts";
31process.emit("startup", shared.value);
32void import("./linux.ts").then(({ result }) => process.emit("ready", result));`,
33 ),
34 NodeFSP.writeFile(
35 NodePath.join(directory, "src/shared.ts"),
36 "export const shared = { value: 42 };",
37 ),
38 NodeFSP.writeFile(
39 NodePath.join(directory, "src/linux.ts"),
40 'import { shared } from "./shared.ts"; export const result = shared.value + 1;',
41 ),
42 ...workerEntries.map((entry) =>
43 NodeFSP.writeFile(
44 NodePath.join(directory, entry),
45 'import { shared } from "../shared.ts"; process.emit("worker", shared.value);',
46 ),
47 ),
48 ]);
49 assert.ok(Array.isArray(desktopConfig.pack));
50 const fixtureEntries = new Set(["src/main.ts", ...workerEntries]);
51 for (const packConfig of desktopConfig.pack) {
52 if (!Array.isArray(packConfig.entry)) continue;
53 if (!packConfig.entry.some((entry) => fixtureEntries.has(entry))) continue;
54 await build({
55 ...packConfig,
56 config: false,
57 cwd: directory,
58 tsconfig: false,
59 sourcemap: false,
60 onSuccess: undefined,
61 logLevel: "silent",
62 });
63 }
64
65 const outputDirectory = NodePath.join(directory, "dist-electron");
66 const filenames = (await NodeFSP.readdir(outputDirectory, { recursive: true })).filter(
67 (filename) => filename.endsWith(".cjs"),
68 );
69 const sources = new Map(
70 await Promise.all(
71 filenames.map(async (filename) => {
72 const path = NodePath.join(outputDirectory, filename);
73 return [path, await NodeFSP.readFile(path, "utf8")];
74 }),
75 ),
76 );
77 const modules = new Map();
78 const startups = [];
79 const workers = [];
80 const ready = Promise.withResolvers();
81 const load = (filename, cacheModule = true) => {
82 const cached = modules.get(filename);
83 if (cached) return cached.exports;
84 const module = { exports: {} };
85 if (cacheModule) modules.set(filename, module);
86 const source = sources.get(filename);
87 assert.ok(source, `Missing bundle: ${filename}`);
88 NodeVM.runInNewContext(source, {
89 exports: module.exports,
90 module,
91 require: (specifier) => load(NodePath.resolve(NodePath.dirname(filename), specifier)),
92 process: {
93 emit: (event, value) => {
94 if (event === "startup") startups.push(value);
95 if (event === "worker") workers.push(value);
96 if (event === "ready") ready.resolve(value);
97 },
98 },
99 });
100 return module.exports;
101 };
102
103 load(NodePath.join(outputDirectory, "main.cjs"), false);
104 assert.equal(await ready.promise, 43);
105 assert.deepEqual(startups, [42]);
106 for (const entry of workerEntries) {
107 load(NodePath.join(outputDirectory, entry.replace(/^src\//, "").replace(/\.ts$/, ".cjs")));
108 }
109 assert.deepEqual(
110 workers,
111 workerEntries.map(() => 42),
112 );
113 assert.deepEqual(startups, [42]);
114 } finally {
115 await NodeFSP.rm(directory, { recursive: true, force: true });
116 }
117});
118
119it("loads the emitted packaged boot entry and backend cache preload", async () => {
120 const directory = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-desktop-boot-"));
121 try {
122 const entries = ["src/boot.ts", "src/compileCache.ts"];
123 assert.ok(Array.isArray(desktopConfig.pack));
124 for (const packConfig of desktopConfig.pack) {
125 if (!Array.isArray(packConfig.entry)) continue;
126 if (!packConfig.entry.some((entry) => entries.includes(entry))) continue;
127 await build({
128 ...packConfig,
129 entry: packConfig.entry.map((entry) =>
130 NodeURL.fileURLToPath(new URL(`../${entry}`, import.meta.url)),
131 ),
132 outDir: NodePath.join(directory, "dist-electron"),
133 config: false,
134 cwd: directory,
135 tsconfig: false,
136 sourcemap: false,
137 onSuccess: undefined,
138 logLevel: "silent",
139 });
140 }
141 const outputDirectory = NodePath.join(directory, "dist-electron");
142 const fixture = `console.log(require('node:module').getCompileCacheDir() ? 'cached' : 'uncached');`;
143 await NodeFSP.writeFile(NodePath.join(outputDirectory, "main.cjs"), fixture);
144 await NodeFSP.writeFile(
145 NodePath.join(outputDirectory, "backend.mjs"),
146 `import { getCompileCacheDir } from 'node:module'; console.log(getCompileCacheDir() ? 'cached' : 'uncached');`,
147 );
148 for (const disabled of [false, true]) {
149 for (const args of [
150 [NodePath.join(outputDirectory, "boot.cjs")],
151 [
152 "--require",
153 NodePath.join(outputDirectory, "compileCache.cjs"),
154 NodePath.join(outputDirectory, "backend.mjs"),
155 ],
156 ]) {
157 const child = NodeChildProcess.spawnSync(process.execPath, args, {
158 encoding: "utf8",
159 env: {
160 ...process.env,
161 APPIMAGE: "",
162 NODE_COMPILE_CACHE: undefined,
163 NODE_DISABLE_COMPILE_CACHE: disabled ? "1" : undefined,
164 XDG_CACHE_HOME: directory,
165 TMPDIR: directory,
166 TEMP: directory,
167 TMP: directory,
168 },
169 });
170 assert.equal(child.status, 0, child.stderr);
171 assert.equal(child.stdout.trim(), disabled ? "uncached" : "cached");
172 }
173 }
174 } finally {
175 await NodeFSP.rm(directory, { recursive: true, force: true });
176 }
177});