Skip to content

Commit f5d79ee

Browse files
authored
fix: Extending custom path support (#487)
* fix: Adding support for custom path in Safe mode * fix: Adding support for custom path with Defaults * test: Correcting test that gave false positive * docs: Complementing documentation on the `path` configuration * docs: Adding quick note about the path behavior BREAKING CHANGE: The `path`, `defaults` and `safe` props all look at the `path` by default. Please be advised.
1 parent 1adf0f3 commit f5d79ee

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

README.md

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<img width="30" height="30" src="https://webpack.js.org/assets/icon-square-big.svg" alt="webpack">
44
dotenv-webpack
55
</h1>
6-
6+
77
A secure webpack plugin that supports dotenv and other environment variables and **only exposes what you choose and use**.
88

99
<div align="center">
@@ -109,7 +109,7 @@ If you are running into issues where you or another package you use interfaces w
109109

110110
Use the following properties to configure your instance.
111111

112-
* **path** (`'./.env'`) - The path to your environment variables.
112+
* **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).
113113
* **safe** (`false`) - If true, load '.env.example' to verify the '.env' variables are all set. Can also be a string to a different file.
114114
* **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).
115115
* **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 = {
138138
...
139139
};
140140
```
141+
## About `path` settings
142+
143+
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:
144+
145+
```javascript
146+
module.exports = {
147+
...
148+
plugins: [
149+
new Dotenv({
150+
path: './some.other.env',
151+
})
152+
]
153+
...
154+
};
155+
```
156+
157+
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:
158+
159+
```javascript
160+
module.exports = {
161+
...
162+
plugins: [
163+
new Dotenv({
164+
path: '../../path/to/other.env',
165+
safe: true, // load '../../path/to/other.env.example'
166+
defaults: true, // load '../../path/to/other.env.defaults'
167+
})
168+
]
169+
...
170+
};
171+
```
172+
173+
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:
174+
175+
```bash
176+
.
177+
├── packages/
178+
│ ├── app/
179+
│ │ └── webpack.config.js # { path: '../../.env' }
180+
│ └── libs/
181+
│ └── webpack.config.js # { path: '../../.env' }
182+
├── .env
183+
├── .env.example
184+
└── .env.defaults
185+
```
141186

142187
## LICENSE
143188

src/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class Dotenv {
9595

9696
let blueprint = env
9797
if (safe) {
98-
let file = './.env.example'
98+
let file = `${path}.example`
9999
if (safe !== true) {
100100
file = safe
101101
}
@@ -112,11 +112,11 @@ class Dotenv {
112112
}
113113

114114
getDefaults () {
115-
const { silent, defaults } = this.config
115+
const { path, silent, defaults } = this.config
116116

117117
if (defaults) {
118118
return this.loadFile({
119-
file: defaults === true ? './.env.defaults' : defaults,
119+
file: defaults === true ? `${path}.defaults` : defaults,
120120
silent
121121
})
122122
}

test/index.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ describe.each(versions)('%s', (_, DotenvPlugin) => {
197197
})
198198

199199
test('Should fail when not passing safe-mode', (done) => {
200-
const config = getConfig('web', new DotenvPlugin({ path: envEmpty, safe: true }))
200+
const config = getConfig('web', new DotenvPlugin({ path: envMissingOne, safe: true }))
201201

202202
webpack(config, (err) => {
203203
expect(err.message).toBe('Missing environment variable: TEST')

0 commit comments

Comments
 (0)