vite-plugin-environment

repository·main·Indexed 41 days ago

https://github.com/elmassimo/vite-plugin-environment

A Vite plugin that exposes environment variables to client-side code without requiring the VITE_ prefix. It allows exposing specific variables, loading all variables with a specific prefix, and defining default values. Variables can be exposed on process.env by default or import.meta.env using the defineOn option. Version 1.1.4.

Tokens
1.7K
Snippets
9
Records
10
Agent score
1%

What's inside vite-plugin-environment

  1. Expose specific environment variables to client code

    main

    To expose a specific list of environment variables, pass an array of variable names to EnvironmentPlugin. These variables will be accessible in your client code via process.env.

    import { defineConfig } from 'vite'
    import EnvironmentPlugin from 'vite-plugin-environment'
    
    export default defineConfig({
      plugins: [
        EnvironmentPlugin(['API_KEY', 'DEBUG']),
      ],
    })

    Usage in client code:

    const apiKey = process.env.API_KEY
  2. Define environment variable defaults

    main

    When passing an object as the first argument to EnvironmentPlugin, you can specify default values for variables. This prevents the build from failing if a variable is missing from the environment.

    • Use undefined for required variables: The build will throw an error if the variable is missing.
    • Use null for optional variables: The variable will be assigned null if missing.
    • Use a string for a specific default value.

    Note: While booleans or numbers can technically be passed, all process.env values are strings; using other types may lead to subtle bugs.

    import EnvironmentPlugin from 'vite-plugin-environment'
    
    export default EnvironmentPlugin({
      API_URL: 'https://api.example.com', // Default string
      OPTIONAL_KEY: null,                 // Optional variable
      REQUIRED_KEY: undefined,            // Required variable (throws error if missing)
    })
  3. Load all variables with a specific prefix

    main

    To load all environment variables that start with a specific prefix, pass the string 'all' as the first argument and provide a prefix option in the configuration object.

    EnvironmentPlugin('all', { prefix: 'VUE_APP_' })
    EnvironmentPlugin('all', { prefix: 'REACT_APP_' })

    Usage:

    process.env.VUE_APP_NOT_SECRET_CODE
  4. Expose variables to import.meta.env instead of process.env

    main

    By default, variables are exposed on process.env. To expose them on a different object, such as import.meta.env (useful for SSR or porting apps), use the defineOn option.

    EnvironmentPlugin({ APP_VERSION: 'local' }, { defineOn: 'import.meta.env' })

    Usage:

    const version = import.meta.env.APP_VERSION
  5. Use default values for environment variables

    main

    You can pass an object to EnvironmentPlugin to map keys to default values. The default value is only used if the environment variable is not defined.

    • Use undefined for required variables: the build will fail if the variable is missing.
    • Use null for optional variables: the build will not fail if the variable is missing.
    • Note: Variables coming from process.env are always strings.
    EnvironmentPlugin({
      // Uses 'development' if the NODE_ENV environment variable is not defined.
      NODE_ENV: 'development',
    
      // Variables from process.env are always strings.
      DEBUG: 'false',
    
      // Required: will fail if the API_KEY environment variable is not provided.
      API_KEY: undefined, 
    
      // Optional: will not fail if the APP_VERSION environment variable is missing.
      APP_VERSION: null,
    })
  6. Configure EnvironmentPlugin options

    main

    The EnvironmentPlugin accepts an optional EnvOptions object to control how variables are loaded and exposed:

    • prefix (string, default: ''): Only variables starting with this prefix will be made available. Useful for filtering process.env.
    • defineOn (string, default: 'process.env'): The object path where variables will be defined in your client code (e.g., 'import.meta.env').
    • loadEnvFiles (boolean, default: true): Whether to use Vite's loadEnv to load variables from .env files. If set to false, it falls back to using only process.env filtered by the prefix.
    EnvironmentPlugin('all', { 
      prefix: 'VUE_APP_', 
      defineOn: 'import.meta.env', 
      loadEnvFiles: true 
    })
  7. Use EnvironmentPlugin to expose environment variables

    main

    The EnvironmentPlugin function allows you to expose specific environment variables (or all variables matching a prefix) to your client-side code via Vite's define mechanism. You can choose to expose them on process.env (default) or import.meta.env.

    import EnvironmentPlugin from 'vite-plugin-environment'
    
    // Expose specific variables with default values
    export default EnvironmentPlugin(['NODE_ENV', 'API_URL'], { 
      defineOn: 'import.meta.env' 
    })
    
    // Or expose all variables matching a prefix
    export default EnvironmentPlugin('all', { prefix: 'VUE_APP_' })