All Projects → garronej → cra-envs

garronej / cra-envs

Licence: MIT license
⚙️ Bundle env var in CRA at launch time!

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to cra-envs

React Env
Runtime environment variables for react apps.
Stars: ✭ 90 (+100%)
Mutual labels:  create-react-app, environment-variables
Cra Runtime Environment Variables
Guide: Runtime environment variables within Create React App
Stars: ✭ 111 (+146.67%)
Mutual labels:  create-react-app, environment-variables
tutorial-react-docker
Boilerplate React app in Docker container with ENV args
Stars: ✭ 63 (+40%)
Mutual labels:  create-react-app, environment-variables
universal-scripts
Build universal apps without configuration.
Stars: ✭ 23 (-48.89%)
Mutual labels:  create-react-app
spring-boot-create-react-app
Simple template to create a spring boot back-end with create react app front-end combined into a microservice
Stars: ✭ 25 (-44.44%)
Mutual labels:  create-react-app
arnold
🎬 A modern Kodi web interface
Stars: ✭ 16 (-64.44%)
Mutual labels:  create-react-app
dart environment config
Environment specific config generator for Dart and Flutter applications during CI/CD builds
Stars: ✭ 87 (+93.33%)
Mutual labels:  environment-variables
envyable
The simplest yaml to ENV config loader.
Stars: ✭ 78 (+73.33%)
Mutual labels:  environment-variables
rune
tool to query for tokens and passwords for use as environment variables
Stars: ✭ 13 (-71.11%)
Mutual labels:  environment-variables
superconfig
Access environment variables. Also includes presence validation, type coercion and default values.
Stars: ✭ 33 (-26.67%)
Mutual labels:  environment-variables
slack-notifier
Command line utility to send messages with attachments to Slack channels via Incoming Webhooks
Stars: ✭ 61 (+35.56%)
Mutual labels:  environment-variables
env
Package to provide configuration as environment variables for 12 factor applications
Stars: ✭ 31 (-31.11%)
Mutual labels:  environment-variables
backup-suite
Backup database, static files and config to AWS S3 with Cronjob
Stars: ✭ 32 (-28.89%)
Mutual labels:  environment-variables
docker-files
Teracy docker-files project to build common Docker images
Stars: ✭ 87 (+93.33%)
Mutual labels:  create-react-app
netlify-forms-formik
📝 Netlify Forms with Formik and ReCaptcha
Stars: ✭ 33 (-26.67%)
Mutual labels:  create-react-app
webcam-object-detection
Tensorflow.js webcam object detection in React
Stars: ✭ 24 (-46.67%)
Mutual labels:  create-react-app
DBEnvironmentConfiguration
Easily switch between iOS development environments/ configurations
Stars: ✭ 18 (-60%)
Mutual labels:  environment-variables
hotlist
今日热榜(前端)
Stars: ✭ 51 (+13.33%)
Mutual labels:  create-react-app
paerser
No description or website provided.
Stars: ✭ 38 (-15.56%)
Mutual labels:  environment-variables
checkdotenv
Verify environment variables presence for Node JS.
Stars: ✭ 12 (-73.33%)
Mutual labels:  environment-variables

Bundle environment variables in create-react-app at build time launch time!

Motivation

Create-react-app supports environment variable but they are bundled at build time when yarn build is run.
If we want to change anything like the URL of the backend the app should connect to, we have to rebuild, we can't ship customizable Docker image of our CRA apps.

The solution would be to be able to do:

 docker run --env FOO="xyz" my-org/my-create-react-app

Then access FOO:

  • In the code like process.env["FOO"]
  • In public/index.html like <title>%FOO%</title>

cra-envs does just that, in a secure, performant and type safe way.

Features

  • No impact on the startup time.
  • No impact on the Docker image size.
  • Require no network connection at container startups.
  • Secure: It only injects the envs declared in the .env file.
  • It works like you are already used to. It just changes when the envs are injected.
  • EJS support in public/index.html (did you know?).
    This enables for example to conditionally preload one font or another
  • (Optional) Type safe: An env getter is generated so you know what envs are available.

Usecase example

Onyxia-web is a create-react-app distributed as a Docker image.

Sysadmins that would like to deploy Onyxia on their infrastructure can simply use the official Docker image and provide relevant environnement variable to adjust the theme/branding of the website to their usecase.

Here are two deployment example:

Click on the social media preview to access the websites

Documentation

Find 👉here👈 a demo setup of:
cra-envs + create-react-app + TypeScript + Nginx + Docker

More details on how it works

The recommended way to get started with cra-envs is to follow the instructions provided in the cra-envs-demo-app.
Now, if you want to acquire a deeper understanding what the tool does and how you can follow the following steps.

Start by installing the tool:

yarn add cra-envs 

Then declare all the allowed environment variables into the .env file of your project

Example

REACT_APP_FOO="Default value of foo"
REACT_APP_BAR=
REACT_APP_BAZ=
REACT_APP_FIZZ=

Once it's done run the script npx generate-env-getter ( Use npx generate-env-getter js if you your project don't use TypeScript)

It will generate src/env.ts ( or src/env.js) looking like:

/* 
* Automatically generated by cra-envs.
* If you wish to declare a new environment variable declare it in the .env file (prefixed by REACT_APP_)
* then run 'npx generate-env-getter' at the root of your project.
* This file will be updated.
*/
import { getEnvVarValue } from "cra-envs";

export const envNames = [
  "FOO",
  "BAR",
  "BAZ",
  "FIZZ"
] as const;

export type EnvNames = typeof envNames[number];

let env: Record<EnvNames, string> | undefined = undefined;

export function getEnv() {

    if (env === undefined) {
        env = {} as Record<EnvNames, string>;
        for (const envName of envNames) {
            env[envName] = getEnvVarValue(envName);
        }
    }

    return env;

}

(This file should be gitignored)

Now let's test it by creating a .env.local file like:

REACT_APP_BAR="Value of bar defined in .env.local"

And let's do this somewhere in our code:

import { getEnv } from "./env.ts"

console.log(getEnv());

Now if we run REACT_APP_BAZ="Value of baz passed inline" yarn start we get this in the console:

{
    "FOO": "Default value of foo",
    "BAR": "Value of bar defined in .env.local",
    "BAZ": "Value of baz passed inline",
    "FIZZ": ""
}

Now if you run yarn build then BAZ="Value of baz on the server" npx embed-environnement-variables the value of BAZ will be injected in build/index.html (or html/index.html) so that if you start statically serving the build/ dir, for example with serve -s build you will get this in the console:

{
    "FOO": "Default value of foo",
    "BAR": "Value of baz on the server",
    "BAZ": "",
    "FIZZ": ""
}

Note that on the server the environment variable names don't need to be prefixed with REACT_APP_ (they can though). Also note that the script runs very fast and thus represent virtually no overhead when starting your container.
By default embed-environnement-variables does not embed variables defined in .env.local, if you want to include them use: --includes-.env.local or -i.

The next step is to set up a clean Dockerfile where there is both node and Ngnix available.
Node for being able to run npx embed-environnement-variables and Ngnix for serving the app.
It is also important to make sure cra-envs is not bootstrapped by npx in the entrypoint.

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].