-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add README.md and comments in cra example
- Loading branch information
Showing
9 changed files
with
100 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ | |
*.html | ||
*.ico | ||
Dockerfile | ||
*.json | ||
*.json | ||
*.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# runtime-env-cli | ||
|
||
[](https://www.npmjs.org/package/runtime-env-cli) | ||
[](https://www.npmjs.org/package/runtime-env-cli) | ||
[](https://github.com/JoseVSeb/runtime-env/issues) | ||
[](https://github.com/JoseVSeb/runtime-env/actions/workflows/release.yaml) | ||
[](https://www.npmjs.org/package/runtime-env-cli) | ||
[](https://github.com/semantic-release/semantic-release) | ||
|
||
## Overview | ||
|
||
`runtime-env-cli` is an NPM package that helps manage environment variables in web applications. It allows environment-agnostic build for frameworks like create-react-app and inject environment variables as `window.env` in html files during server startup. | ||
|
||
## Usage | ||
|
||
### Development Mode | ||
|
||
1. Install `runtime-env-cli` as development dependency. | ||
|
||
pnpm: | ||
|
||
```bash | ||
pnpm i -D runtime-env-cli | ||
``` | ||
|
||
npm: | ||
|
||
```bash | ||
npm i -D runtime-env-cli | ||
``` | ||
|
||
Yarn: | ||
|
||
```bash | ||
pnpm add -D runtime-env-cli | ||
``` | ||
|
||
2. Import `runtime-env-cli` in top-level javascript/typescript file for aliasing `process.env` as `windows.env` in development mode. | ||
|
||
```ts | ||
import "runtime-env-cli"; | ||
``` | ||
|
||
It also provides type-safety for `windows.env`. | ||
|
||
Note: You can make it further type-safe by extending interfaces `ProcessEnv` or `WindowEnv`. | ||
|
||
Use environment variables from `window.env`: | ||
|
||
```ts | ||
window.env.SAMPLE_ENV; | ||
``` | ||
|
||
### Production Mode | ||
|
||
1. Generate static files for your application. | ||
|
||
```bash | ||
npm run build | ||
``` | ||
|
||
2. Install `runtime-env-cli` in hosting environment (if using docker, do this in the last stage). | ||
|
||
```bash | ||
npm i -g runtime-env-cli | ||
``` | ||
|
||
3. Run the cli to inject env into html files before serving files (in docker, do this in entrypoint following by serving files, refer [example](https://github.com/JoseVSeb/runtime-env/tree/main/examples/cra)). | ||
|
||
```bash | ||
runtime-env [options] | ||
``` | ||
|
||
Note: CLI uses `process.env` to get environment while injecting. Any matching environment variables already configured in shell will be read. To use .env files, first load it before running this (refer [dotenv-cli](https://www.npmjs.com/package/dotenv-cli)). | ||
|
||
### CLI Options | ||
|
||
- **-v, --version**: output the version number. | ||
- **-p, --path [paths...]** (optional): glob patterns to match path of html files (default: ["./build/**/*.html"]). | ||
- **-m, --match \<regex\>**: regular expression to match environment variable names to be injected. | ||
- **-h, --help**: display help for command. | ||
|
||
#### example | ||
|
||
```bash | ||
runtime-env --path "./**/*.html" --match "^APP_ENV_" | ||
``` | ||
|
||
This will inject all environment variables prefixed with `APP_ENV_` as `window.env` object into all html files under working directory. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
# environment variable to be used for development server | ||
REACT_APP_SAMPLE_ENV = "This is for development server." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import React from "react"; | ||
|
||
export default function App() { | ||
// Access environment variable from `window.env` object | ||
return <div>REACT_APP_SAMPLE_ENV: {window.env.REACT_APP_SAMPLE_ENV}</div>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters