The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import path from "node:path"; |
| 2 | import type { MinimalPluginContext } from "rolldown"; |
| 3 | import type * as vite from "vite"; |
| 4 | import { assert, describe, expect, it } from "vitest"; |
| 5 | import { optionsPlugin } from "../plugins/options.ts"; |
| 6 | |
| 7 | const callViteConfig = async ( |
| 8 | plugin: vite.Plugin | null, |
| 9 | userConfig: vite.UserConfig, |
| 10 | ): Promise<vite.UserConfig> => { |
| 11 | assert(plugin, "plugin is not defined"); |
| 12 | const hook = plugin.config; |
| 13 | assert(typeof hook === "function", "plugin.config is not a function"); |
| 14 | const result = await hook.call({ meta: {} } as never, userConfig, { |
| 15 | command: "build", |
| 16 | mode: "production", |
| 17 | } as never); |
| 18 | assert(result, "config hook returned nothing"); |
| 19 | return result as vite.UserConfig; |
| 20 | }; |
| 21 | |
| 22 | const NO_HTML_ROOT = path.resolve("test/fixtures"); |
| 23 | const HTML_ROOT = path.resolve("test/fixtures/client-html"); |
| 24 | |
| 25 | interface FakeEnvironment { |
| 26 | name: string; |
| 27 | isBuilt?: boolean; |
| 28 | /** Defaults to a directory with no `index.html`. */ |
| 29 | root?: string; |
| 30 | build?: { |
| 31 | rollupOptions?: { input?: unknown }; |
| 32 | rolldownOptions?: { input?: unknown }; |
| 33 | }; |
| 34 | } |
| 35 | |
| 36 | /** Records the environments the resolved `buildApp` actually builds. */ |
| 37 | const runBuildApp = async ( |
| 38 | config: vite.UserConfig, |
| 39 | environments: Array<FakeEnvironment>, |
| 40 | ): Promise<Array<string>> => { |
| 41 | const buildApp = config.builder?.buildApp; |
| 42 | assert(typeof buildApp === "function", "buildApp is not a function"); |
| 43 | const built: Array<string> = []; |
| 44 | await buildApp({ |
| 45 | config: { root: NO_HTML_ROOT, publicDir: false }, |
| 46 | environments: Object.fromEntries( |
| 47 | environments.map(({ name, isBuilt = false, root = NO_HTML_ROOT, build = {} }) => [ |
| 48 | name, |
| 49 | { name, isBuilt, config: { root, build: { outDir: `dist/${name}`, ...build } } }, |
| 50 | ]), |
| 51 | ), |
| 52 | build: async (environment: { name: string }) => { |
| 53 | built.push(environment.name); |
| 54 | }, |
| 55 | } as never); |
| 56 | return built; |
| 57 | }; |
| 58 | |
| 59 | describe("options plugin", () => { |
| 60 | it("preserves dependency discovery settings for each Worker environment", async () => { |
| 61 | const plugin = optionsPlugin.vite({ |
| 62 | viteEnvironments: { entry: "rsc", children: ["ssr"] }, |
| 63 | }); |
| 64 | const result = await callViteConfig(plugin, { |
| 65 | optimizeDeps: { noDiscovery: true }, |
| 66 | environments: { ssr: { optimizeDeps: { noDiscovery: false } } }, |
| 67 | }); |
| 68 | expect(result.environments?.rsc?.optimizeDeps?.noDiscovery).toBe(true); |
| 69 | expect(result.environments?.ssr?.optimizeDeps?.noDiscovery).toBe(false); |
| 70 | |
| 71 | const explicit = await callViteConfig(optionsPlugin.vite({ main: "./worker.ts" }), { |
| 72 | environments: { ssr: { optimizeDeps: { noDiscovery: true } } }, |
| 73 | }); |
| 74 | expect(explicit.environments?.ssr?.optimizeDeps?.noDiscovery).toBe(true); |
| 75 | |
| 76 | const defaults = await callViteConfig(optionsPlugin.vite({ main: "./worker.ts" }), {}); |
| 77 | expect(defaults.environments?.ssr?.optimizeDeps?.noDiscovery).toBe(false); |
| 78 | }); |
| 79 | |
| 80 | it("applies the Cloudflare defaults", () => { |
| 81 | const plugin = optionsPlugin.rolldown({}); |
| 82 | const input = {}; |
| 83 | assert(typeof plugin.options === "function", "plugin.options is not a function"); |
| 84 | const output = plugin.options.call({} as MinimalPluginContext, input); |
| 85 | assert(output, "output is not defined"); |
| 86 | assert(!(output instanceof Promise), "output is a promise"); |
| 87 | |
| 88 | expect(output.platform).toBe("neutral"); |
| 89 | expect(output.resolve?.conditionNames).toEqual([ |
| 90 | "workerd", |
| 91 | "worker", |
| 92 | "module", |
| 93 | "browser", |
| 94 | "production", |
| 95 | ]); |
| 96 | expect(output.resolve?.mainFields).toEqual([ |
| 97 | "browser", |
| 98 | "module", |
| 99 | "jsnext:main", |
| 100 | "jsnext", |
| 101 | "main", |
| 102 | ]); |
| 103 | expect(output.resolve?.extensions).toEqual([ |
| 104 | ".mjs", |
| 105 | ".js", |
| 106 | ".mts", |
| 107 | ".ts", |
| 108 | ".jsx", |
| 109 | ".tsx", |
| 110 | ".json", |
| 111 | ".cjs", |
| 112 | ".cts", |
| 113 | ".ctx", |
| 114 | ]); |
| 115 | expect(output.transform?.target).toBe("es2026"); |
| 116 | }); |
| 117 | |
| 118 | it("defines production node env values and stubs process.env without nodejs_compat", () => { |
| 119 | const plugin = optionsPlugin.rolldown({}); |
| 120 | const input = {}; |
| 121 | assert(typeof plugin.options === "function", "plugin.options is not a function"); |
| 122 | const output = plugin.options!.call({} as MinimalPluginContext, input); |
| 123 | assert(output, "output is not defined"); |
| 124 | assert(!(output instanceof Promise), "output is a promise"); |
| 125 | |
| 126 | expect(output.transform?.define).toMatchObject({ |
| 127 | "process.env.NODE_ENV": '"production"', |
| 128 | "global.process.env.NODE_ENV": '"production"', |
| 129 | "globalThis.process.env.NODE_ENV": '"production"', |
| 130 | "process.env": "{}", |
| 131 | "global.process.env": "{}", |
| 132 | "globalThis.process.env": "{}", |
| 133 | }); |
| 134 | }); |
| 135 | |
| 136 | it.each([{ compatibilityFlags: ["nodejs_compat"] }, { compatibilityDate: "2026-08-31" }])( |
| 137 | "preserves runtime process.env with Node compatibility: %j", |
| 138 | (options) => { |
| 139 | const plugin = optionsPlugin.rolldown(options); |
| 140 | const input = {}; |
| 141 | assert(typeof plugin.options === "function", "plugin.options is not a function"); |
| 142 | const output = plugin.options!.call({} as MinimalPluginContext, input); |
| 143 | assert(output, "output is not defined"); |
| 144 | assert(!(output instanceof Promise), "output is a promise"); |
| 145 | |
| 146 | expect(output.transform?.define).toMatchObject({ |
| 147 | "process.env.NODE_ENV": '"production"', |
| 148 | "global.process.env.NODE_ENV": '"production"', |
| 149 | "globalThis.process.env.NODE_ENV": '"production"', |
| 150 | }); |
| 151 | expect(output.transform?.define?.["process.env"]).toBeUndefined(); |
| 152 | expect(output.transform?.define?.["global.process.env"]).toBeUndefined(); |
| 153 | expect(output.transform?.define?.["globalThis.process.env"]).toBeUndefined(); |
| 154 | }, |
| 155 | ); |
| 156 | |
| 157 | it("defines navigator.userAgent only when the compatibility date supports it", () => { |
| 158 | const withNavigator = optionsPlugin.rolldown({ |
| 159 | compatibilityDate: "2022-03-21", |
| 160 | }); |
| 161 | const withoutNavigator = optionsPlugin.rolldown({ |
| 162 | compatibilityDate: "2022-03-20", |
| 163 | }); |
| 164 | |
| 165 | assert(typeof withNavigator.options === "function", "withNavigator.options is not a function"); |
| 166 | assert( |
| 167 | typeof withoutNavigator.options === "function", |
| 168 | "withoutNavigator.options is not a function", |
| 169 | ); |
| 170 | |
| 171 | const withNavigatorOutput = withNavigator.options!.call({} as MinimalPluginContext, {}); |
| 172 | const withoutNavigatorOutput = withoutNavigator.options.call({} as MinimalPluginContext, {}); |
| 173 | |
| 174 | assert(withNavigatorOutput, "withNavigatorOutput is not defined"); |
| 175 | assert(!(withNavigatorOutput instanceof Promise), "withNavigatorOutput is a promise"); |
| 176 | assert(withoutNavigatorOutput, "withoutNavigatorOutput is not defined"); |
| 177 | assert(!(withoutNavigatorOutput instanceof Promise), "withoutNavigatorOutput is a promise"); |
| 178 | |
| 179 | expect(withNavigatorOutput.transform?.define?.["navigator.userAgent"]).toBe( |
| 180 | '"Cloudflare-Workers"', |
| 181 | ); |
| 182 | expect(withoutNavigatorOutput.transform?.define?.["navigator.userAgent"]).toBeUndefined(); |
| 183 | }); |
| 184 | |
| 185 | it("normalizes Windows backslashes in worker entry ids", () => { |
| 186 | const plugin = optionsPlugin.rolldown({}); |
| 187 | assert(typeof plugin.options === "function", "plugin.options is not a function"); |
| 188 | const output = plugin.options.call({} as MinimalPluginContext, { |
| 189 | input: { worker: "D:\\src\\app\\entry-server.ts" }, |
| 190 | }); |
| 191 | assert(output, "output is not defined"); |
| 192 | assert(!(output instanceof Promise), "output is a promise"); |
| 193 | |
| 194 | // The virtual worker-entry id must use POSIX separators so it round-trips |
| 195 | // through Rolldown/Vite id handling and resolves on Windows. |
| 196 | expect(output.input).toEqual({ |
| 197 | worker: "\0distilled:worker-entry:D:/src/app/entry-server.ts", |
| 198 | }); |
| 199 | }); |
| 200 | |
| 201 | it("defaults builder.buildApp when the user config does not define one", async () => { |
| 202 | const plugin = optionsPlugin.vite({ main: "./worker.ts" }); |
| 203 | const result = await callViteConfig(plugin, {}); |
| 204 | expect(typeof result.builder?.buildApp).toBe("function"); |
| 205 | }); |
| 206 | |
| 207 | it("does not clobber a framework-provided builder.buildApp", async () => { |
| 208 | // Vite merges plugin `config` results over the user config, so returning a |
| 209 | // default here would replace the framework's orchestrator (e.g. Astro's |
| 210 | // prerender -> ssr -> client pipeline). |
| 211 | const buildApp = async () => {}; |
| 212 | const plugin = optionsPlugin.vite({ main: "./worker.ts" }); |
| 213 | const result = await callViteConfig(plugin, { builder: { buildApp } }); |
| 214 | expect(result.builder).toBeUndefined(); |
| 215 | }); |
| 216 | |
| 217 | it("builds only the client in SPA mode", async () => { |
| 218 | const plugin = optionsPlugin.vite({}); |
| 219 | const result = await callViteConfig(plugin, { root: HTML_ROOT }); |
| 220 | // `custom`, as `@cloudflare/vite-plugin` sets it: HTML is served through |
| 221 | // the Worker's router, never Vite's SPA middleware. |
| 222 | expect(result.appType).toBe("custom"); |
| 223 | // An assets-only site declares no Worker environments. |
| 224 | expect(result.environments).toBeUndefined(); |
| 225 | const built = await runBuildApp(result, [ |
| 226 | { name: "client", root: HTML_ROOT }, |
| 227 | { name: "ssr", root: HTML_ROOT }, |
| 228 | ]); |
| 229 | expect(built).toEqual(["client"]); |
| 230 | }); |
| 231 | |
| 232 | it("builds an assets-only site with only public/ through a fallback client entry", async () => { |
| 233 | // As `@cloudflare/vite-plugin` does: the client still builds, from an |
| 234 | // empty entry, so its output holds the `public/` files. |
| 235 | const plugin = optionsPlugin.vite({}); |
| 236 | const result = await callViteConfig(plugin, { root: NO_HTML_ROOT }); |
| 237 | const environments = { |
| 238 | client: { |
| 239 | name: "client", |
| 240 | isBuilt: false, |
| 241 | config: { root: NO_HTML_ROOT, build: { outDir: "dist" } as Record<string, unknown> }, |
| 242 | }, |
| 243 | }; |
| 244 | const built: Array<string> = []; |
| 245 | await result.builder?.buildApp?.({ |
| 246 | config: { root: NO_HTML_ROOT, publicDir: path.join(NO_HTML_ROOT, "client-html") }, |
| 247 | environments, |
| 248 | build: async (environment: { name: string }) => { |
| 249 | built.push(environment.name); |
| 250 | }, |
| 251 | } as never); |
| 252 | expect(built).toEqual(["client"]); |
| 253 | expect(environments.client.config.build.rollupOptions).toMatchObject({ |
| 254 | input: "virtual:cloudflare/client-fallback-entry", |
| 255 | }); |
| 256 | }); |
| 257 | |
| 258 | it("skips skipEnvironments in the default buildApp", async () => { |
| 259 | const plugin = optionsPlugin.vite({ |
| 260 | main: "./worker.ts", |
| 261 | skipEnvironments: ["prerender"], |
| 262 | }); |
| 263 | const result = await callViteConfig(plugin, {}); |
| 264 | |
| 265 | const built = await runBuildApp(result, [ |
| 266 | { name: "ssr" }, |
| 267 | { name: "client", build: { rollupOptions: { input: "./src/main.tsx" } } }, |
| 268 | // Framework-owned environment with no build input of its own. |
| 269 | { name: "prerender" }, |
| 270 | { name: "already", isBuilt: true }, |
| 271 | ]); |
| 272 | expect(built).toEqual(["ssr", "client"]); |
| 273 | }); |
| 274 | |
| 275 | it("skips the entry-less client environment in the default buildApp", async () => { |
| 276 | // Vite creates `client` for every app. A pure Worker app never configures |
| 277 | // it, so building it would fall back to a nonexistent `index.html`. |
| 278 | const plugin = optionsPlugin.vite({ main: "./worker.ts" }); |
| 279 | const result = await callViteConfig(plugin, {}); |
| 280 | |
| 281 | const built = await runBuildApp(result, [{ name: "ssr" }, { name: "client" }]); |
| 282 | expect(built).toEqual(["ssr"]); |
| 283 | }); |
| 284 | |
| 285 | it("builds a client environment that has a configured input", async () => { |
| 286 | const plugin = optionsPlugin.vite({ main: "./worker.ts" }); |
| 287 | const result = await callViteConfig(plugin, {}); |
| 288 | |
| 289 | const withRollupInput = await runBuildApp(result, [ |
| 290 | { name: "client", build: { rollupOptions: { input: "./src/main.tsx" } } }, |
| 291 | ]); |
| 292 | expect(withRollupInput).toEqual(["client"]); |
| 293 | |
| 294 | const withRolldownInput = await runBuildApp(result, [ |
| 295 | { |
| 296 | name: "client", |
| 297 | build: { rolldownOptions: { input: "./src/main.tsx" } }, |
| 298 | }, |
| 299 | ]); |
| 300 | expect(withRolldownInput).toEqual(["client"]); |
| 301 | }); |
| 302 | |
| 303 | it("builds a client environment whose root has an index.html", async () => { |
| 304 | const plugin = optionsPlugin.vite({ main: "./worker.ts" }); |
| 305 | const result = await callViteConfig(plugin, {}); |
| 306 | |
| 307 | const built = await runBuildApp(result, [{ name: "client", root: HTML_ROOT }]); |
| 308 | expect(built).toEqual(["client"]); |
| 309 | }); |
| 310 | }); |