Skip to content
This repository was archived by the owner on Jun 2, 2023. It is now read-only.

Commit 363fbb8

Browse files
committed
feat(dotenv): add script to load the .env file content
1 parent 31e3256 commit 363fbb8

File tree

5 files changed

+36
-13
lines changed

5 files changed

+36
-13
lines changed

.env.example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
API_PREFIX=api
2+
# if the project uses the API from another server / domain
3+
API_HOST=api.example.com (without protocol)
4+
# use it in case that the frontend is deployed together with the backend
5+
# on the server, so it will enable you to call the backend on the server
6+
PROXY_URL=https://server.example.com

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ typings/
5858

5959
# dotenv environment variables file
6060
.env
61+
.env.*
62+
!.env.example
6163

6264
# gatsby files
6365
.cache/

gatsby-config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
const postcssPresetEnv = require('postcss-preset-env');
22

3+
const dotenvParsed = require('./scripts/load-dotenv-config');
4+
console.log(`\n\n\n>> gatsby-config.js\n${JSON.stringify(dotenvParsed, null, 2)}\n\n\n`);
5+
36
const gatsbyConfig = {
47
siteMetadata: {
58
title: `Gatsby TypeScript Application Starter`,

gatsby-node.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
11
const path = require('path');
22
const webpack = require('webpack');
3-
const dotenv = require('dotenv');
43

5-
const activeEnv = process.env.ACTIVE_ENV || process.env.NODE_ENV || 'development';
6-
const dotenvPath = path.resolve(__dirname, `.env.${activeEnv}`);
7-
8-
//www.gatsbyjs.org/docs/environment-variables/
9-
const dotenvConfig = dotenv.config({
10-
path: dotenvPath,
11-
});
12-
13-
if (dotenvConfig.error) {
14-
console.log(`\n\n\ndotenv file not found: ${dotenvPath}\n\n\n`);
15-
}
4+
const dotenvParsed = require('./scripts/load-dotenv-config');
5+
console.log(`\n\n\n>> gatsby-node.js\n${JSON.stringify(dotenvParsed, null, 2)}\n\n\n`);
166

177
exports.onCreateWebpackConfig = ({ actions }) => {
188
// make sure to have the process.env configs from the .env file available on the /src/* files
199
const plugins = [];
20-
const { parsed: dotenvParsed } = dotenvConfig;
10+
// const { parsed: dotenvParsed } = dotenvConfig;
2111
if (dotenvParsed) {
2212
const webpackDefinePluginConfig = {};
2313
Object.keys(dotenvParsed).forEach(key => {

scripts/load-dotenv-config.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const path = require('path');
2+
const dotenv = require('dotenv');
3+
4+
const activeEnv = process.env.ACTIVE_ENV || process.env.NODE_ENV || 'development';
5+
const dotenvPath = path.resolve(__dirname, '..', `.env.${activeEnv}`);
6+
7+
//www.gatsbyjs.org/docs/environment-variables/
8+
const dotenvConfig = dotenv.config({
9+
path: dotenvPath,
10+
});
11+
12+
if (dotenvConfig.error) {
13+
console.log(`\n\n\ndotenv file not found: '${dotenvPath}'\n\n\n`);
14+
}
15+
16+
const { parsed: dotenvParsed } = dotenvConfig;
17+
18+
if (dotenvParsed) {
19+
console.log(`\n\ndotenv file '${dotenvPath}' loaded\n${JSON.stringify(dotenvParsed, null, 2)}\n\n`);
20+
}
21+
22+
module.exports = dotenvParsed;

0 commit comments

Comments
 (0)