Skip to content

Commit 0a731b0

Browse files
committed
Added dotenv for environment variables
1 parent 97f38ee commit 0a731b0

File tree

7 files changed

+67
-13
lines changed

7 files changed

+67
-13
lines changed

.env.default

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# ADD ANY VARIABLES YOU NEED HERE
2+
3+
HELLO=world

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.env
12
.idea
23
.vscode
34
.DS_Store

README.md

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,36 @@ new webpack.ProvidePlugin({
8585
});
8686
```
8787

88-
## 5. Responsive Image Generation
88+
## 5. Environment Configurations
8989

90-
### 5.1 What is it?
90+
If you use sensitive information in your code, like API keys or encryption tokens, you should never store those in your code repository. This can could lead to a security issue, especially if the repository is public.
91+
92+
Therefore I included the [dotenv-webpack](https://github.com/mrsteele/dotenv-webpack) plugin in this boilerplate, that enables you to store all your sensitive information in a `.env` file, that is ignored by git.
93+
94+
The `.env.default` file should contain all the variables that your application needs, but without the real data and should contain either empty variables or default values that can be used by everyone. The variables will get replaced during asset compilation so that only those variables are added, that are referenced in your code.
95+
96+
It is a common scheme to use an uppercase syntax for environment variables, as you can see in the example below. Comments inside of .env files start with a hash.
97+
98+
```
99+
# GOOGLE APIs
100+
101+
GOOGLE_MAPS_API_KEY=vEVmihkWZ2fqedyHQTGCyCc1qu4uaZoYPkOMPMyU
102+
YOUTUBE_API_KEY=TnJ8u0bYOfVuL9bbFH83T13N05I2XOX2LCJLur8L
103+
104+
# CACHING
105+
CACHE_ENABLED=false
106+
CACHE_TIMEOUT=3600
107+
```
108+
109+
You can test the usage of environment variables by copying the `.env.default` file to a new `.env` file and changing the value of `HELLO`. After re-compiling the assets you should see a message in the developer console, as soon as you visit the demo page.
110+
111+
**Important:**
112+
113+
After each change of the `.env` file you need to reload Webpack, as the environment is only loaded once per runtime. If you've got an active `npm run dev` command, you need to stop and re-run it, for the changes to take effect.
114+
115+
## 6. Responsive Image Generation
116+
117+
### 6.1 What is it?
91118

92119
This boilerplate includes a command to resize images based on a configuration file, to get rid of the hassle to care about the responsive image sizes manually. One of the benefits of this process is that it works on all major operating systems, without the need to do any manual installations.
93120

@@ -165,11 +192,11 @@ All files that are listed should get renamed, following the rules you can see in
165192

166193
---
167194

168-
### 5.2 The Configuration
195+
### 6.2 The Configuration
169196

170197
The responsive image configuration is saved in the `images.config.js` file, located in the root directory of the project.
171198

172-
#### 5.2.1 Global Settings
199+
#### 6.2.1 Global Settings
173200

174201
The configuration has some global settings, that you should set to your personal preferences.
175202

@@ -190,7 +217,7 @@ The configuration has some global settings, that you should set to your personal
190217
</tbody>
191218
</table>
192219

193-
#### 5.2.2 Collections
220+
#### 6.2.2 Collections
194221

195222
The configuration uses **collections** which include a set of configuration options to resize images. This allows you to define different resizing rules for multiple directories.
196223

@@ -233,7 +260,7 @@ Each collection has the following options.
233260
</tbody>
234261
</table>
235262

236-
#### 5.2.3 Sizes
263+
#### 6.2.3 Sizes
237264

238265
Each collection has the option "sizes" which includes a set of configurations for different image sizes that will be generated. Width and height are optional, if at least one of them is set.
239266

@@ -292,7 +319,7 @@ Each size has the following options.
292319

293320
---
294321

295-
### 5.3 The Command Line Arguments
322+
### 6.3 The Command Line Arguments
296323

297324
The resizing command supports different arguments to remove resized images, recreate all images, etc.
298325

@@ -329,7 +356,7 @@ npm run images remove
329356

330357
---
331358

332-
## 6. Placeholder Images
359+
## 7. Placeholder Images
333360

334361
All placeholder images used in the `index.html` file are downloaded from [pexels.com](https://www.pexels.com/), and [pixabay.com](https://pixabay.com/). Those are two fantastic collections of free stock photos from photographers around the globe.
335362

package-lock.json

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"autoprefixer": "^9.3.1",
2222
"babel-loader": "^8.0.4",
2323
"css-loader": "^1.0.1",
24+
"dotenv-webpack": "^1.5.7",
2425
"exports-loader": "^0.7.0",
2526
"mime-types": "^2.1.21",
2627
"mini-css-extract-plugin": "^0.4.4",

src/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
// Remove the following lines, if you do not need any of Bootstrap's JavaScript features
12
import Popper from "popper.js";
23
window.jQuery = $;
34
window.$ = $;
45

56
require("bootstrap");
7+
8+
// Remove this demo code, that's only here to show how the .env file works!
9+
if (process.env["HELLO"]) {
10+
console.log(`Hello ${process.env.HELLO}!`);
11+
}

webpack.config.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const webpack = require("webpack");
22
const path = require("path");
33
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
4+
const Dotenv = require("dotenv-webpack");
45

56
module.exports = (env, argv) => {
67
return {
@@ -39,17 +40,14 @@ module.exports = (env, argv) => {
3940
loader: "postcss-loader",
4041
options: {
4142
ident: "postcss",
42-
plugins: [
43-
require("autoprefixer")({ browsers: "last 3 versions" })
44-
]
43+
plugins: [require("autoprefixer")({ browsers: "last 3 versions" })]
4544
}
4645
},
4746
{
4847
loader: "sass-loader",
4948
options: {
5049
sourceMap: true,
51-
outputStyle:
52-
argv.mode === "production" ? "compressed" : "expanded"
50+
outputStyle: argv.mode === "production" ? "compressed" : "expanded"
5351
}
5452
}
5553
]
@@ -70,6 +68,9 @@ module.exports = (env, argv) => {
7068

7169
// Define used plugins
7270
plugins: [
71+
new Dotenv({
72+
path: "./.env"
73+
}),
7374
new MiniCssExtractPlugin({
7475
filename: "[name].css",
7576
chunkFilename: "[id].css"

0 commit comments

Comments
 (0)