From ce7122746bffe2f2f488c27a0e63a776f434d21d Mon Sep 17 00:00:00 2001 From: Tudor Date: Fri, 20 Aug 2021 23:09:32 +0100 Subject: [PATCH] add option to disable env expanding in plugin-dotenv --- .changeset/breezy-comics-glow.md | 5 + plugins/plugin-dotenv/README.md | 7 +- plugins/plugin-dotenv/plugin.js | 11 +- .../test/__snapshots__/plugin.test.js.snap | 124 ++++++++++++++++++ plugins/plugin-dotenv/test/env/.env | 2 + plugins/plugin-dotenv/test/plugin.test.js | 16 +++ 6 files changed, 157 insertions(+), 8 deletions(-) create mode 100644 .changeset/breezy-comics-glow.md diff --git a/.changeset/breezy-comics-glow.md b/.changeset/breezy-comics-glow.md new file mode 100644 index 0000000000..5dd149fe7a --- /dev/null +++ b/.changeset/breezy-comics-glow.md @@ -0,0 +1,5 @@ +--- +'@snowpack/plugin-dotenv': minor +--- + +Added an option to disable dotenv-expand functionality diff --git a/plugins/plugin-dotenv/README.md b/plugins/plugin-dotenv/README.md index 1d7d15a64f..b3fbf3a9f5 100644 --- a/plugins/plugin-dotenv/README.md +++ b/plugins/plugin-dotenv/README.md @@ -30,6 +30,7 @@ SNOWPACK_PUBLIC_ENABLE_FEATURE=true #### Plugin Options -| Name | Type | Description | -| :---- | :------- | :------------------------------------------------------------------------------------------------- | -| `dir` | `string` | (optional) Where to find `.env` files. Default is your current working directory (`process.cwd()`) | +| Name | Type | Description | +| :------- | :-------- | :------------------------------------------------------------------------------------------------- | +| `dir` | `string` | (optional) Where to find `.env` files. Default is your current working directory (`process.cwd()`) | +| `expand` | `boolean` | (optional) Enable `dotenv-expand` support. Default is `true` | diff --git a/plugins/plugin-dotenv/plugin.js b/plugins/plugin-dotenv/plugin.js index 7b7fe74323..e32907c02e 100644 --- a/plugins/plugin-dotenv/plugin.js +++ b/plugins/plugin-dotenv/plugin.js @@ -16,6 +16,7 @@ module.exports = function plugin(snowpackConfig, options) { ].filter(Boolean); const dir = options && options.dir ? options.dir.toString() : '.'; + const expand = options && options.expand !== undefined ? options.expand : true; // Load environment variables from .env* files. Suppress warnings using silent // if this file is missing. dotenv will never modify any environment variables // that have already been set. Variable expansion is supported in .env files. @@ -25,11 +26,11 @@ module.exports = function plugin(snowpackConfig, options) { dotenvFile = path.resolve(process.cwd(), dir, dotenvFile); if (fs.existsSync(dotenvFile)) { - require('dotenv-expand')( - require('dotenv').config({ - path: dotenvFile, - }), - ); + const dotenv = require('dotenv').config({ + path: dotenvFile + }); + + if (expand) require('dotenv-expand')(dotenv); } }); diff --git a/plugins/plugin-dotenv/test/__snapshots__/plugin.test.js.snap b/plugins/plugin-dotenv/test/__snapshots__/plugin.test.js.snap index 398521e24d..267fec1405 100644 --- a/plugins/plugin-dotenv/test/__snapshots__/plugin.test.js.snap +++ b/plugins/plugin-dotenv/test/__snapshots__/plugin.test.js.snap @@ -5,6 +5,7 @@ Object { "__DOTENV_DEVELOPMENT": "DEVELOPMENT", "__DOTENV_DEVELOPMENT_LOCAL": "DEVELOPMENT_LOCAL", "__DOTENV_ENV": "ENV", + "__DOTENV_EXPAND": "PRESET", "__DOTENV_LOCAL": "LOCAL", "__DOTENV_PRESET": "PRESET", "__DOTENV_PRODUCTION": "ENV", @@ -19,6 +20,7 @@ Object { "__DOTENV_DEVELOPMENT": "ENV", "__DOTENV_DEVELOPMENT_LOCAL": "LOCAL", "__DOTENV_ENV": "ENV", + "__DOTENV_EXPAND": "PRESET", "__DOTENV_LOCAL": "LOCAL", "__DOTENV_PRESET": "PRESET", "__DOTENV_PRODUCTION": "PRODUCTION", @@ -33,6 +35,7 @@ Object { "__DOTENV_DEVELOPMENT": "ENV", "__DOTENV_DEVELOPMENT_LOCAL": "ENV", "__DOTENV_ENV": "ENV", + "__DOTENV_EXPAND": "PRESET", "__DOTENV_LOCAL": "TEST", "__DOTENV_PRESET": "PRESET", "__DOTENV_PRODUCTION": "ENV", @@ -47,6 +50,7 @@ Object { "__DOTENV_DEVELOPMENT": "ENV", "__DOTENV_DEVELOPMENT_LOCAL": "LOCAL", "__DOTENV_ENV": "ENV", + "__DOTENV_EXPAND": "PRESET", "__DOTENV_LOCAL": "LOCAL", "__DOTENV_PRESET": "PRESET", "__DOTENV_PRODUCTION": "ENV", @@ -111,3 +115,123 @@ Object { "__DOTENV_TEST_LOCAL": "LOCAL", } `; + +exports[`with expand explicitly off development 1`] = ` +Object { + "__DOTENV_DEVELOPMENT": "DEVELOPMENT", + "__DOTENV_DEVELOPMENT_LOCAL": "DEVELOPMENT_LOCAL", + "__DOTENV_ENV": "ENV", + "__DOTENV_EXPAND": "\${__DOTENV_PRESET}", + "__DOTENV_LOCAL": "LOCAL", + "__DOTENV_PRESET": "PRESET", + "__DOTENV_PRODUCTION": "ENV", + "__DOTENV_PRODUCTION_LOCAL": "LOCAL", + "__DOTENV_TEST": "ENV", + "__DOTENV_TEST_LOCAL": "LOCAL", +} +`; + +exports[`with expand explicitly off production 1`] = ` +Object { + "__DOTENV_DEVELOPMENT": "ENV", + "__DOTENV_DEVELOPMENT_LOCAL": "LOCAL", + "__DOTENV_ENV": "ENV", + "__DOTENV_EXPAND": "\${__DOTENV_PRESET}", + "__DOTENV_LOCAL": "LOCAL", + "__DOTENV_PRESET": "PRESET", + "__DOTENV_PRODUCTION": "PRODUCTION", + "__DOTENV_PRODUCTION_LOCAL": "PRODUCTION_LOCAL", + "__DOTENV_TEST": "ENV", + "__DOTENV_TEST_LOCAL": "LOCAL", +} +`; + +exports[`with expand explicitly off test 1`] = ` +Object { + "__DOTENV_DEVELOPMENT": "ENV", + "__DOTENV_DEVELOPMENT_LOCAL": "ENV", + "__DOTENV_ENV": "ENV", + "__DOTENV_EXPAND": "\${__DOTENV_PRESET}", + "__DOTENV_LOCAL": "TEST", + "__DOTENV_PRESET": "PRESET", + "__DOTENV_PRODUCTION": "ENV", + "__DOTENV_PRODUCTION_LOCAL": "ENV", + "__DOTENV_TEST": "TEST", + "__DOTENV_TEST_LOCAL": "TEST_LOCAL", +} +`; + +exports[`with expand explicitly off undefined 1`] = ` +Object { + "__DOTENV_DEVELOPMENT": "ENV", + "__DOTENV_DEVELOPMENT_LOCAL": "LOCAL", + "__DOTENV_ENV": "ENV", + "__DOTENV_EXPAND": "\${__DOTENV_PRESET}", + "__DOTENV_LOCAL": "LOCAL", + "__DOTENV_PRESET": "PRESET", + "__DOTENV_PRODUCTION": "ENV", + "__DOTENV_PRODUCTION_LOCAL": "LOCAL", + "__DOTENV_TEST": "ENV", + "__DOTENV_TEST_LOCAL": "LOCAL", +} +`; + +exports[`with expand explicitly on development 1`] = ` +Object { + "__DOTENV_DEVELOPMENT": "DEVELOPMENT", + "__DOTENV_DEVELOPMENT_LOCAL": "DEVELOPMENT_LOCAL", + "__DOTENV_ENV": "ENV", + "__DOTENV_EXPAND": "PRESET", + "__DOTENV_LOCAL": "LOCAL", + "__DOTENV_PRESET": "PRESET", + "__DOTENV_PRODUCTION": "ENV", + "__DOTENV_PRODUCTION_LOCAL": "LOCAL", + "__DOTENV_TEST": "ENV", + "__DOTENV_TEST_LOCAL": "LOCAL", +} +`; + +exports[`with expand explicitly on production 1`] = ` +Object { + "__DOTENV_DEVELOPMENT": "ENV", + "__DOTENV_DEVELOPMENT_LOCAL": "LOCAL", + "__DOTENV_ENV": "ENV", + "__DOTENV_EXPAND": "PRESET", + "__DOTENV_LOCAL": "LOCAL", + "__DOTENV_PRESET": "PRESET", + "__DOTENV_PRODUCTION": "PRODUCTION", + "__DOTENV_PRODUCTION_LOCAL": "PRODUCTION_LOCAL", + "__DOTENV_TEST": "ENV", + "__DOTENV_TEST_LOCAL": "LOCAL", +} +`; + +exports[`with expand explicitly on test 1`] = ` +Object { + "__DOTENV_DEVELOPMENT": "ENV", + "__DOTENV_DEVELOPMENT_LOCAL": "ENV", + "__DOTENV_ENV": "ENV", + "__DOTENV_EXPAND": "PRESET", + "__DOTENV_LOCAL": "TEST", + "__DOTENV_PRESET": "PRESET", + "__DOTENV_PRODUCTION": "ENV", + "__DOTENV_PRODUCTION_LOCAL": "ENV", + "__DOTENV_TEST": "TEST", + "__DOTENV_TEST_LOCAL": "TEST_LOCAL", +} +`; + +exports[`with expand explicitly on undefined 1`] = ` +Object { + "__DOTENV_DEVELOPMENT": "ENV", + "__DOTENV_DEVELOPMENT_LOCAL": "LOCAL", + "__DOTENV_ENV": "ENV", + "__DOTENV_EXPAND": "PRESET", + "__DOTENV_LOCAL": "LOCAL", + "__DOTENV_PRESET": "PRESET", + "__DOTENV_PRODUCTION": "ENV", + "__DOTENV_PRODUCTION_LOCAL": "LOCAL", + "__DOTENV_TEST": "ENV", + "__DOTENV_TEST_LOCAL": "LOCAL", +} +`; diff --git a/plugins/plugin-dotenv/test/env/.env b/plugins/plugin-dotenv/test/env/.env index 7d4d7fe6c2..ba2c37fd86 100644 --- a/plugins/plugin-dotenv/test/env/.env +++ b/plugins/plugin-dotenv/test/env/.env @@ -17,3 +17,5 @@ __DOTENV_PRODUCTION=ENV # new environment variable __DOTENV_ENV=ENV +# expanded variable +__DOTENV_EXPAND=${__DOTENV_PRESET} diff --git a/plugins/plugin-dotenv/test/plugin.test.js b/plugins/plugin-dotenv/test/plugin.test.js index b19bc5b050..d27d000c1c 100644 --- a/plugins/plugin-dotenv/test/plugin.test.js +++ b/plugins/plugin-dotenv/test/plugin.test.js @@ -33,3 +33,19 @@ describe('with dir NODE_ENV=', () => { }); }); }); + +describe('with expand explicitly off', () => { + NODE_ENV_LIST.forEach((nodeEnv) => { + test(`${nodeEnv}`, () => { + expect(execPlugin(nodeEnv, {expand: false})).toMatchSnapshot(); + }); + }); +}); + +describe('with expand explicitly on', () => { + NODE_ENV_LIST.forEach((nodeEnv) => { + test(`${nodeEnv}`, () => { + expect(execPlugin(nodeEnv, {expand: true})).toMatchSnapshot(); + }); + }); +});