From 66c10f76608de1fa798af6a1041e160e84e319d3 Mon Sep 17 00:00:00 2001 From: dannegm Date: Wed, 6 Jul 2022 13:35:38 -0500 Subject: [PATCH 1/5] fix: Adding support for custom path in Safe mode --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 3dbab8e..5af8457 100644 --- a/src/index.js +++ b/src/index.js @@ -95,7 +95,7 @@ class Dotenv { let blueprint = env if (safe) { - let file = './.env.example' + let file = `${path}.example` if (safe !== true) { file = safe } From ae2c4b9ed535740af1ecdbc5d41511b87347491c Mon Sep 17 00:00:00 2001 From: dannegm Date: Wed, 6 Jul 2022 13:36:00 -0500 Subject: [PATCH 2/5] fix: Adding support for custom path with Defaults --- src/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 5af8457..5f9aa63 100644 --- a/src/index.js +++ b/src/index.js @@ -112,11 +112,11 @@ class Dotenv { } getDefaults () { - const { silent, defaults } = this.config + const { path, silent, defaults } = this.config if (defaults) { return this.loadFile({ - file: defaults === true ? './.env.defaults' : defaults, + file: defaults === true ? `${path}.defaults` : defaults, silent }) } From 823a97c441934028492e0774c8ee216e6ba3fac9 Mon Sep 17 00:00:00 2001 From: dannegm Date: Wed, 6 Jul 2022 13:36:47 -0500 Subject: [PATCH 3/5] test: Correcting test that gave false positive --- test/index.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/index.test.js b/test/index.test.js index bc9e488..cdc4e07 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -197,7 +197,7 @@ describe.each(versions)('%s', (_, DotenvPlugin) => { }) test('Should fail when not passing safe-mode', (done) => { - const config = getConfig('web', new DotenvPlugin({ path: envEmpty, safe: true })) + const config = getConfig('web', new DotenvPlugin({ path: envMissingOne, safe: true })) webpack(config, (err) => { expect(err.message).toBe('Missing environment variable: TEST') From f9f27185f10158b3132a189469087c9bd10083b2 Mon Sep 17 00:00:00 2001 From: dannegm Date: Fri, 8 Jul 2022 03:33:38 -0500 Subject: [PATCH 4/5] docs: Complementing documentation on the `path` configuration --- README.md | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2ef72e2..8d44b25 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ webpack dotenv-webpack - + A secure webpack plugin that supports dotenv and other environment variables and **only exposes what you choose and use**.
@@ -109,7 +109,7 @@ If you are running into issues where you or another package you use interfaces w Use the following properties to configure your instance. -* **path** (`'./.env'`) - The path to your environment variables. +* **path** (`'./.env'`) - The path to your environment variables. This same path applies to the `.env.example` and `.env.defaults` files. [Read more here](#about-path-settings). * **safe** (`false`) - If true, load '.env.example' to verify the '.env' variables are all set. Can also be a string to a different file. * **allowEmptyValues** (`false`) - Whether to allow empty strings in safe mode. If false, will throw an error if any env variables are empty (but only if safe mode is enabled). * **systemvars** (`false`) - Set to true if you would rather load all system variables as well (useful for CI purposes). @@ -138,6 +138,51 @@ module.exports = { ... }; ``` +## About `path` settings + +As previously mentioned, it is possible to customize the `path` where the `.env` file is located as well as its *filename* from the plugin settings: + +```javascript +module.exports = { + ... + plugins: [ + new Dotenv({ + path: './some.other.env', + }) + ] + ... +}; +``` + +It is important to mention that this same path and filename will be used for the location of the `.env.example` and `.env.defaults` files if they are configured: + +```javascript +module.exports = { + ... + plugins: [ + new Dotenv({ + path: '../../path/to/other.env', + safe: true, // load '../../path/to/other.env.example' + defaults: true, // load '../../path/to/other.env.defaults' + }) + ] + ... +}; +``` + +This is especially useful when working with [Monorepos](https://monorepo.tools/) where the same configuration can be shared within all sub-packages of the repository: + +```bash +. +├── packages/ +│ ├── app/ +│ │ └── webpack.config.js # { path: '../../.env' } +│ └── libs/ +│ └── webpack.config.js # { path: '../../.env' } +├── .env +├── .env.example +└── .env.defaults +``` ## LICENSE From ee9839fa18534939b2eff96b3fe13a65828542ff Mon Sep 17 00:00:00 2001 From: dannegm Date: Fri, 8 Jul 2022 11:55:50 -0500 Subject: [PATCH 5/5] docs: Adding quick note about the path behavior --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8d44b25..dc40047 100644 --- a/README.md +++ b/README.md @@ -154,7 +154,7 @@ module.exports = { }; ``` -It is important to mention that this same path and filename will be used for the location of the `.env.example` and `.env.defaults` files if they are configured: +It is important to mention that this same path and filename will be used for the location of the `.env.example` and `.env.defaults` files if they are configured, this will only add the `.example` and `.defaults` suffixes respectively: ```javascript module.exports = {