serverless-dotenv-plugin icon indicating copy to clipboard operation
serverless-dotenv-plugin copied to clipboard

Feature Request: Add `envFile` to configuration.

Open sgolovine opened this issue 2 years ago • 0 comments

Is your feature request related to a problem? Please describe.

Often times when setting up this plugin, I have to fight the automatic resolution of env files. On one project for example i have a file called .env which contains all local variables and an .env.dev that has all dev variables (and an .env.prod, etc). Due to the default behavior of this plugin, variables in .env overwrite those in .env.dev when trying to build the api for dev.

Describe the solution you'd like

I have been using a patch in my projects which adds an envFile configuration. When specified, the default resolution behavior is ignored and the env file that is specified is used. In a configuration it looks like this:

custom:
  dotenv:
    envFile: ".env.dev"

The patch is pretty simple and looks like this:

diff --git a/node_modules/serverless-dotenv-plugin/src/index.js b/node_modules/serverless-dotenv-plugin/src/index.js
index 2b0f24c..dd8e54c 100644
--- a/node_modules/serverless-dotenv-plugin/src/index.js
+++ b/node_modules/serverless-dotenv-plugin/src/index.js
@@ -104,7 +104,7 @@ class ServerlessPlugin {
     }
 
     // https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
-    const dotenvFiles = [
+    const dotenvFiles = !!this.config.envFile ? [this.config.envFile] : [
       `.env.${env}.local`,
       `.env.${env}`,
       // Don't include `.env.local` for `test` environment

Describe alternatives you've considered

I currently have not found an alternative and rely on the patch.

Additional context

I will do all the required dev work and open a PR for this feature. Opening this feature request to determine if the core devs of this project want this feature.

sgolovine avatar Aug 13 '23 17:08 sgolovine