Skip to content

Commit 3393c97

Browse files
committed
Expand .env variables
1 parent 750ebaf commit 3393c97

File tree

4 files changed

+1708
-713
lines changed

4 files changed

+1708
-713
lines changed

README.md

+23
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,29 @@
22

33
Monorepo containing setup for a best-practice React application scaffolding
44

5+
### ENV variables
6+
7+
You can use environment variables to configure build scripts.
8+
9+
Here's the list of supported variables with their defaults:
10+
11+
```
12+
SOURCE_DIR=src
13+
BUILD_DIR=build
14+
SOURCEMAPS=false
15+
```
16+
17+
You can override any of these variables with `.env` files. We support these formats (in the order of inclusion):
18+
19+
```
20+
.env.${NODE_ENV}.local
21+
.env.${NODE_ENV}
22+
.env.local
23+
.env
24+
```
25+
26+
The order of inclusion means that anything in `.env` will override content in e.g. `.env.development.local`.
27+
528
### DLLs
629

730
DLLs precompile libraries to save time for development builds and re-builds.

packages/more/package.json

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"version": "0.0.4",
44
"description": "Modus React App Scaffolding",
55
"bin": "bin/more.js",
6+
"engines": {
7+
"node": ">=8.9.3"
8+
},
69
"scripts": {
710
"test": "echo \"Error: no test specified\" && exit 1"
811
},
@@ -23,6 +26,7 @@
2326
"cross-spawn": "^6.0.5",
2427
"css-loader": "1.0.1",
2528
"dotenv": "^6.2.0",
29+
"dotenv-expand": "^4.2.0",
2630
"eslint": "5.9.0",
2731
"file-loader": "2.0.0",
2832
"find-file-up": "^2.0.1",

packages/more/utils/env.js

+23-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
1+
const fs = require('fs');
12
const path = require('path');
2-
const find = require('find-file-up');
3+
const expand = require('dotenv-expand');
4+
const dotenv = require('dotenv');
35

4-
// Find .env or .env.local
5-
const envFile =
6-
find.sync('.env') ||
7-
find.sync('.env.local') ||
8-
path.resolve(__dirname, '..', '.env.local');
6+
const { NODE_ENV, INIT_CWD: projectRoot } = process.env;
97

10-
require('dotenv').config({ path: envFile });
8+
const dotenvFiles = [
9+
path.resolve(__dirname, '..', '.env.local'),
10+
path.join(projectRoot, `.env.${NODE_ENV}.local`),
11+
path.join(projectRoot, `.env.${NODE_ENV}`),
12+
NODE_ENV !== 'test' && path.join(projectRoot, `.env.local`),
13+
path.join(projectRoot, `.env`),
14+
].filter(Boolean);
15+
16+
// Load environment variables from .env* files.
17+
// Use local .env as defaults
18+
dotenvFiles.forEach(dotenvFile => {
19+
if (fs.existsSync(dotenvFile)) {
20+
expand(
21+
dotenv.config({
22+
path: dotenvFile,
23+
})
24+
);
25+
}
26+
});

0 commit comments

Comments
 (0)