Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

bundledDev.test.ts

126 lines4.4 KB

1// @effect-diagnostics nodeBuiltinImport:off - builds and executes real dev bundles on disk.
2import * as NodeChildProcess from "node:child_process";
3import * as NodeFSP from "node:fs/promises";
4import * as NodeOS from "node:os";
5import * as NodePath from "node:path";
6import * as NodeURL from "node:url";
7import * as NodeUtil from "node:util";
8​
9import react from "@vitejs/plugin-react";
10import { createServer } from "vite-plus";
11import { expect, it } from "vite-plus/test";
12​
13const execFile = NodeUtil.promisify(NodeChildProcess.execFile);
14​
15it("initializes React refresh before a shared UI chunk runs in bundled dev", async () => {
16 const root = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-bootstrap-"));
17 const output = NodePath.join(root, "output");
18 let resolveBundle!: (files: Map<string, string>) => void;
19 let rejectBundle!: (error: unknown) => void;
20 const bundled = new Promise<Map<string, string>>((resolve, reject) => {
21 resolveBundle = resolve;
22 rejectBundle = reject;
23 });
24 let server: Awaited<ReturnType<typeof createServer>> | undefined;
25​
26 try {
27 await NodeFSP.mkdir(NodePath.join(root, "src/lib"), { recursive: true });
28 await NodeFSP.writeFile(NodePath.join(root, "package.json"), '{"type":"module"}');
29 for (const file of [
30 "index.html",
31 "src/bootstrap.ts",
32 "src/lib/bootError.ts",
33 "src/lib/chunkReloadGuard.ts",
34 ]) {
35 await NodeFSP.copyFile(new URL(`../${file}`, import.meta.url), NodePath.join(root, file));
36 }
37 await NodeFSP.writeFile(
38 NodePath.join(root, "src/shared.tsx"),
39 "export function Shared() { return <div>ready</div>; }",
40 );
41 await NodeFSP.writeFile(
42 NodePath.join(root, "src/main.tsx"),
43 `import { Shared } from "./shared";
44export const startup = Promise.resolve().then(() => globalThis.onStarted(Shared()));`,
45 );
46​
47 server = await createServer({
48 configFile: false,
49 root,
50 publicDir: NodeURL.fileURLToPath(new URL("../public", import.meta.url)),
51 logLevel: "silent",
52 resolve: {
53 alias: { react: NodePath.dirname(NodeURL.fileURLToPath(import.meta.resolve("react"))) },
54 },
55 experimental: { bundledDev: true },
56 plugins: [
57 react(),
58 {
59 name: "capture-bootstrap-bundle",
60 buildEnd(error) {
61 if (error) rejectBundle(error);
62 },
63 generateBundle(_options, bundle) {
64 resolveBundle(
65 new Map(
66 Object.values(bundle)
67 .filter((file) => file.type === "chunk")
68 .map((file) => [file.fileName, file.code]),
69 ),
70 );
71 },
72 },
73 ],
74 build: {
75 rolldownOptions: {
76 experimental: { devMode: { lazy: false } },
77 output: {
78 codeSplitting: {
79 groups: [
80 { name: "vendor", test: /node_modules|@react-refresh/, priority: 10 },
81 { name: "shared-ui", test: /shared\.tsx$/, includeDependenciesRecursively: false },
82 ],
83 },
84 },
85 },
86 },
87 server: { host: "127.0.0.1", port: 0 },
88 });
89 await server.listen();
90 for (const [file, code] of await bundled) {
91 const target = NodePath.join(output, file);
92 await NodeFSP.mkdir(NodePath.dirname(target), { recursive: true });
93 await NodeFSP.writeFile(target, code);
94 }
95​
96 const runner = NodePath.join(output, "check.mjs");
97 await NodeFSP.writeFile(
98 runner,
99 `import assert from "node:assert/strict";
100const started = Promise.withResolvers();
101globalThis.window = globalThis;
102globalThis.addEventListener = () => {};
103globalThis.document = {
104 createElement: () => ({ relList: { supports: () => true } }),
105 getElementById: () => null,
106};
107globalThis.__rolldown_runtime__ = {
108 registerGraph() {},
109 registerModule() {},
110 createModuleHotContext: () => ({ accept() {} }),
111};
112globalThis.onStarted = started.resolve;
113console.error = (_message, error) => started.reject(error);
114await import("./assets/index.js");
115const element = await started.promise;
116assert.equal(element.props.children, "ready");
117assert.equal(typeof window.$RefreshReg$, "function");
118console.log("App started with React refresh ready.");`,
119 );
120 const result = await execFile("node", [runner]);
121 expect(result.stdout).toContain("App started with React refresh ready.");
122 } finally {
123 await server?.close();
124 await NodeFSP.rm(root, { recursive: true, force: true });
125 }
126});