Skip to content
This repository was archived by the owner on Dec 26, 2022. It is now read-only.

Commit 8f1b49d

Browse files
author
kevalmalde
committed
🔧 Deployment Specific Env.
Checked against existing env.example schema
1 parent aefc1c9 commit 8f1b49d

File tree

11 files changed

+528
-415
lines changed

11 files changed

+528
-415
lines changed

.env.development

Lines changed: 0 additions & 1 deletion
This file was deleted.

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
API_BASE_URL=

.env.production

Lines changed: 0 additions & 1 deletion
This file was deleted.

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
/build
1313

1414
# Environment files
15-
.env.local
15+
.env.*
16+
!.env.example
1617

1718
#others
1819
yarn-error.log

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ This Template is for building React efficient React Frontend Apps. It uses moder
2121

2222
- Package Manager : 🧶 yarn Version 1.22.5
2323
- Compiler : 🤖 Babel Version 7
24-
- Framework : 🔥 React Project (TypeScript) Verison 17
24+
- Framework : 🔥 React Project (TypeScript) Version 17
2525
- Bundler : 📦 Webpack Version 5
2626
- Styling : 💅 styled-components (supports traditional css/scss)
2727
- Component Preview : 🔖 Storybook with Webpack 5 bundler
28-
- Development : eslint, prettier, editorconfig
28+
- Development : 🎨 eslint, prettier, editorconfig
2929
- Testing : 🧪 React Testing Library with Jest
30-
- Environment Variable - ⚙️ Support with environment files
31-
- .env.local - varialbles with secrets (should include in .gitignore)
32-
- .env.development - variables required while development
33-
- .env.production - variables required for production
30+
- Environment Variable -
31+
- APP_ENV - Current build / running environment. Default is local.
32+
- Environment Configuration - ⚙️ Support with deployment specific environment files
33+
- .env.example - Define a schema of what variables should be defined in .env.{your_APP_ENV}. It throws an error if all values are not configured
34+
- Access the env variable in javascript using process.env.<your-env-variable>
3435
# Future Implementations
3536
- Code Spiltting
3637
- Tree Shaking
37-
- cross-env

