Skip to content
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

Expose validation function for scripts not bundled with Vite #34

Open
aweebit opened this issue Mar 2, 2025 · 0 comments
Open

Expose validation function for scripts not bundled with Vite #34

aweebit opened this issue Mar 2, 2025 · 0 comments

Comments

@aweebit
Copy link

aweebit commented Mar 2, 2025

I want the same environment variable loading and validation to happen in code bundled with Vite for SSR and before running command line tools such as kysely-ctl.

For the environment preloading, I use Node CLI's --import option like this:

node --import ./loadEnv.js node_modules/kysely-ctl/dist/bin.js

Right now, this is how my loadEnv.js file looks:

// @ts-check

import { ValidateEnv } from '@julr/vite-plugin-validate-env';

const { config } = ValidateEnv();
const configHandler = typeof config === 'function' ? config : config?.handler;
const envDefine = (
  await configHandler?.(
    { root: import.meta.dirname },
    { command: 'build', mode: 'development' },
  )
)?.define;
const env =
  envDefine &&
  Object.keys(envDefine).reduce((env, key) => {
    env[key.replace('import.meta.env.', '')] = JSON.parse(envDefine[key]);
    return env;
  }, {});

Object.assign(process.env, env);

It would be great if it could be simplified to something like this:

// @ts-check

import { validateEnv } from '@julr/vite-plugin-validate-env';

const env = await validateEnv(
  { root: import.meta.dirname },
  { command: 'build', mode: 'development' },
);

Object.assign(process.env, env);

I could submit a PR for this feature if you agree it's a reasonable addition to the library.

@aweebit aweebit changed the title Expose validation Expose validation function for scripts not bundled with Vite Mar 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant