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

Commit 0750354

Browse files
authored
Merge pull request #9 from Tushar1998/app-specific-env
App specific env
2 parents b791433 + 291ed81 commit 0750354

File tree

11 files changed

+540
-426
lines changed

11 files changed

+540
-426
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
- Code Spiltting : 🖖 Webpack Split Chunks (basic)
3536
- Tree Shaking : 🌲 Webpack Terser Plugin (basic)
3637
- Clone Branch react-router if you want to use [react-router-dom](https://reactrouter.com/)
37-

package.json

Lines changed: 22 additions & 21 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": "webpack --config webpack/webpack.config.js --env env=prod",
1111
"analyze": "webpack --config webpack/webpack.config.js --env env=prod --analyze",
1212
"lint": "eslint --fix \"./src/**/*.{js,jsx,ts,tsx,scss,css,json}\"",
@@ -30,17 +30,17 @@
3030
"styled-components"
3131
],
3232
"dependencies": {
33-
"@babel/core": "^7.14.0",
34-
"@babel/plugin-transform-runtime": "^7.13.15",
35-
"@babel/preset-env": "^7.14.1",
33+
"@babel/core": "^7.14.2",
34+
"@babel/plugin-transform-runtime": "^7.14.2",
35+
"@babel/preset-env": "^7.14.2",
3636
"@babel/preset-react": "^7.13.13",
3737
"@babel/preset-typescript": "^7.13.0",
3838
"@babel/runtime": "^7.14.0",
3939
"@svgr/webpack": "^5.5.0",
4040
"babel-loader": "^8.2.2",
4141
"copy-webpack-plugin": "^8.1.1",
4242
"css-loader": "^5.2.4",
43-
"dotenv": "^9.0.2",
43+
"dotenv-webpack": "^7.0.2",
4444
"html-webpack-plugin": "^5.3.1",
4545
"react": "^17.0.2",
4646
"react-dom": "^17.0.2",
@@ -58,28 +58,29 @@
5858
"@storybook/addon-links": "^6.2.9",
5959
"@storybook/builder-webpack5": "^6.2.9",
6060
"@storybook/react": "^6.2.9",
61-
"@testing-library/jest-dom": "5.12.0",
62-
"@testing-library/react": "11.2.6",
63-
"@types/jest": "26.0.23",
64-
"@types/react": "17.0.5",
65-
"@types/react-dom": "17.0.4",
66-
"@types/styled-components": "5.1.9",
61+
"@testing-library/jest-dom": "^5.12.0",
62+
"@testing-library/react": "^11.2.7",
63+
"@types/jest": "^26.0.23",
64+
"@types/react": "^17.0.5",
65+
"@types/react-dom": "^17.0.5",
66+
"@types/styled-components": "^5.1.9",
6767
"@typescript-eslint/eslint-plugin": "^4.23.0",
6868
"@typescript-eslint/parser": "^4.23.0",
69-
"eslint": "7.26.0",
69+
"cross-env": "^7.0.3",
70+
"eslint": "^7.26.0",
7071
"eslint-config-airbnb": "18.2.1",
71-
"eslint-config-prettier": "8.3.0",
72-
"eslint-plugin-import": "2.22.1",
73-
"eslint-plugin-jest": "24.3.6",
74-
"eslint-plugin-jsx-a11y": "6.4.1",
75-
"eslint-plugin-prettier": "3.4.0",
76-
"eslint-plugin-react": "7.23.2",
72+
"eslint-config-prettier": "^8.3.0",
73+
"eslint-plugin-import": "^2.23.1",
74+
"eslint-plugin-jest": "^24.3.6",
75+
"eslint-plugin-jsx-a11y": "^6.4.1",
76+
"eslint-plugin-prettier": "^3.4.0",
77+
"eslint-plugin-react": "^7.23.2",
7778
"eslint-plugin-react-hooks": "^4.2.0",
7879
"husky": "6.0.0",
7980
"jest": "^26.6.3",
80-
"lint-staged": "11.0.0",
81-
"netlify-cli": "3.29.4",
82-
"prettier": "2.3.0",
81+
"lint-staged": "^11.0.0",
82+
"netlify-cli": "^3.29.15",
83+
"prettier": "^2.3.0",
8384
"react-refresh": "^0.10.0",
8485
"ts-jest": "26.5.6",
8586
"ts-node": "9.1.1",

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 DotEnvWebPackPlugin = 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'),
@@ -80,6 +73,6 @@ module.exports = {
8073
new CopyWebpackPlugin({
8174
patterns: [{ from: path.join(__dirname, 'source'), to: 'dest', noErrorOnMissing: true }],
8275
}),
83-
new DefinePlugin(envKeys),
76+
DotEnvWebPackPlugin,
8477
],
8578
};

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 & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,5 @@
1-
/* eslint-disable no-console */
2-
const dotenv = require('dotenv');
3-
const path = require('path');
4-
const { DefinePlugin } = require('webpack');
51
const TerserPlugin = require('terser-webpack-plugin');
62

7-
let envKeys = {};
8-
try {
9-
const env = dotenv.config({ path: path.join(__dirname, '..', '.env.production') }).parsed;
10-
envKeys = Object.keys(env).reduce((prev, next) => {
11-
// eslint-disable-next-line no-param-reassign
12-
prev[`process.env.${next}`] = JSON.stringify(env[next]);
13-
return prev;
14-
}, {});
15-
console.log('Loaded environment variable from .env.production');
16-
} catch (error) {
17-
console.log('.env.production file Not Found');
18-
}
19-
203
module.exports = {
214
mode: 'production',
225
devtool: 'source-map',
@@ -31,5 +14,4 @@ module.exports = {
3114
}),
3215
],
3316
},
34-
plugins: [new DefinePlugin(envKeys)],
3517
};

0 commit comments

Comments
 (0)