All Projects → ElMassimo → vite-plugin-environment

ElMassimo / vite-plugin-environment

Licence: MIT license
Easily expose environment variables in Vite.js

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to vite-plugin-environment

vite-plugin-webfont-dl
⚡ Webfont Download Vite Plugin - Make your Vite site load faster
Stars: ✭ 69 (+21.05%)
Mutual labels:  vite, vitejs, vite-plugin
vite-plugin-ssr
Like Next.js / Nuxt but as do-one-thing-do-it-well Vite plugin.
Stars: ✭ 1,703 (+2887.72%)
Mutual labels:  vite, vitejs, vite-plugin
vite-plugins
🌱 为社区尽一份绵薄之力
Stars: ✭ 63 (+10.53%)
Mutual labels:  vite, vitejs, vite-plugin
admin-one-vue-tailwind
Free Vue.js 3.x Tailwind 3.x admin dashboard template with dark mode. Vite builds. Pinia state. Laravel integration available
Stars: ✭ 742 (+1201.75%)
Mutual labels:  vite, vitejs
webpack-dotenv-plugin
Use dotenv with webpack.
Stars: ✭ 53 (-7.02%)
Mutual labels:  environment-variables, env
read-env
🔧 Transform environment variables into JSON object with sanitized values.
Stars: ✭ 60 (+5.26%)
Mutual labels:  environment-variables, env
preview-pro
Use pro-layout in vitejs. preview https://sendya.github.io/preview-pro/index.html
Stars: ✭ 71 (+24.56%)
Mutual labels:  vite, vitejs
image-optimizer
A free and open source tool for optimizing images and vector graphics.
Stars: ✭ 740 (+1198.25%)
Mutual labels:  vite, vitejs
django-vite
Integration of ViteJS in a Django project.
Stars: ✭ 201 (+252.63%)
Mutual labels:  vite, vitejs
vite-plugin-banner
A banner plugin for Vite. It can adds a banner to the top of each generated chunk.
Stars: ✭ 39 (-31.58%)
Mutual labels:  vite, vite-plugin
envman
Manage your .env configuration easily
Stars: ✭ 20 (-64.91%)
Mutual labels:  environment-variables, env
env-dot-prop
♻️ Get, set, or delete nested properties of process.env using a dot path
Stars: ✭ 31 (-45.61%)
Mutual labels:  environment-variables, env
envclasses
envclasses is a library to map fields on dataclass object to environment variables.
Stars: ✭ 26 (-54.39%)
Mutual labels:  environment-variables, env
vite-plugin-radar
All in one analytics loader for vite
Stars: ✭ 64 (+12.28%)
Mutual labels:  vite, vite-plugin
vite-plugin-vue-gql
⚡ GraphQL Tags for your Vue SFC ⚡
Stars: ✭ 188 (+229.82%)
Mutual labels:  vite, vitejs
envyable
The simplest yaml to ENV config loader.
Stars: ✭ 78 (+36.84%)
Mutual labels:  environment-variables, env
vite-plugin-restart
Custom files/globs to restart Vite server
Stars: ✭ 92 (+61.4%)
Mutual labels:  vite, vite-plugin
vite-plugin-dts
A vite plugin for generating `.d.ts` files.
Stars: ✭ 539 (+845.61%)
Mutual labels:  vite, vite-plugin
checkdotenv
Verify environment variables presence for Node JS.
Stars: ✭ 12 (-78.95%)
Mutual labels:  environment-variables, env
exenv
Exenv makes loading environment variables from external sources easy.
Stars: ✭ 35 (-38.6%)
Mutual labels:  environment-variables, env

vite-plugin-environment

Expose environment variables to your client code in Vite.js


Why? 🤔

Although Vite.js provides its own mechanism for exposing environment variables through import.meta.env, sometimes it's not possible or desirable to prefix variables with VITE_.

This plugin is a shorthand for exposing environment variables by configuring define.

It provides the same functionality as webpack's EnvironmentPlugin, but for Vite.js.

Installation 💿

Install the package as a development dependency:

npm i -D vite-plugin-environment # yarn add -D vite-plugin-environment

Usage 🚀

You can provide a list of environment variable names to expose to your client code:

import { defineConfig } from 'vite'
import EnvironmentPlugin from 'vite-plugin-environment'

export default defineConfig({
  plugins: [
    EnvironmentPlugin(['API_KEY', 'DEBUG']),
  ],
})

And then use them as:

const apiKey = process.env.API_KEY

Usage with default values

You may instead provide an object which maps keys to their default values.

The default value for a key is only used if the variable is not defined.

EnvironmentPlugin({
  // Uses 'development' if the NODE_ENV environment variable is not defined.
  NODE_ENV: 'development',

  // Have in mind that variables coming 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,
}),

Use null for optional variables, or undefined for variables that must be provided.

Configuration ⚙️

Have in mind that you can add the plugin several times—passing different options to load different sets of variables.

Loading prefixed variables

In some cases, it's useful to load all environment variables with a certain prefix.

You can achieve that by passing 'all' and providing the prefix option.

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

and then use it as usual:

process.env.VUE_APP_NOT_SECRET_CODE

Exposing variables differently

When porting apps to Vite or using SSR it can be useful to expose variables in process.env, which is the default.

In other cases, you may use the defineOn option to expose them in a different object, such as import.meta.env.

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

and then use it as:

const version = import.meta.env.APP_VERSION

Ignoring .env files

By default the plugin will load .env files using the same strategy as Vite.js.

If you want to ignore .env files and only use values in process.env, you can opt out:

EnvironmentPlugin(['API_KEY'], { loadEnvFiles: false }),

Inside the box 📦

The first example in this README is equivalent to manually configuring:

import { defineConfig } from 'vite'

export default defineConfig({
  define: {
    'process.env.API_KEY': JSON.stringify(process.env.API_KEY),
    'process.env.DEBUG': JSON.stringify(process.env.DEBUG),
  }
})

except it will also use any variables provided by your .env files, and will fail if any of the specified variables is not defined.

Acknowledgements

I created this library only because I wanted something that:

  • Reused Vite's loadEnv functionality, making the library very light (no dependencies).
  • Allowed to provide a subset of variables to expose, and their defaults.

The following libraries might be helpful depending on your use case:

License

This library is available as open source under the terms of the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].