bulletproof-react icon indicating copy to clipboard operation
bulletproof-react copied to clipboard

.env validation on build

Open ytori opened this issue 1 year ago • 0 comments
trafficstars

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

ytori avatar May 28 '24 11:05 ytori