Skip to content

Commit

Permalink
add option to disable env expanding in plugin-dotenv (#3675)
Browse files Browse the repository at this point in the history
  • Loading branch information
tudddorrr authored Aug 24, 2021
1 parent e2018f4 commit cf1bd44
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/breezy-comics-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@snowpack/plugin-dotenv': minor
---

Added an option to disable dotenv-expand functionality
7 changes: 4 additions & 3 deletions plugins/plugin-dotenv/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
11 changes: 6 additions & 5 deletions plugins/plugin-dotenv/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}
});

Expand Down
124 changes: 124 additions & 0 deletions plugins/plugin-dotenv/test/__snapshots__/plugin.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
}
`;
2 changes: 2 additions & 0 deletions plugins/plugin-dotenv/test/env/.env
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ __DOTENV_PRODUCTION=ENV
# new environment variable
__DOTENV_ENV=ENV

# expanded variable
__DOTENV_EXPAND=${__DOTENV_PRESET}
16 changes: 16 additions & 0 deletions plugins/plugin-dotenv/test/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});

1 comment on commit cf1bd44

@vercel
Copy link

@vercel vercel bot commented on cf1bd44 Aug 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.