Skip to content

Commit 4bb956e

Browse files
committed
feat(env): add runtime
1 parent 8c9ad3d commit 4bb956e

2 files changed

Lines changed: 39 additions & 3 deletions

File tree

apps/tests/src/functions/use-server-function-meta.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import { getServerFunctionMeta } from "@solidjs/start";
44

5+
import { SERVER_EXAMPLE } from 'env:server';
6+
57
export function serverFnWithMeta() {
8+
console.log(SERVER_EXAMPLE);
69
return typeof getServerFunctionMeta()?.id;
710
}

packages/start/src/config/env.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import { loadEnv, type Plugin } from "vite";
22

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+
39
export interface EnvPluginOptions {
410
server?: {
11+
runtime: keyof typeof LOADERS | (string & {});
512
load?: () => Record<string, string>;
613
prefix?: string;
714
};
@@ -14,11 +21,23 @@ export interface EnvPluginOptions {
1421
const SERVER_ENV = "env:server";
1522
const CLIENT_ENV = "env:client";
1623

24+
const SERVER_RUNTIME_ENV = `${SERVER_ENV}/runtime`;
25+
26+
const SERVER_RUNTIME_LOADER = `${SERVER_RUNTIME_ENV}/loader`;
27+
1728
const DEFAULT_SERVER_PREFIX = "SERVER_";
1829
const DEFAULT_CLIENT_PREFIX = "CLIENT_";
1930

2031
const SERVER_ONLY_MODULE = `throw new Error('Attempt to load server-only environment variables in client runtime.');`;
2132

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+
2241
function convertObjectToModule(object: Record<string, string>): string {
2342
let result = "";
2443
for (const key in object) {
@@ -32,14 +51,22 @@ export function envPlugin(options?: EnvPluginOptions): Plugin {
3251
let env: string;
3352
const serverPrefix = currentOptions.server?.prefix ?? DEFAULT_SERVER_PREFIX;
3453
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+
3557
return {
3658
name: "solid-start:env",
3759
enforce: "pre",
3860
configResolved(config) {
3961
env = config.mode !== "production" ? "development" : "production";
4062
},
4163
resolveId(id) {
42-
if (id === SERVER_ENV || id === CLIENT_ENV) {
64+
if (
65+
id === SERVER_ENV ||
66+
id === CLIENT_ENV ||
67+
id === SERVER_RUNTIME_ENV ||
68+
SERVER_RUNTIME_LOADER
69+
) {
4370
return id;
4471
}
4572
return null;
@@ -51,15 +78,21 @@ export function envPlugin(options?: EnvPluginOptions): Plugin {
5178
}
5279
const vars = currentOptions.server?.load
5380
? currentOptions.server.load()
54-
: loadEnv(env, false, clientPrefix);
81+
: loadEnv(env, false, serverPrefix);
5582
return convertObjectToModule(vars);
5683
}
5784
if (id === CLIENT_ENV) {
5885
const vars = currentOptions.client?.load
5986
? currentOptions.client.load()
60-
: loadEnv(env, false, serverPrefix);
87+
: loadEnv(env, false, clientPrefix);
6188
return convertObjectToModule(vars);
6289
}
90+
if (id === SERVER_RUNTIME_LOADER) {
91+
return runtimeCode;
92+
}
93+
if (id === SERVER_RUNTIME_ENV) {
94+
return SERVER_RUNTIME_CODE;
95+
}
6396
return null;
6497
},
6598
};

0 commit comments

Comments
 (0)