• Stars
    star
    144
  • Rank 255,590 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 3 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Easily expose environment variables in Vite.js

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.

More Repositories

1

vite_ruby

⚑️ Vite.js in Ruby, bringing joy to your JavaScript experience
Ruby
1,270
star
2

iles

🏝 The joyful site generator
TypeScript
1,074
star
3

vite-plugin-image-presets

πŸ–Ό Image Presets for Vite.js apps
TypeScript
246
star
4

vite-plugin-full-reload

♻️ Automatically reload the page when files are modified
JavaScript
134
star
5

oj_serializers

⚑️ Faster JSON serialization for Ruby on Rails. Easily migrate away from Active Model Serializers.
Ruby
113
star
6

js_from_routes

πŸ›£οΈ Generate path helpers and API methods from your Rails routes
Ruby
98
star
7

types_from_serializers

βœ… Generate TypeScript interfaces from your JSON serializers
Ruby
92
star
8

request_store_rails

πŸ“¦ Per-request global storage for Rails prepared for multi-threaded apps
Ruby
85
star
9

vuex-stores

πŸ—„ Store objects for Vuex, a simple and more fluid API for state-management.
JavaScript
62
star
10

vue-custom-element-example

An example on how to define custom elements using Vue 3
TypeScript
59
star
11

jekyll-vite

⚑️🩸 Use Vite.js in Jekyll as your assets pipeline
Ruby
50
star
12

vite-plugin-stimulus-hmr

⚑️ HMR for Stimulus controllers in Vite.js
TypeScript
50
star
13

mongoid_includes

🌿 Improves eager loading support for Mongoid
Ruby
46
star
14

stimulus-vite-helpers

Helpers to easily load all your Stimulus controllers when using Vite.js
TypeScript
43
star
15

queryable

❔ Gives your queries a home and avoid tucking scopes inside your models
Ruby
42
star
16

capybara-compose

βœ… Easily write fluent integration tests with Capybara in Ruby
Ruby
32
star
17

better_settings

βš™ Settings for Ruby apps – fast, immutable, better.
Ruby
20
star
18

vite-plugin-bugsnag

Report builds and upload source maps to Bugsnag
TypeScript
18
star
19

i18n_multitenant

🌎 Provides a convenient way to use tenant-specific translations
Ruby
18
star
20

vite-plugin-manifest-sri

Subresource Integrity for Vite.js manifest files
JavaScript
15
star
21

resourcerer

✨ Works like magic to dry up your controllers
Ruby
10
star
22

sublime-toggle-dark-mode

🌚🌞 Toggle between dark and light mode in Sublime Text 4
JavaScript
10
star
23

pakiderm

🐘 Pakiderm will never forget the return value
Ruby
7
star
24

presenter_rails

πŸ”­ Expose your view models in a convenient way
Ruby
6
star
25

vite-plugin-erb

Use ERB files in Vite.js projects with a Ruby backend
TypeScript
5
star
26

journeyman

Let your factories use your business logic, making them flexible and easier to update.
Ruby
5
star
27

jekyll-vite-minima

⚑️🩸 Use Vite.js in Jekyll minima theme as your assets pipeline
Ruby
3
star
28

automatic-music-transcription

Automatically exported from code.google.com
C
2
star
29

vite-plugin-xdm

Use XDM in VIte.js
JavaScript
2
star
30

fast-food-mvc

Automatically exported from code.google.com
C#
1
star
31

ElMassimo

1
star
32

vite-vue-router-hmr-repro

Vue
1
star
33

crouton

🍞 Context sensitive notifications for Rails
Ruby
1
star