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

Allow validating env vars without prefix #25

Open
biesbjerg opened this issue Apr 16, 2024 · 1 comment
Open

Allow validating env vars without prefix #25

biesbjerg opened this issue Apr 16, 2024 · 1 comment

Comments

@biesbjerg
Copy link

biesbjerg commented Apr 16, 2024

I'd like to validate env vars coming from .env as well as from the system.

System environment:

EXPORT SENTRY_AUTH_TOKEN=sensitivevalue

.env:

VITE_APP_VAR=publicvalue

vite.config.ts:

export default defineConfig(({ mode, command }) => {
  process.env = {
    ...process.env,
    ...loadEnv(mode, process.cwd(), "VITE_APP_")
  };

  return {
    envPrefix: "VITE_APP_", // Default env prefix
    plugins: [
      // Validate environment variables from .env file prefixed with VITE_APP_
      // that are used in the application and will end up in the bundle
      ValidateEnv({
        validator: "builtin",
        schema: {
          VITE_APP_VAR: Schema.string()
        }
      }),
      [
        // Validate sensitive environment variables from system that doesn't have prefix
        // and should never end up in the bundle
        {
          ...ValidateEnv({
            validator: "builtin",
            schema: {
              SENTRY_AUTH_TOKEN: Schema.string()
            }
          }),
          apply: "build"
        },
        {
          ...sentryVitePlugin({
            authToken: process.env.SENTRY_AUTH_TOKEN // Use sensitive env variable here
          }),
          apply: "build"
        }
      ]
    ]
  };
});

Above won't work because we're only able to validate vars prefixed with envPrefix (VITE_APP_), hence SENTRY_AUTH_TOKEN will always report as missing.

Do you think there's a way to achieve this? Would you accept a PR, and if so, any pointers how you'd like it done?

@biesbjerg biesbjerg changed the title Allow validating all env vars Allow validating env vars without prefix Apr 16, 2024
@biesbjerg
Copy link
Author

What do you think of #26 ?

It loads all environment variables, enabling validation, but only exposes variables (through define) if their keys begin with the configured envPrefix.

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