Skip to content

Commit f4250f3

Browse files
committed
Add build-time config stub via Vite plugin
During `vite build`, env vars are unavailable so the real config validation fails. A Vite plugin now redirects all imports of $lib/config to a build-stub with safe defaults.
1 parent b5d25cd commit f4250f3

3 files changed

Lines changed: 70 additions & 2 deletions

File tree

src/lib/config/build-stub.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Build-time stub for $lib/config.
3+
* During `vite build`, env vars are unavailable — this provides safe defaults
4+
* so that module-level imports don't throw. None of this code runs at runtime.
5+
*/
6+
import type { Config, Environment } from './index';
7+
8+
const nodeEnv: Environment = 'development';
9+
const port = 7000;
10+
11+
const jwt = { access: { secret: '-' }, refresh: { secret: '-' } };
12+
const github = {};
13+
const postgate = { url: '-', token: '-', systemTokenSecret: '-' };
14+
const sharedStorage = {};
15+
const email = {};
16+
const mistral = {};
17+
const anthropic = {};
18+
19+
const appUrl = 'http://localhost:4200';
20+
21+
export const config: Config = {
22+
nodeEnv,
23+
port,
24+
jwt,
25+
github,
26+
postgate,
27+
sharedStorage,
28+
mistral,
29+
anthropic,
30+
email,
31+
appUrl
32+
} as Config;
33+
34+
export { nodeEnv, port, jwt, github, postgate, sharedStorage, mistral, anthropic, email, appUrl };
35+
export type { Config, Environment };

src/lib/config/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ function loadConfig(): Config {
182182
}
183183

184184
// Export singleton config instance
185-
export const config = loadConfig();
185+
export const config: Config = loadConfig();
186186

187187
// Export individual sections for convenience
188188
export const { nodeEnv, port, jwt, github, postgate, sharedStorage, mistral, anthropic, email, appUrl } = config;

vite.config.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,41 @@ if (!/^\d{1,5}$/.test(envPort || '')) {
8484
console.warn(`Warning: Invalid PORT environment variable "${envPort}", defaulting to 5173`);
8585
}
8686

87+
/**
88+
* At build time, replace $lib/config with a stub that provides safe defaults.
89+
* Env vars are unavailable during `vite build` — the real config loads at runtime.
90+
*/
91+
function configBuildStubPlugin(): Plugin {
92+
const stubPath = resolve('./src/lib/config/build-stub.ts');
93+
const realConfigPath = resolve('./src/lib/config/index.ts');
94+
let isBuild = false;
95+
96+
return {
97+
name: 'config-build-stub',
98+
enforce: 'pre',
99+
100+
configResolved(config) {
101+
isBuild = config.command === 'build';
102+
},
103+
104+
async resolveId(id, importer, options) {
105+
if (!isBuild) return;
106+
107+
// Don't redirect the stub's own type import back to itself
108+
if (importer === stubPath) return;
109+
110+
// Let Vite resolve normally, then check if it points to config/index.ts
111+
const resolved = await this.resolve(id, importer, { ...options, skipSelf: true });
112+
113+
if (resolved && resolved.id === realConfigPath) {
114+
return stubPath;
115+
}
116+
}
117+
};
118+
}
119+
87120
export default defineConfig({
88-
plugins: [cronerWasmInlinePlugin(), zodLocalesPlugin(), sveltekit()],
121+
plugins: [configBuildStubPlugin(), cronerWasmInlinePlugin(), zodLocalesPlugin(), sveltekit()],
89122
server: {
90123
port: parseInt(process.env.PORT || '5173', 10)
91124
}

0 commit comments

Comments
 (0)