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