|
| 1 | +import { loadEnv, type Plugin } from "vite"; |
| 2 | + |
| 3 | +const LOADERS = { |
| 4 | + node: `export default key => process.env[key];`, |
| 5 | + "cloudflare-workers": `import { env } from 'cloudflare:workers';export default key => env[key];`, |
| 6 | + "netlify-edge": `export default key => Netlify.env.get(key);`, |
| 7 | +}; |
| 8 | + |
| 9 | +export interface EnvPluginOptions { |
| 10 | + server?: { |
| 11 | + runtime?: keyof typeof LOADERS | (string & {}); |
| 12 | + load?: (mode: string) => Record<string, string>; |
| 13 | + prefix?: string; |
| 14 | + }; |
| 15 | + client?: { |
| 16 | + load?: (mode: string) => Record<string, string>; |
| 17 | + prefix?: string; |
| 18 | + }; |
| 19 | +} |
| 20 | + |
| 21 | +const SERVER_ENV = "env:server"; |
| 22 | +const CLIENT_ENV = "env:client"; |
| 23 | + |
| 24 | +const SERVER_RUNTIME_ENV = `${SERVER_ENV}/runtime`; |
| 25 | + |
| 26 | +const SERVER_RUNTIME_LOADER = `${SERVER_RUNTIME_ENV}/loader`; |
| 27 | + |
| 28 | +const DEFAULT_SERVER_PREFIX = "SERVER_"; |
| 29 | +const DEFAULT_CLIENT_PREFIX = "CLIENT_"; |
| 30 | + |
| 31 | +const SERVER_ONLY_MODULE = `throw new Error('Attempt to load server-only environment variables in client runtime.');`; |
| 32 | + |
| 33 | +const SERVER_RUNTIME_CODE = `import load from '${SERVER_RUNTIME_LOADER}'; |
| 34 | +
|
| 35 | +export default new Proxy({}, { |
| 36 | + get(_, key) { |
| 37 | + return load(key); |
| 38 | + }, |
| 39 | +})`; |
| 40 | + |
| 41 | +function convertObjectToModule(object: Record<string, string>): string { |
| 42 | + let result = ""; |
| 43 | + for (const key in object) { |
| 44 | + result += `export const ${key} = ${JSON.stringify(object[key])};`; |
| 45 | + } |
| 46 | + return result; |
| 47 | +} |
| 48 | + |
| 49 | +export function envPlugin(options?: EnvPluginOptions): Plugin { |
| 50 | + const currentOptions = options ?? {}; |
| 51 | + let env: string; |
| 52 | + const serverPrefix = currentOptions.server?.prefix ?? DEFAULT_SERVER_PREFIX; |
| 53 | + const clientPrefix = currentOptions.client?.prefix ?? DEFAULT_CLIENT_PREFIX; |
| 54 | + const runtime = options?.server?.runtime ?? "node"; |
| 55 | + const runtimeCode = runtime in LOADERS ? LOADERS[runtime as keyof typeof LOADERS] : runtime; |
| 56 | + |
| 57 | + return { |
| 58 | + name: "solid-start:env", |
| 59 | + enforce: "pre", |
| 60 | + configResolved(config) { |
| 61 | + env = config.mode !== "production" ? "development" : "production"; |
| 62 | + }, |
| 63 | + resolveId(id) { |
| 64 | + if ( |
| 65 | + id === SERVER_ENV || |
| 66 | + id === CLIENT_ENV || |
| 67 | + id === SERVER_RUNTIME_ENV || |
| 68 | + id === SERVER_RUNTIME_LOADER |
| 69 | + ) { |
| 70 | + return id; |
| 71 | + } |
| 72 | + return undefined; |
| 73 | + }, |
| 74 | + load(id, opts) { |
| 75 | + if (id === SERVER_ENV) { |
| 76 | + if (!opts?.ssr) { |
| 77 | + return SERVER_ONLY_MODULE; |
| 78 | + } |
| 79 | + const vars = currentOptions.server?.load |
| 80 | + ? currentOptions.server.load(env) |
| 81 | + : loadEnv(env, '.', serverPrefix); |
| 82 | + return convertObjectToModule(vars); |
| 83 | + } |
| 84 | + if (id === CLIENT_ENV) { |
| 85 | + const vars = currentOptions.client?.load |
| 86 | + ? currentOptions.client.load(env) |
| 87 | + : loadEnv(env, '.', clientPrefix); |
| 88 | + return convertObjectToModule(vars); |
| 89 | + } |
| 90 | + if (id === SERVER_RUNTIME_LOADER) { |
| 91 | + if (!opts?.ssr) { |
| 92 | + return SERVER_ONLY_MODULE; |
| 93 | + } |
| 94 | + return runtimeCode; |
| 95 | + } |
| 96 | + if (id === SERVER_RUNTIME_ENV) { |
| 97 | + if (!opts?.ssr) { |
| 98 | + return SERVER_ONLY_MODULE; |
| 99 | + } |
| 100 | + return SERVER_RUNTIME_CODE; |
| 101 | + } |
| 102 | + return undefined; |
| 103 | + }, |
| 104 | + }; |
| 105 | +} |
0 commit comments