package.json

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"author": "Tushar Mistry",
77
"license": "ISC",
88
"scripts": {
9-
"start": "webpack serve --config webpack/webpack.config.js --env env=dev",
9+
"start": "cross-env APP_ENV=local webpack serve --config webpack/webpack.config.js --env env=dev",
1010
"build": "yarn clean && webpack --config webpack/webpack.config.js --env env=prod",
1111
"analyze": "yarn clean && webpack --config webpack/webpack.config.js --env env=prod --analyze",
1212
"clean": "rimraf build",
@@ -31,17 +31,17 @@
3131
"styled-components"
3232
],
3333
"dependencies": {
34-
"@babel/core": "^7.14.0",
35-
"@babel/plugin-transform-runtime": "^7.13.15",
36-
"@babel/preset-env": "^7.14.1",
34+
"@babel/core": "^7.14.2",
35+
"@babel/plugin-transform-runtime": "^7.14.2",
36+
"@babel/preset-env": "^7.14.2",
3737
"@babel/preset-react": "^7.13.13",
3838
"@babel/preset-typescript": "^7.13.0",
3939
"@babel/runtime": "^7.14.0",
4040
"@svgr/webpack": "^5.5.0",
4141
"babel-loader": "^8.2.2",
4242
"copy-webpack-plugin": "^8.1.1",
4343
"css-loader": "^5.2.4",
44-
"dotenv": "^9.0.2",
44+
"dotenv-webpack": "^7.0.2",
4545
"html-webpack-plugin": "^5.3.1",
4646
"react": "^17.0.2",
4747
"react-dom": "^17.0.2",
@@ -61,17 +61,18 @@
6161
"@storybook/builder-webpack5": "^6.2.9",
6262
"@storybook/react": "^6.2.9",
6363
"@testing-library/jest-dom": "^5.12.0",
64-
"@testing-library/react": "^11.2.6",
64+
"@testing-library/react": "^11.2.7",
6565
"@types/jest": "^26.0.23",
6666
"@types/react": "^17.0.5",
67-
"@types/react-dom": "^17.0.4",
67+
"@types/react-dom": "^17.0.5",
6868
"@types/styled-components": "^5.1.9",
6969
"@typescript-eslint/eslint-plugin": "^4.23.0",
7070
"@typescript-eslint/parser": "^4.23.0",
71+
"cross-env": "^7.0.3",
7172
"eslint": "^7.26.0",
7273
"eslint-config-airbnb": "18.2.1",
7374
"eslint-config-prettier": "^8.3.0",
74-
"eslint-plugin-import": "^2.22.1",
75+
"eslint-plugin-import": "^2.23.1",
7576
"eslint-plugin-jest": "^24.3.6",
7677
"eslint-plugin-jsx-a11y": "^6.4.1",
7778
"eslint-plugin-prettier": "^3.4.0",
@@ -80,7 +81,7 @@
8081
"husky": "^6.0.0",
8182
"jest": "^26.6.3",
8283
"lint-staged": "^11.0.0",
83-
"netlify-cli": "^3.29.2",
84+
"netlify-cli": "^3.29.15",
8485
"prettier": "^2.3.0",
8586
"react-refresh": "^0.10.0",
8687
"ts-jest": "^26.5.6",

public/index.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
77
<link rel="preconnect" href="https://fonts.gstatic.com" />
8-
<link rel="preconnect" href="https://fonts.gstatic.com" />
9-
<link rel="preconnect" href="https://fonts.gstatic.com" />
10-
<link href="https://fonts.googleapis.com/css2?family=Nunito&display=swap" rel="stylesheet" />
118
<title>React-Template</title>
129
</head>
1310
<body>
1411
<div id="root"></div>
12+
<link href="https://fonts.googleapis.com/css2?family=Nunito&display=swap" rel="stylesheet" />
1513
</body>
1614
</html>

webpack/webpack.common.js

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,15 @@
22
const path = require('path');
33
const HtmlWebpackPlugin = require('html-webpack-plugin');
44
const CopyWebpackPlugin = require('copy-webpack-plugin');
5-
const dotenv = require('dotenv');
6-
const { DefinePlugin } = require('webpack');
5+
const DotEnv = require('dotenv-webpack');
76

8-
let envKeys = {};
9-
try {
10-
const env = dotenv.config({ path: path.join(__dirname, '..', '.env.local') }).parsed;
7+
const APP_ENV = process.env.APP_ENV || 'local';
118

12-
envKeys = Object.keys(env).reduce((prev, next) => {
13-
// eslint-disable-next-line no-param-reassign
14-
prev[`process.env.${next}`] = JSON.stringify(env[next]);
15-
return prev;
16-
}, {});
17-
console.log('Loaded Environment variable from .env.local');
18-
} catch (error) {
19-
console.log('.env.local File Not Found');
20-
}
9+
const DotEnvPlugin = new DotEnv({
10+
systemvars: true, // load all the predefined 'process.env' variables which will trump anything local per dotenv specs. (useful for CI purposes)
11+
safe: true, // load '.env.example' to verify the '.env' variables are all set.
12+
path: path.join(__dirname, '..', `.env.${APP_ENV}`),
13+
});
2114

2215
module.exports = {
2316
entry: path.resolve(__dirname, '..', './src/index.tsx'),
@@ -73,6 +66,6 @@ module.exports = {
7366
new CopyWebpackPlugin({
7467
patterns: [{ from: path.join(__dirname, 'source'), to: 'dest', noErrorOnMissing: true }],
7568
}),
76-
new DefinePlugin(envKeys),
69+
DotEnvPlugin,
7770
],
7871
};

webpack/webpack.dev.js

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,11 @@
11
/* eslint-disable no-console */
22
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
3-
const { DefinePlugin } = require('webpack');
4-
const dotenv = require('dotenv');
5-
const path = require('path');
6-
7-
let envKeys = {};
8-
try {
9-
const env = dotenv.config({ path: path.join(__dirname, '..', '.env.development') }).parsed;
10-
11-
envKeys = Object.keys(env).reduce((prev, next) => {
12-
// eslint-disable-next-line no-param-reassign
13-
prev[`process.env.${next}`] = JSON.stringify(env[next]);
14-
return prev;
15-
}, {});
16-
console.log('Loaded environment variable from .env.development');
17-
} catch (error) {
18-
console.log('.env.development file Not Found');
19-
}
203

214
module.exports = {
225
mode: 'development',
236
devServer: { hot: true },
247
devtool: 'cheap-module-source-map',
258
stats: 'minimal',
269
// incase failures for webpack react-refresh add HotModuleReplacementPlugin
27-
plugins: [new ReactRefreshWebpackPlugin(), new DefinePlugin(envKeys)],
10+
plugins: [new ReactRefreshWebpackPlugin()],
2811
};

webpack/webpack.prod.js

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,5 @@
1-
/* eslint-disable no-console */
2-
const dotenv = require('dotenv');
3-
const path = require('path');
4-
const { DefinePlugin } = require('webpack');
5-
6-
let envKeys = {};
7-
try {
8-
const env = dotenv.config({ path: path.join(__dirname, '..', '.env.production') }).parsed;
9-
envKeys = Object.keys(env).reduce((prev, next) => {
10-
// eslint-disable-next-line no-param-reassign
11-
prev[`process.env.${next}`] = JSON.stringify(env[next]);
12-
return prev;
13-
}, {});
14-
console.log('Loaded environment variable from .env.production');
15-
} catch (error) {
16-
console.log('.env.production file Not Found');
17-
}
18-
191
module.exports = {
202
mode: 'production',
213
devtool: 'source-map',
224
stats: 'normal',
23-
plugins: [new DefinePlugin(envKeys)],
245
};

0 commit comments

Comments
 (0)