-
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
Open
lxsmnsyc
wants to merge
6
commits into
main
Choose a base branch
from
feat-runtime-env
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: add env:*
#2110
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8c9ad3d
feat: add `env:*`
lxsmnsyc 4bb956e
feat(env): add runtime
lxsmnsyc cdbfb59
fix(env): add guards
lxsmnsyc 44cfda7
fix(env): resolver
lxsmnsyc 0ce8739
remove unnecessary code
lxsmnsyc 7415042
Update index.ts
lxsmnsyc 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,106 @@ | ||
| 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?: () => Record<string, string>; | ||
| prefix?: string; | ||
| }; | ||
| client?: { | ||
| load?: () => 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])};`; | ||
| } | ||
| console.log(result); | ||
| 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() | ||
| : loadEnv(env, '.', serverPrefix); | ||
| return convertObjectToModule(vars); | ||
| } | ||
| if (id === CLIENT_ENV) { | ||
| const vars = currentOptions.client?.load | ||
| ? currentOptions.client.load() | ||
| : 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we error on build-time, instead of run-time?
Maybe if it is done after the server-function & tree-shake
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yinonburgansky This actually yields error on build, but not dev.
fun fact: despite the "throw" side-effect, the missing module exports error yields first on runtime.