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, 06:00 UTC
| 1 | import * as NodeFs from "node:fs/promises"; |
| 2 | import * as NodeOs from "node:os"; |
| 3 | import * as NodePath from "node:path"; |
| 4 | import { cloudflare as officialCloudflare } from "@cloudflare/vite-plugin"; |
| 5 | import * as vite from "vite"; |
| 6 | import { afterAll, beforeAll, describe, expect, it } from "vitest"; |
| 7 | import cloudflareVitePlugin from "../../plugin.ts"; |
| 8 | import { AHEAD_OF_RELEASE_BUILT_IN_MODULES } from "./ahead-of-release.ts"; |
| 9 | |
| 10 | // Differential conformance: the same project resolved with |
| 11 | // `@cloudflare/vite-plugin` and with Alchemy's plugin must look the same to |
| 12 | // the frameworks and plugins that read the resolved Vite config. |
| 13 | |
| 14 | const COMPATIBILITY_DATE = "2026-03-17"; |
| 15 | |
| 16 | let root: string; |
| 17 | |
| 18 | beforeAll(async () => { |
| 19 | root = await NodeFs.realpath( |
| 20 | await NodeFs.mkdtemp(NodePath.join(NodeOs.tmpdir(), "alchemy-vite-parity-")), |
| 21 | ); |
| 22 | await NodeFs.mkdir(NodePath.join(root, "src")); |
| 23 | await NodeFs.writeFile( |
| 24 | NodePath.join(root, "src/index.ts"), |
| 25 | "export default { fetch: () => new Response('ok') };", |
| 26 | ); |
| 27 | await NodeFs.writeFile(NodePath.join(root, "package.json"), JSON.stringify({ type: "module" })); |
| 28 | }); |
| 29 | |
| 30 | afterAll(async () => { |
| 31 | await NodeFs.rm(root, { recursive: true, force: true }); |
| 32 | }); |
| 33 | |
| 34 | const resolve = ( |
| 35 | plugins: vite.PluginOption, |
| 36 | command: "serve" | "build", |
| 37 | userConfig: vite.InlineConfig = {}, |
| 38 | ) => |
| 39 | vite.resolveConfig( |
| 40 | { ...userConfig, configFile: false, root, logLevel: "silent", plugins: [plugins] }, |
| 41 | command, |
| 42 | ); |
| 43 | |
| 44 | const official = (flags: Array<string>) => |
| 45 | officialCloudflare({ |
| 46 | viteEnvironment: { name: "ssr" }, |
| 47 | inspectorPort: false, |
| 48 | persistState: false, |
| 49 | config: { |
| 50 | name: "worker", |
| 51 | main: "./src/index.ts", |
| 52 | compatibility_date: COMPATIBILITY_DATE, |
| 53 | compatibility_flags: flags, |
| 54 | }, |
| 55 | }); |
| 56 | |
| 57 | const alchemy = (flags: Array<string>) => |
| 58 | cloudflareVitePlugin({ |
| 59 | main: "./src/index.ts", |
| 60 | compatibilityDate: COMPATIBILITY_DATE, |
| 61 | compatibilityFlags: flags, |
| 62 | }); |
| 63 | |
| 64 | // Built-ins ported ahead of the installed release are left out on both sides. |
| 65 | const sorted = (values: ReadonlyArray<unknown> | undefined) => |
| 66 | [...(values ?? [])] |
| 67 | .map((value) => String(value)) |
| 68 | .filter((value) => !AHEAD_OF_RELEASE_BUILT_IN_MODULES.includes(value)) |
| 69 | .sort(); |
| 70 | |
| 71 | /** What a framework can observe of a Worker environment's resolved options. */ |
| 72 | const workerEnvironment = (config: vite.ResolvedConfig) => { |
| 73 | const environment = config.environments.ssr!; |
| 74 | const build = environment.build; |
| 75 | const rolldown = build.rolldownOptions ?? {}; |
| 76 | const optimizeDeps = environment.optimizeDeps; |
| 77 | const optimizeRolldown = (optimizeDeps.rolldownOptions ?? {}) as { |
| 78 | platform?: string; |
| 79 | resolve?: { conditionNames?: Array<string>; extensions?: Array<string> }; |
| 80 | transform?: { target?: unknown; define?: Record<string, string> }; |
| 81 | }; |
| 82 | return { |
| 83 | resolve: { |
| 84 | conditions: environment.resolve.conditions, |
| 85 | noExternal: environment.resolve.noExternal, |
| 86 | builtins: sorted(environment.resolve.builtins), |
| 87 | external: environment.resolve.external, |
| 88 | }, |
| 89 | define: environment.define, |
| 90 | keepProcessEnv: environment.keepProcessEnv, |
| 91 | build: { |
| 92 | target: build.target, |
| 93 | cssTarget: build.cssTarget, |
| 94 | emitAssets: build.emitAssets, |
| 95 | manifest: build.manifest, |
| 96 | outDir: build.outDir, |
| 97 | sourcemap: build.sourcemap, |
| 98 | copyPublicDir: build.copyPublicDir, |
| 99 | ssr: build.ssr, |
| 100 | inputNames: Object.keys((rolldown.input as Record<string, string> | undefined) ?? {}), |
| 101 | preserveEntrySignatures: rolldown.preserveEntrySignatures, |
| 102 | platform: rolldown.platform, |
| 103 | extensions: rolldown.resolve?.extensions, |
| 104 | }, |
| 105 | optimizeDeps: { |
| 106 | noDiscovery: optimizeDeps.noDiscovery, |
| 107 | ignoreOutdatedRequests: optimizeDeps.ignoreOutdatedRequests, |
| 108 | exclude: sorted(optimizeDeps.exclude), |
| 109 | platform: optimizeRolldown.platform, |
| 110 | conditionNames: optimizeRolldown.resolve?.conditionNames, |
| 111 | extensions: optimizeRolldown.resolve?.extensions, |
| 112 | target: optimizeRolldown.transform?.target, |
| 113 | define: optimizeRolldown.transform?.define, |
| 114 | }, |
| 115 | }; |
| 116 | }; |
| 117 | |
| 118 | const clientEnvironment = (config: vite.ResolvedConfig) => ({ |
| 119 | outDir: config.environments.client!.build.outDir, |
| 120 | exclude: sorted(config.environments.client!.optimizeDeps.exclude), |
| 121 | }); |
| 122 | |
| 123 | describe.each([ |
| 124 | { name: "without nodejs_compat", flags: [] }, |
| 125 | { name: "with nodejs_compat", flags: ["nodejs_compat"] }, |
| 126 | { name: "with nodejs_als", flags: ["nodejs_als"] }, |
| 127 | ])("resolved config matches @cloudflare/vite-plugin $name", ({ flags }) => { |
| 128 | it.each(["serve", "build"] as const)("%s: the Worker environment", async (command) => { |
| 129 | const expected = workerEnvironment(await resolve(official(flags), command)); |
| 130 | const actual = workerEnvironment(await resolve(alchemy(flags), command)); |
| 131 | expect(actual).toEqual(expected); |
| 132 | }); |
| 133 | |
| 134 | it.each(["serve", "build"] as const)("%s: the client environment", async (command) => { |
| 135 | const expected = clientEnvironment(await resolve(official(flags), command)); |
| 136 | const actual = clientEnvironment(await resolve(alchemy(flags), command)); |
| 137 | expect(actual).toEqual(expected); |
| 138 | }); |
| 139 | |
| 140 | it.each(["serve", "build"] as const)("%s: app type and denied files", async (command) => { |
| 141 | const expected = await resolve(official(flags), command); |
| 142 | const actual = await resolve(alchemy(flags), command); |
| 143 | expect(actual.appType).toBe(expected.appType); |
| 144 | // Alchemy also denies its own state and stack files. |
| 145 | expect(actual.server.fs.deny).toEqual(expect.arrayContaining(expected.server.fs.deny)); |
| 146 | }); |
| 147 | }); |
| 148 | |
| 149 | describe.each([ |
| 150 | { name: "a custom build.outDir", userConfig: { build: { outDir: "out" } } }, |
| 151 | { name: "source maps turned off", userConfig: { build: { sourcemap: false } } }, |
| 152 | { |
| 153 | name: "the Worker's own source map setting", |
| 154 | userConfig: { environments: { ssr: { build: { sourcemap: "hidden" as const } } } }, |
| 155 | }, |
| 156 | ])("resolved config matches @cloudflare/vite-plugin with $name", ({ userConfig }) => { |
| 157 | it.each(["serve", "build"] as const)("%s", async (command) => { |
| 158 | const expected = await resolve(official(["nodejs_compat"]), command, userConfig); |
| 159 | const actual = await resolve(alchemy(["nodejs_compat"]), command, userConfig); |
| 160 | expect(workerEnvironment(actual)).toEqual(workerEnvironment(expected)); |
| 161 | expect(clientEnvironment(actual)).toEqual(clientEnvironment(expected)); |
| 162 | }); |
| 163 | }); |
| 164 | |
| 165 | describe("resolved config matches @cloudflare/vite-plugin in production", () => { |
| 166 | it("defines the production NODE_ENV", async () => { |
| 167 | const previous = process.env.NODE_ENV; |
| 168 | process.env.NODE_ENV = "production"; |
| 169 | try { |
| 170 | const expected = workerEnvironment(await resolve(official(["nodejs_compat"]), "build")); |
| 171 | const actual = workerEnvironment(await resolve(alchemy(["nodejs_compat"]), "build")); |
| 172 | expect(actual.define).toEqual(expected.define); |
| 173 | expect(actual.optimizeDeps.define).toEqual(expected.optimizeDeps.define); |
| 174 | } finally { |
| 175 | process.env.NODE_ENV = previous; |
| 176 | } |
| 177 | }); |
| 178 | }); |