Coder Social home page Coder Social logo

hizo / vite-plugin-env-config Goto Github PK

View Code? Open in Web Editor NEW

This project forked from geprog/vite-plugin-env-config

0.0 0.0 0.0 312 KB

Vite Plugin for providing config from environment variables at runtime

Home Page: https://www.npmjs.com/package/@geprog/vite-plugin-env-config

License: MIT License

JavaScript 3.40% TypeScript 96.60%

vite-plugin-env-config's Introduction

@geprog/vite-plugin-env-config

npm version vite peer dependency version

Vite plugin for providing configurations from environment variables at runtime.

The generated template can be populated with envsubst in production.

Usage

Add envConfig plugin to vite.config.js / vite.config.ts and provide a list of environment variable names:

// vite.config.js / vite.config.ts
import { envConfig } from '@geprog/vite-plugin-env-config';

export default {
  plugins: [envConfig({ variables: ['BACKEND_URL'] })],
};

To access the environment variables use the built-in getter:

import { getEnvConfig } from '@geprog/vite-plugin-env-config';

const backendURL = getEnvConfig('BACKEND_URL');

For production use envsubst as outlined in the next section.

Motivation

To adhere to the principles of the twelve-factor app you might need to access environment variables that are set when your frontend server starts. Instead of building your frontend on startup, you can use a config template like the one above and populate it using envsubst:

CMD ["/bin/sh", "-c", "envsubst < ./dist/env-config.template.js > ./dist/env-config.js && exec nginx -g 'daemon off;'"]

@geprog/vite-plugin-env-config generates the required template from a list of variable names and provides the already populated file via the dev-server during development.

vite-plugin-env-config's People

Contributors

lukashass avatar renovate[bot] avatar anbraten avatar mariusheine avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.