Skip to content

Commit

Permalink
fix: Extending custom path support (#487)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
dannegm committed Jul 8, 2022
1 parent 1adf0f3 commit f5d79ee
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
49 changes: 47 additions & 2 deletions README.md
Expand Up @@ -3,7 +3,7 @@
<img width="30" height="30" src="https://webpack.js.org/assets/icon-square-big.svg" alt="webpack">
dotenv-webpack
</h1>

A secure webpack plugin that supports dotenv and other environment variables and **only exposes what you choose and use**.

<div align="center">
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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, this will only add the `.example` and `.defaults` suffixes respectively:

```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

Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Expand Up @@ -95,7 +95,7 @@ class Dotenv {

let blueprint = env
if (safe) {
let file = './.env.example'
let file = `${path}.example`
if (safe !== true) {
file = safe
}
Expand All @@ -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
})
}
Expand Down
2 changes: 1 addition & 1 deletion test/index.test.js
Expand Up @@ -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')
Expand Down

0 comments on commit f5d79ee

Please sign in to comment.