npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vite-plugin-runtime-env

v0.1.1

Published

Vite plugin which enables you to configure your environment variables when deploying your app.

Downloads

2,066

Readme

vite-plugin-runtime-env

Test Status npm License

Inject environment variables on runtime rather than build time.

Installation

npm install --save-dev vite-plugin-runtime-env

Usage

First you need to add the plugin to your vite config.

import runtimeEnv from 'vite-plugin-runtime-env';

export default defineConfig({
  plugins: [
    runtimeEnv(),
  ]
});

When building your app, the plugin replaces all occurences of import.meta.env.VITE_MY_VARIABLE with somethink like window.env.VITE_MY_VARIABLE and inserts a mapping into index.html. The mapping contains placeholder variables like ${VITE_MY_VARIABLE} which can be substituted using envsubst or npx envsub when deploying your app.

cp dist/index.html index.html.template
envsubst < index.html.template > dist/index.html

Or, when Node.js is available in your deployment environment:

npx envsub dist/index.html

Both examples assume that your variables are defined in the deploy environment. The node tool also supports .env files as arguments.

Configuration

variableName

The variable name to be used for replacements. Defaults to 'window.env'.

Example:

import runtimeEnv from 'vite-plugin-runtime-env';

export default defineConfig({
  plugins: [
    runtimeEnv({
        varialbeName: 'window.myCustomEnv',
    }),
  ]
});

injectHtml

Wheter to inject the variables map into index.html. Sometimes it can be handy to not have the map in index.html and load an external javascript file which sets the object. Defaults to true.

Example:

import runtimeEnv from 'vite-plugin-runtime-env';

export default defineConfig({
  plugins: [
    runtimeEnv({
        injectHtml: false,
    }),
  ]
});

substitutionSyntax

The syntax for inserted placeholders. This should match the tool you are using to substitute the variables on deployment. Valid values are 'dollar-basic' for $MYVAR, 'dollar-curly' for ${MYVAR} and 'handlebars' for {{MYVAR}}. Defaults to 'dollar-curly'.

Example:

import runtimeEnv from 'vite-plugin-runtime-env';

export default defineConfig({
  plugins: [
    runtimeEnv({
        substitutionSyntax: 'handlebars',
    }),
  ]
});

Now you can use a custom script using handlebars to inject the variables.

ignoreEnv

Variable names to be ignored by this plugin. They will remain in the code and will be statically replaced by Vite on build time. This can be useful for informations which are known on build time and not be configurable on runtime, like the version number of the current build. Valid values are an array of strings, containing each variable to be ignored, or a function which gets each variable as an input and return true if this variable should be skipped.

Example:

import runtimeEnv from 'vite-plugin-runtime-env';

export default defineConfig({
  plugins: [
    runtimeEnv({
        ignoreEnv: ['VITE_CURRENT_VERSION'],
    }),
  ]
});

Example using a function:

import runtimeEnv from 'vite-plugin-runtime-env';

export default defineConfig({
  plugins: [
    runtimeEnv({
        ignoreEnv: env => env.startsWith('VITE_STATIC_'),
    }),
  ]
});

Motivation

If your project want to adhere to the 12 factor principles it can be cumbersome that Vite replaces your environment variables with static content on build time. Following the principle „I – Config” we want to store configurations in the environment. And funnily enough, this is also implied by the name „environment variable”. Respecting these principles can be useful when running your app in a containerized setup like Docker or Kubernetes.

A workaround would be to run vite build when deploying the app. But this may slow down our deployments, especially when deploying multiple instances and each one builds the app for its own. Also, this would be contrary to principle „V – Build, release, run” which requires strict separation between the build, release, and run stages.

To be able to create a single bundle and run the same bundle in different environments like testing, staging and production, we need to be able to pass some configurations when the app is deployed. After a lot of projects where each solves this same problem with its own custom workaround, I created this plugin to rule them all.