Skip to content

.env validation on build #174

Description

@ytori

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
  };
});

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions