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

.env validation on build #174

Open
ytori opened this issue May 28, 2024 · 0 comments
Open

.env validation on build #174

ytori opened this issue May 28, 2024 · 0 comments

Comments

@ytori
Copy link

ytori commented May 28, 2024

Thanks for this very nice and useful repository !

How about validation of .env at build time when Vite command is build?

As of now, I understand that the .env value is validated by Zod when the application is running.
However, I believe that validating on build is better to prevent misconfigured application from being deployed to the production environment

The following snippet is just an idea.

const validateEnv = async (command: 'build' | 'serve', mode: string) => {
  if (command !== 'build') {
    return;
  }

  const env = loadEnv(mode, process.cwd(), '');
  Object.assign(import.meta, { env });
  await import('./src/config/env');
};

export default defineConfig(async ({ command, mode }) => {
  void (await validateEnv(command, mode));

  return {
    // omit config
  };
});
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