├── .drone.yml ├── .gitignore ├── LICENCE ├── README.md ├── examples ├── create-react-app │ ├── .env │ ├── .gitignore │ ├── README.md │ ├── package.json │ ├── public │ │ ├── favicon.ico │ │ ├── index.html │ │ └── manifest.json │ ├── src │ │ ├── App.js │ │ ├── index.css │ │ ├── index.js │ │ └── serviceWorker.js │ └── yarn.lock └── next.js │ ├── .env │ ├── .gitignore │ ├── Dockerfile │ ├── README.md │ ├── package.json │ ├── pages │ └── index.js │ ├── public │ └── env.js │ └── yarn.lock └── packages └── node ├── .gitignore ├── babel.config.js ├── dist ├── cli-index.js ├── cli.js ├── index.js └── types.d.ts ├── package.json ├── rollup.config.js ├── src ├── __tests__ │ ├── dotenv.spec.js │ └── index.spec.js ├── cli-index.js ├── cli.js └── index.js ├── testing └── mockEnv.js └── yarn.lock /.drone.yml: -------------------------------------------------------------------------------- 1 | --- 2 | #-------------------------- 3 | # Gateway service 4 | #-------------------------- 5 | kind: pipeline 6 | type: docker 7 | name: default 8 | steps: 9 | - name: test 10 | image: node:13-alpine 11 | environment: 12 | COVERALLS_SERVICE_NAME: 'drone' 13 | COVERALLS_GIT_BRANCH: ${DRONE_BRANCH} 14 | COVERALLS_SERVICE_NUMBER: ${DRONE_TAG=${DRONE_COMMIT}} 15 | COVERALLS_REPO_TOKEN: 16 | from_secret: COVERALLS_REPO_TOKEN 17 | commands: 18 | - cd packages/node 19 | - yarn install 20 | - yarn test:ci 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /LICENCE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 John Barton 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # React Env - Runtime Environment Configuration 2 | 3 | [![Build Status](https://cloud.drone.io/api/badges/andrewmclagan/react-env/status.svg)](https://cloud.drone.io/andrewmclagan/react-env) 4 | [![npm version](https://badge.fury.io/js/%40beam-australia%2Freact-env.svg)](https://badge.fury.io/js/%40beam-australia%2Freact-env) 5 | [![Coverage Status](https://coveralls.io/repos/github/beam-australia/react-env/badge.svg)](https://coveralls.io/github/beam-australia/react-env) 6 | 7 | Populates your environment from `.env` files at **run-time** rather than **build-time**. 8 | 9 | - Isomorphic - Server and browser compatible. 10 | - Supports static site generation. 11 | - Supports multiple `.env` files. 12 | 13 | ## README 14 | 15 | - [Examples](#examples) 16 | - [Getting started](#getting-started) 17 | - [File priority](#env-file-order-of-priority) 18 | - [Common use cases](#common-use-cases) 19 | - [Environment specific config](#environment-specific-config) 20 | - [Specifing an env file](#Specifing-an-env-file) 21 | - [Using with Docker entrypoint](#using-with-docker-entrypoint) 22 | - [Arguments and parameters](#arguments-and-parameters) 23 | 24 | ### Examples 25 | 26 | - Example using [Next.js](examples/next.js/README.md) (see README.md) 27 | - Example using [Create React APP](examples/create-react-app/README.md) (see README.md) 28 | 29 | ### Getting started 30 | 31 | This package generates a `__ENV.js` file from multiple `.env` files that contains white-listed environment variables that have a `REACT_APP_` prefix. 32 | 33 | ```html 34 | 24 | 25 | 26 | 27 |
28 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /examples/create-react-app/public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | } 10 | ], 11 | "start_url": ".", 12 | "display": "standalone", 13 | "theme_color": "#000000", 14 | "background_color": "#ffffff" 15 | } 16 | -------------------------------------------------------------------------------- /examples/create-react-app/src/App.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import env from "@beam-australia/react-env"; 3 | 4 | const styles = { 5 | padding: 30, 6 | margin: 30, 7 | backgroundColor: "rgba(238, 238, 238, 0.39)", 8 | fontFamily: "monospace" 9 | }; 10 | 11 | export default class extends React.Component { 12 | state = { 13 | todos: [] 14 | }; 15 | 16 | async componentDidMount() { 17 | const response = await fetch(`${env("API_HOST")}/todos`); 18 | const todos = await response.json(); 19 | this.setState({ todos }); 20 | } 21 | 22 | render() { 23 | return ( 24 |
25 |

React Env - {env("FRAMEWORK")}

26 |

Runtime environment variables

27 |
28 |

Environment

29 |
30 |
{JSON.stringify(env(), null, 2)}
31 |
32 |
33 |

Todos

34 | 39 |
40 | ); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /examples/create-react-app/src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", 4 | "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | code { 11 | font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New", 12 | monospace; 13 | } 14 | -------------------------------------------------------------------------------- /examples/create-react-app/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import * as serviceWorker from './serviceWorker'; 6 | 7 | ReactDOM.render(, document.getElementById('root')); 8 | 9 | // If you want your app to work offline and load faster, you can change 10 | // unregister() to register() below. Note this comes with some pitfalls. 11 | // Learn more about service workers: https://bit.ly/CRA-PWA 12 | serviceWorker.unregister(); 13 | -------------------------------------------------------------------------------- /examples/create-react-app/src/serviceWorker.js: -------------------------------------------------------------------------------- 1 | // This optional code is used to register a service worker. 2 | // register() is not called by default. 3 | 4 | // This lets the app load faster on subsequent visits in production, and gives 5 | // it offline capabilities. However, it also means that developers (and users) 6 | // will only see deployed updates on subsequent visits to a page, after all the 7 | // existing tabs open on the page have been closed, since previously cached 8 | // resources are updated in the background. 9 | 10 | // To learn more about the benefits of this model and instructions on how to 11 | // opt-in, read https://bit.ly/CRA-PWA 12 | 13 | const isLocalhost = Boolean( 14 | window.location.hostname === 'localhost' || 15 | // [::1] is the IPv6 localhost address. 16 | window.location.hostname === '[::1]' || 17 | // 127.0.0.1/8 is considered localhost for IPv4. 18 | window.location.hostname.match( 19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 20 | ) 21 | ); 22 | 23 | export function register(config) { 24 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 25 | // The URL constructor is available in all browsers that support SW. 26 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href); 27 | if (publicUrl.origin !== window.location.origin) { 28 | // Our service worker won't work if PUBLIC_URL is on a different origin 29 | // from what our page is served on. This might happen if a CDN is used to 30 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374 31 | return; 32 | } 33 | 34 | window.addEventListener('load', () => { 35 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 36 | 37 | if (isLocalhost) { 38 | // This is running on localhost. Let's check if a service worker still exists or not. 39 | checkValidServiceWorker(swUrl, config); 40 | 41 | // Add some additional logging to localhost, pointing developers to the 42 | // service worker/PWA documentation. 43 | navigator.serviceWorker.ready.then(() => { 44 | console.log( 45 | 'This web app is being served cache-first by a service ' + 46 | 'worker. To learn more, visit https://bit.ly/CRA-PWA' 47 | ); 48 | }); 49 | } else { 50 | // Is not localhost. Just register service worker 51 | registerValidSW(swUrl, config); 52 | } 53 | }); 54 | } 55 | } 56 | 57 | function registerValidSW(swUrl, config) { 58 | navigator.serviceWorker 59 | .register(swUrl) 60 | .then(registration => { 61 | registration.onupdatefound = () => { 62 | const installingWorker = registration.installing; 63 | if (installingWorker == null) { 64 | return; 65 | } 66 | installingWorker.onstatechange = () => { 67 | if (installingWorker.state === 'installed') { 68 | if (navigator.serviceWorker.controller) { 69 | // At this point, the updated precached content has been fetched, 70 | // but the previous service worker will still serve the older 71 | // content until all client tabs are closed. 72 | console.log( 73 | 'New content is available and will be used when all ' + 74 | 'tabs for this page are closed. See https://bit.ly/CRA-PWA.' 75 | ); 76 | 77 | // Execute callback 78 | if (config && config.onUpdate) { 79 | config.onUpdate(registration); 80 | } 81 | } else { 82 | // At this point, everything has been precached. 83 | // It's the perfect time to display a 84 | // "Content is cached for offline use." message. 85 | console.log('Content is cached for offline use.'); 86 | 87 | // Execute callback 88 | if (config && config.onSuccess) { 89 | config.onSuccess(registration); 90 | } 91 | } 92 | } 93 | }; 94 | }; 95 | }) 96 | .catch(error => { 97 | console.error('Error during service worker registration:', error); 98 | }); 99 | } 100 | 101 | function checkValidServiceWorker(swUrl, config) { 102 | // Check if the service worker can be found. If it can't reload the page. 103 | fetch(swUrl) 104 | .then(response => { 105 | // Ensure service worker exists, and that we really are getting a JS file. 106 | const contentType = response.headers.get('content-type'); 107 | if ( 108 | response.status === 404 || 109 | (contentType != null && contentType.indexOf('javascript') === -1) 110 | ) { 111 | // No service worker found. Probably a different app. Reload the page. 112 | navigator.serviceWorker.ready.then(registration => { 113 | registration.unregister().then(() => { 114 | window.location.reload(); 115 | }); 116 | }); 117 | } else { 118 | // Service worker found. Proceed as normal. 119 | registerValidSW(swUrl, config); 120 | } 121 | }) 122 | .catch(() => { 123 | console.log( 124 | 'No internet connection found. App is running in offline mode.' 125 | ); 126 | }); 127 | } 128 | 129 | export function unregister() { 130 | if ('serviceWorker' in navigator) { 131 | navigator.serviceWorker.ready.then(registration => { 132 | registration.unregister(); 133 | }); 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /examples/next.js/.env: -------------------------------------------------------------------------------- 1 | # Available to the browser and server 2 | REACT_APP_FRAMEWORK='Next.js' 3 | REACT_APP_VERSION='1.0.0' 4 | REACT_APP_SSR=true 5 | REACT_APP_FOO='bar' 6 | REACT_APP_BAR='foo' 7 | 8 | # Available to server only 9 | API_HOST='https://jsonplaceholder.typicode.com' 10 | HIDDEN_SECRET=42 -------------------------------------------------------------------------------- /examples/next.js/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # next.js 12 | /.next/ 13 | /out/ 14 | 15 | # production 16 | /build 17 | 18 | # misc 19 | .DS_Store 20 | *.pem 21 | 22 | # debug 23 | npm-debug.log* 24 | yarn-debug.log* 25 | yarn-error.log* 26 | 27 | # local env files 28 | .env.local 29 | .env.development.local 30 | .env.test.local 31 | .env.production.local 32 | 33 | # vercel 34 | .vercel 35 | 36 | # env 37 | public/__ENV.js -------------------------------------------------------------------------------- /examples/next.js/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM node:10-alpine AS build 2 | 3 | WORKDIR /var/app 4 | 5 | COPY package.json /var/app 6 | 7 | COPY yarn.lock /var/app 8 | 9 | ENV NODE_ENV=production 10 | 11 | RUN yarn install 12 | 13 | ADD . . 14 | 15 | RUN yarn build 16 | 17 | CMD ["yarn", "start"] 18 | -------------------------------------------------------------------------------- /examples/next.js/README.md: -------------------------------------------------------------------------------- 1 | ## Next.js 2 | 3 | This folder contains an example app using Next.js server rendered framework. 4 | -------------------------------------------------------------------------------- /examples/next.js/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-env-next.js", 3 | "license": "ISC", 4 | "version": "1.0.0", 5 | "scripts": { 6 | "dev": "react-env -- next", 7 | "build": "next build", 8 | "start": "react-env -- next start" 9 | }, 10 | "dependencies": { 11 | "isomorphic-fetch": "latest", 12 | "@beam-australia/react-env": "3.0.4", 13 | "next": "latest", 14 | "react": "latest", 15 | "react-dom": "latest", 16 | "react-env": "latest" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /examples/next.js/pages/index.js: -------------------------------------------------------------------------------- 1 | import "isomorphic-fetch"; 2 | import React from "react"; 3 | import Head from "next/head"; 4 | import env from "@beam-australia/react-env"; 5 | 6 | const styles = { 7 | padding: 30, 8 | margin: 30, 9 | backgroundColor: "rgba(238, 238, 238, 0.39)", 10 | fontFamily: "monospace" 11 | }; 12 | 13 | export default class extends React.Component { 14 | static async getInitialProps() { 15 | const response = await fetch(`${process.env.API_HOST}/todos`); 16 | const todos = await response.json(); 17 | return { todos }; 18 | } 19 | 20 | render() { 21 | return ( 22 |
23 | 24 | React Env 25 |