diff --git a/README.md b/README.md index 2ef72e2..dc40047 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, 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 diff --git a/src/index.js b/src/index.js index 3dbab8e..5f9aa63 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 } @@ -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 }) } 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')