mrsteele / dotenv-webpack

A secure webpack plugin that supports dotenv and other environment variables and only exposes what you choose and use.
MIT License
1.3k stars 76 forks source link

Multi-path precedence support #508

Open abxhub opened 2 months ago

abxhub commented 2 months ago

Description

I'd like to have multiple .env files with support to load them in order of precedence. For example, assume a project defines

If local exists, that file should be used. If not, it should load .env instead. This way, any CI/CD pipelines can use the .env file (included in source control) with templated values at build time, where as locally, I can simply redefine the file with the .local extension (excluded from source control) with the desired values.

Example

In dotenv, you can pass an array to path for such functionality (see: manage multiple environments) like so:

    require('dotenv').config({ path: ['.env.local', '.env'] })

It would be great if dotenv-webpack could support this syntax for path similarly, like so:

    plugins: [
      new Dotenv({
        path: [path.resolve(__dirname, `../.env.local`), path.resolve(__dirname, `../.env`)],
      }),
    ],

Alternatives

I've looked into the suggestion on #434, but I'm not sure this is suitable to my situation as I can't rely on the NODE_ENV variable to determine whether we're doing a local build or serve versus a pipeline build as I have multiple environments being marked as development which are technically doing production builds but with different config, and so would like to avoid pipeline changes where possible.

Would the defaults feature of dotenv-webpack be suitable for this? For example, define .env.defaults as the tokenised values, and .env as containing the locally desired values (and therefore exclude .env from source control and include .env.defaults in source control?). Even if so, support for this dotenv feature would be great if possible.

mrsteele commented 1 month ago

The defaults absolutely be suitable. In fact, I would suggest you checkout the other npm module dotenv-defaults.