@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 185 lines 8.01 KB
Raw
1 /**
2 * Config synthesis for driving SolidStart programmatically while keeping the
3 * project's own `vite.config.*` authoritative.
4 *
5 * SolidStart 2's `solidStart()` is a plain Vite plugin: it declares the
6 * `client` / `ssr` environments and a `builder.buildApp` that builds both.
7 * The server half — turning the SSR bundle into a deployable server — is
8 * `@solidjs/vite-plugin-nitro-2`, a second Vite plugin whose `buildApp`
9 * replaces SolidStart's and runs nitro over the emitted SSR bundle.
10 *
11 * That plugin takes its nitro configuration as a plain argument and always
12 * passes a `preset` down to nitro (its own default is `"node-server"`), so
13 * neither `NITRO_PRESET` nor a `nitro.config.ts` can override it. The only
14 * seam that gets the last word is the plugin instance itself — so this
15 * integration constructs it:
16 *
17 * 1. The project's `vite.config.*` loads natively (its `solidStart()` and any
18 * other plugins are untouched).
19 * 2. The integration APPENDS its own `nitroV2Plugin(...)` instance — loaded
20 * from the *project's* `node_modules` — through vite's inline config.
21 * Inline plugins are concatenated after the config file's, and vite merges
22 * plugin `config()` results in order, so the appended instance's
23 * `builder.buildApp` (and therefore its nitro config) wins.
24 * 3. {@link resolveNitroConfig} assembles that nitro config: the deploy
25 * target's preset is enforced last, after the caller's `nitro` overrides
26 * and the target's own {@link NitroConfigSlice} pass.
27 *
28 * Conflict policy: a project that registers `nitroV2Plugin` in its own
29 * `vite.config.*` would have its nitro options silently dropped (our
30 * appended instance replaces its `buildApp`), so the build fails with
31 * {@link pluginConflictMessage} instead. Nitro options belong on the
32 * integration (`nitro: { ... }`), which forwards them.
33 *
34 * The preset-name helpers are duplicated from the Nuxt integration rather
35 * than shared: they are pure string functions, and a shared module would
36 * couple two independent framework packages through framework-core's public
37 * surface for ~20 lines.
38 */
39
40 /** A minimal structural slice of a nitro config (`NitroConfig`). */
41 export interface NitroConfigSlice extends Record<string, unknown> {
42 preset?: string | undefined;
43 rootDir?: string | undefined;
44 /** Nitro's work directory. Forced under `rootDir` so fixture clones don't share a parent-workspace cache. */
45 buildDir?: string | undefined;
46 awsLambda?: Record<string, unknown> | undefined;
47 output?: NitroOutputConfigSlice | undefined;
48 }
49
50 /** The `output` slice of a nitro config this integration reads. */
51 export interface NitroOutputConfigSlice {
52 dir?: string | undefined;
53 serverDir?: string | undefined;
54 publicDir?: string | undefined;
55 }
56
57 /** The plugin name `@solidjs/vite-plugin-nitro-2` registers. */
58 export const NITRO_PLUGIN_NAME = "solid-start-vite-plugin-nitro";
59
60 /**
61 * Normalize a nitro preset name for comparison: nitro treats `aws_lambda`,
62 * `aws-lambda`, and `awsLambda` as the same preset (kebab/snake/camel
63 * insensitive).
64 */
65 export const normalizePresetName = (name: string): string =>
66 name
67 .replace(/([a-z0-9])([A-Z])/g, "$1-$2")
68 .replace(/[_-]+/g, "-")
69 .toLowerCase();
70
71 /** Whether two preset names identify the same nitro preset. */
72 export const isSamePreset = (a: string, b: string): boolean =>
73 normalizePresetName(a) === normalizePresetName(b);
74
75 /** The actionable error message for a caller-configured foreign preset. */
76 export const presetConflictMessage = (userPreset: string, targetPreset: string): string =>
77 `The integration was given \`nitro.preset: "${userPreset}"\`, but this deploy target ` +
78 `builds through the "${targetPreset}" preset — a "${userPreset}" build would never be ` +
79 "deployed. Remove `nitro.preset` and let the deploy target select it, or run the " +
80 `framework's own CLI for a "${userPreset}" build outside this integration.`;
81
82 /** The actionable error message for a project-registered nitro plugin. */
83 export const pluginConflictMessage = (): string =>
84 "The project's vite config registers `nitroV2Plugin()` from " +
85 "`@solidjs/vite-plugin-nitro-2`. This integration appends its own instance so the " +
86 "deploy target owns the nitro preset, which would silently discard the project's " +
87 "nitro options. Remove `nitroV2Plugin()` from the vite config and pass its options " +
88 "to the integration instead (`nitro: { ... }`, e.g. `nitro: { prerender: { crawlLinks: true } }`).";
89
90 export interface ResolveNitroConfigInput {
91 /** The deploy target's nitro preset — always the last word. */
92 readonly preset: string;
93 /** Absolute project root, forwarded to nitro as `rootDir`. */
94 readonly rootDir: string;
95 /** Caller-supplied nitro overrides (the integration's `nitro` option). */
96 readonly nitro?: Record<string, unknown> | undefined;
97 /**
98 * Extra last-word mutation contributed by the deploy target (e.g. the AWS
99 * target's `awsLambda.streaming`). Runs after the preset is enforced.
100 */
101 readonly configure?: ((nitroConfig: NitroConfigSlice) => void) | undefined;
102 }
103
104 /**
105 * Assemble the nitro config handed to the appended `nitroV2Plugin(...)`
106 * instance: the caller's overrides first, then the owned keys (`preset`,
107 * `rootDir`) enforced over them, then the deploy target's pass.
108 */
109 export const resolveNitroConfig = (input: ResolveNitroConfigInput): NitroConfigSlice => {
110 const config: NitroConfigSlice = { ...input.nitro };
111 config.preset = input.preset;
112 config.rootDir = input.rootDir;
113 // Keep nitro's `.nitro` cache inside this project. A fixture clone
114 // resolves nitro from the parent workspace; nitro's default cache then
115 // lands in that workspace and leaks other tests' traced modules.
116 config.buildDir = `${input.rootDir}/.nitro`;
117 input.configure?.(config);
118 return config;
119 };
120
121 /**
122 * The caller's foreign preset, or `undefined` when none was given or it is
123 * (an alias of) the target's own.
124 */
125 export const findPresetConflict = (
126 nitro: Record<string, unknown> | undefined,
127 targetPreset: string,
128 ): string | undefined => {
129 const preset = nitro?.["preset"];
130 return typeof preset === "string" && preset.length > 0 && !isSamePreset(preset, targetPreset)
131 ? preset
132 : undefined;
133 };
134
135 /**
136 * Resolve the absolute nitro output directories for a build at `root`,
137 * honoring an `output` override on the nitro config. Mirrors nitro's own
138 * defaults (`.output`, `.output/server`, `.output/public`).
139 */
140 export const resolveNitroOutputDirs = (
141 config: NitroConfigSlice,
142 root: string,
143 resolve: (...segments: Array<string>) => string,
144 ): { dir: string; serverDir: string; publicDir: string } => {
145 const dir = resolve(root, config.output?.dir ?? ".output");
146 return {
147 dir,
148 serverDir: resolve(dir, config.output?.serverDir ?? "server"),
149 publicDir: resolve(dir, config.output?.publicDir ?? "public"),
150 };
151 };
152
153 /** Marker set on the plugin instances this integration appends. */
154 const INJECTED = Symbol.for("@alchemy.run/frontend-frameworks/solidstart/injected");
155
156 /** Flatten a vite `plugins` value (nested arrays, falsy entries) to objects. */
157 const flattenPlugins = (value: unknown, out: Array<Record<string | symbol, unknown>>): void => {
158 if (Array.isArray(value)) {
159 for (const entry of value) flattenPlugins(entry, out);
160 return;
161 }
162 if (value !== null && typeof value === "object") {
163 out.push(value as Record<string | symbol, unknown>);
164 }
165 };
166
167 /** Stamp every plugin object this integration created so it is recognizable. */
168 export const markInjectedPlugins = <T>(plugins: T): T => {
169 const flat: Array<Record<string | symbol, unknown>> = [];
170 flattenPlugins(plugins, flat);
171 for (const plugin of flat) {
172 plugin[INJECTED] = true;
173 }
174 return plugins;
175 };
176
177 /**
178 * Whether a vite `plugins` value contains a nitro plugin instance this
179 * integration did NOT create (see {@link pluginConflictMessage}).
180 */
181 export const hasForeignNitroPlugin = (plugins: unknown): boolean => {
182 const flat: Array<Record<string | symbol, unknown>> = [];
183 flattenPlugins(plugins, flat);
184 return flat.some((plugin) => plugin["name"] === NITRO_PLUGIN_NAME && plugin[INJECTED] !== true);
185 };