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

Commit aefc1c9

Browse files
committed
⚙️ Added Environment Variables Support
1 parent 9a41b15 commit aefc1c9

File tree

8 files changed

+52
-32
lines changed

8 files changed

+52
-32
lines changed

.env.development

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

.env.production

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

.gitignore

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

1414
# Environment files
15-
.env
15+
.env.local
1616

1717
#others
1818
yarn-error.log

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ This Template is for building React efficient React Frontend Apps. It uses moder
2525
- Bundler : 📦 Webpack Version 5
2626
- Styling : 💅 styled-components (supports traditional css/scss)
2727
- Component Preview : 🔖 Storybook with Webpack 5 bundler
28-
- Environment Variable - ⚙️ Support with .env file
2928
- Development : eslint, prettier, editorconfig
3029
- Testing : 🧪 React Testing Library with Jest
31-
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
3234
# Future Implementations
3335
- Code Spiltting
3436
- Tree Shaking

package.json

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,6 @@
5353
"webpack-cli": "^4.7.0",
5454
"webpack-merge": "^5.7.3"
5555
},
56-
"lint-staged": {
57-
"src/**/*.{js,jsx,ts,tsx,json}": [
58-
"eslint --fix"
59-
],
60-
"src/**/*.{js,jsx,ts,tsx,json,css,scss,md}": [
61-
"prettier --write"
62-
]
63-
},
64-
"husky": {
65-
"hooks": {
66-
"pre-commit": "lint-staged"
67-
}
68-
},
6956
"devDependencies": {
7057
"@pmmmwh/react-refresh-webpack-plugin": "^0.4.3",
7158
"@storybook/addon-actions": "^6.2.9",

webpack/webpack.common.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,21 @@
22
const path = require('path');
33
const HtmlWebpackPlugin = require('html-webpack-plugin');
44
const CopyWebpackPlugin = require('copy-webpack-plugin');
5-
const { DefinePlugin } = require('webpack');
65
const dotenv = require('dotenv');
7-
8-
//! Note : Alias and Rules are the same for Storybook Webpack configuration
6+
const { DefinePlugin } = require('webpack');
97

108
let envKeys = {};
119
try {
12-
const env = dotenv.config().parsed;
10+
const env = dotenv.config({ path: path.join(__dirname, '..', '.env.local') }).parsed;
1311

1412
envKeys = Object.keys(env).reduce((prev, next) => {
1513
// eslint-disable-next-line no-param-reassign
1614
prev[`process.env.${next}`] = JSON.stringify(env[next]);
1715
return prev;
1816
}, {});
19-
console.log('Loaded Environment variable from .env');
17+
console.log('Loaded Environment variable from .env.local');
2018
} catch (error) {
21-
console.log('.env File Not Found');
19+
console.log('.env.local File Not Found');
2220
}
2321

2422
module.exports = {
@@ -34,10 +32,6 @@ module.exports = {
3432
utils: path.resolve(__dirname, '..', 'src', 'utils'),
3533
},
3634
},
37-
devServer: {
38-
port: 3000,
39-
},
40-
stats: 'minimal',
4135
module: {
4236
rules: [
4337
{

webpack/webpack.dev.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
1+
/* eslint-disable no-console */
12
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+
}
220

321
module.exports = {
422
mode: 'development',
5-
devServer: {
6-
hot: true,
7-
open: true,
8-
},
23+
devServer: { hot: true },
924
devtool: 'cheap-module-source-map',
25+
stats: 'minimal',
1026
// incase failures for webpack react-refresh add HotModuleReplacementPlugin
11-
plugins: [new ReactRefreshWebpackPlugin()],
27+
plugins: [new ReactRefreshWebpackPlugin(), new DefinePlugin(envKeys)],
1228
};

webpack/webpack.prod.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
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+
119
module.exports = {
220
mode: 'production',
321
devtool: 'source-map',
4-
plugins: [],
22+
stats: 'normal',
23+
plugins: [new DefinePlugin(envKeys)],
524
};

0 commit comments

Comments
 (0)