-
Notifications
You must be signed in to change notification settings - Fork 414
feat: add env:*
#2110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+192
−2
Merged
feat: add env:*
#2110
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8b02c27
feat: add `env:*`
lxsmnsyc 928cf50
feat(env): add runtime
lxsmnsyc 68a68a9
fix(env): add guards
lxsmnsyc d02500a
fix(env): resolver
lxsmnsyc b06ede1
remove unnecessary code
lxsmnsyc e7a6c3b
Update index.ts
lxsmnsyc fc3e9fd
Update env.ts
lxsmnsyc f7839d3
Fix env config
lxsmnsyc 2a712ed
Add some env:server test
lxsmnsyc eeb2093
add changeset
atilafassina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@solidjs/start": minor | ||
| --- | ||
|
|
||
| feat: add `env:*` runtime environment variables support |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| declare module "env:server" { | ||
| export const SERVER_EXAMPLE: string; | ||
| } | ||
| declare module "env:server/runtime" { | ||
| const env: { | ||
| NODE_ENV: string; | ||
| }; | ||
|
|
||
| export default env; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import { SERVER_EXAMPLE } from "env:server"; | ||
| import env from "env:server/runtime"; | ||
| import { createEffect, createSignal } from "solid-js"; | ||
|
|
||
| async function getServerCompiledEnv() { | ||
| "use server"; | ||
|
|
||
| return await Promise.resolve(SERVER_EXAMPLE); | ||
| } | ||
|
|
||
| async function getServerRuntimeEnv() { | ||
| "use server"; | ||
|
|
||
| return await Promise.resolve(env.NODE_ENV); | ||
| } | ||
|
|
||
| async function checkServerEnvOnClient() { | ||
| try { | ||
| await import("env:server"); | ||
| return false; | ||
| } catch { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| export default function App() { | ||
| const [output, setOutput] = createSignal<{ result?: boolean }>({}); | ||
|
|
||
| createEffect(async () => { | ||
| const resultA = await getServerCompiledEnv(); | ||
| const resultB = await getServerRuntimeEnv(); | ||
| const checkImport = await checkServerEnvOnClient(); | ||
| setOutput(prev => ({ ...prev, result: !!resultA && !!resultB && checkImport })); | ||
| }); | ||
|
|
||
| return ( | ||
| <main> | ||
| <span id="server-fn-test">{JSON.stringify(output())}</span> | ||
| </main> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,30 @@ | ||
| import { defineConfig } from "vite"; | ||
| import { solidStart } from "../../packages/start/src/config"; | ||
| import { nitroV2Plugin } from "../../packages/start-nitro-v2-vite-plugin/src"; | ||
| import { solidStart } from "../../packages/start/src/config"; | ||
|
|
||
| export default defineConfig({ | ||
| server: { | ||
| port: 3000, | ||
| }, | ||
| plugins: [solidStart(), nitroV2Plugin()], | ||
| plugins: [ | ||
| solidStart({ | ||
| env: { | ||
| server: { | ||
| load() { | ||
| return { | ||
| SERVER_EXAMPLE: "This is a server example.", | ||
| }; | ||
| }, | ||
| }, | ||
| client: { | ||
| load() { | ||
| return { | ||
| CLIENT_EXAMPLE: "This is a client example.", | ||
| }; | ||
| }, | ||
| }, | ||
| }, | ||
| }), | ||
| nitroV2Plugin(), | ||
| ], | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import { loadEnv, type Plugin } from "vite"; | ||
|
|
||
| const LOADERS = { | ||
| node: `export default key => process.env[key];`, | ||
| "cloudflare-workers": `import { env } from 'cloudflare:workers';export default key => env[key];`, | ||
| "netlify-edge": `export default key => Netlify.env.get(key);`, | ||
| }; | ||
|
|
||
| export interface EnvPluginOptions { | ||
| server?: { | ||
| runtime?: keyof typeof LOADERS | (string & {}); | ||
| load?: (mode: string) => Record<string, string>; | ||
| prefix?: string; | ||
| }; | ||
| client?: { | ||
| load?: (mode: string) => Record<string, string>; | ||
| prefix?: string; | ||
| }; | ||
| } | ||
|
|
||
| const SERVER_ENV = "env:server"; | ||
| const CLIENT_ENV = "env:client"; | ||
|
|
||
| const SERVER_RUNTIME_ENV = `${SERVER_ENV}/runtime`; | ||
|
|
||
| const SERVER_RUNTIME_LOADER = `${SERVER_RUNTIME_ENV}/loader`; | ||
|
|
||
| const DEFAULT_SERVER_PREFIX = "SERVER_"; | ||
| const DEFAULT_CLIENT_PREFIX = "CLIENT_"; | ||
|
|
||
| const SERVER_ONLY_MODULE = `throw new Error('Attempt to load server-only environment variables in client runtime.');`; | ||
|
|
||
| const SERVER_RUNTIME_CODE = `import load from '${SERVER_RUNTIME_LOADER}'; | ||
|
|
||
| export default new Proxy({}, { | ||
| get(_, key) { | ||
| return load(key); | ||
| }, | ||
| })`; | ||
|
|
||
| function convertObjectToModule(object: Record<string, string>): string { | ||
| let result = ""; | ||
| for (const key in object) { | ||
| result += `export const ${key} = ${JSON.stringify(object[key])};`; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| export function envPlugin(options?: EnvPluginOptions): Plugin { | ||
| const currentOptions = options ?? {}; | ||
| let env: string; | ||
| const serverPrefix = currentOptions.server?.prefix ?? DEFAULT_SERVER_PREFIX; | ||
| const clientPrefix = currentOptions.client?.prefix ?? DEFAULT_CLIENT_PREFIX; | ||
| const runtime = options?.server?.runtime ?? "node"; | ||
| const runtimeCode = runtime in LOADERS ? LOADERS[runtime as keyof typeof LOADERS] : runtime; | ||
|
|
||
| return { | ||
| name: "solid-start:env", | ||
| enforce: "pre", | ||
| configResolved(config) { | ||
| env = config.mode !== "production" ? "development" : "production"; | ||
| }, | ||
| resolveId(id) { | ||
| if ( | ||
| id === SERVER_ENV || | ||
| id === CLIENT_ENV || | ||
| id === SERVER_RUNTIME_ENV || | ||
| id === SERVER_RUNTIME_LOADER | ||
| ) { | ||
| return id; | ||
| } | ||
| return undefined; | ||
| }, | ||
| load(id, opts) { | ||
| if (id === SERVER_ENV) { | ||
| if (!opts?.ssr) { | ||
| return SERVER_ONLY_MODULE; | ||
| } | ||
| const vars = currentOptions.server?.load | ||
| ? currentOptions.server.load(env) | ||
| : loadEnv(env, '.', serverPrefix); | ||
| return convertObjectToModule(vars); | ||
| } | ||
| if (id === CLIENT_ENV) { | ||
| const vars = currentOptions.client?.load | ||
| ? currentOptions.client.load(env) | ||
| : loadEnv(env, '.', clientPrefix); | ||
| return convertObjectToModule(vars); | ||
| } | ||
| if (id === SERVER_RUNTIME_LOADER) { | ||
| if (!opts?.ssr) { | ||
| return SERVER_ONLY_MODULE; | ||
| } | ||
| return runtimeCode; | ||
| } | ||
| if (id === SERVER_RUNTIME_ENV) { | ||
| if (!opts?.ssr) { | ||
| return SERVER_ONLY_MODULE; | ||
| } | ||
| return SERVER_RUNTIME_CODE; | ||
| } | ||
| return undefined; | ||
| }, | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.