From a78e99ec5b6f242f1aa9b52e8d36fd875aa55bc8 Mon Sep 17 00:00:00 2001 From: Aakash Sigdel Date: Sat, 29 Jun 2019 23:33:33 +0200 Subject: [PATCH 1/3] override variables with env var if present --- babel-plugin-dotenv/index.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/babel-plugin-dotenv/index.js b/babel-plugin-dotenv/index.js index cff3a39..39cc3c8 100644 --- a/babel-plugin-dotenv/index.js +++ b/babel-plugin-dotenv/index.js @@ -30,13 +30,19 @@ module.exports = function (data) { } var importedId = specifier.imported.name var localId = specifier.local.name; - if(!(config.hasOwnProperty(importedId))) { - throw path.get('specifiers')[idx].buildCodeFrameError('Try to import dotenv variable "' + importedId + '" which is not defined in any ' + configFile + ' files.') + if(!(config.hasOwnProperty(importedId)) && !process.env.hasOwnProperty(importedId)) { + throw path.get('specifiers')[idx].buildCodeFrameError( + 'Try to import dotenv variable "' + + importedId + + '" which is not defined in any ' + + configFile + + ' files or as environment variable.' + ) } var binding = path.scope.getBinding(localId); binding.referencePaths.forEach(function(refPath){ - refPath.replaceWith(t.valueToNode(config[importedId])) + refPath.replaceWith(t.valueToNode(process.env[importedId] || config[importedId])) }); }) From 97912e98935ceb63d3ec3ed377f30a4ac6e6951b Mon Sep 17 00:00:00 2001 From: Aakash Sigdel Date: Sun, 30 Jun 2019 09:00:00 +0200 Subject: [PATCH 2/3] add test for override --- babel-plugin-dotenv/index.js | 8 +++++--- .../test/fixtures/override-value/.babelrc | 10 ++++++++++ .../test/fixtures/override-value/.env | 2 ++ .../test/fixtures/override-value/source.js | 2 ++ babel-plugin-dotenv/test/test.js | 17 +++++++++++++++-- 5 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 babel-plugin-dotenv/test/fixtures/override-value/.babelrc create mode 100644 babel-plugin-dotenv/test/fixtures/override-value/.env create mode 100644 babel-plugin-dotenv/test/fixtures/override-value/source.js diff --git a/babel-plugin-dotenv/index.js b/babel-plugin-dotenv/index.js index 39cc3c8..38dbd97 100644 --- a/babel-plugin-dotenv/index.js +++ b/babel-plugin-dotenv/index.js @@ -3,6 +3,8 @@ var fs = require('fs'); var sysPath = require('path'); var process = require('process'); +var environmentCopy = Object.assign({}, process.env) + module.exports = function (data) { var t = data.types; @@ -30,19 +32,19 @@ module.exports = function (data) { } var importedId = specifier.imported.name var localId = specifier.local.name; - if(!(config.hasOwnProperty(importedId)) && !process.env.hasOwnProperty(importedId)) { + if(!(config.hasOwnProperty(importedId)) && !environmentCopy.hasOwnProperty(importedId)) { throw path.get('specifiers')[idx].buildCodeFrameError( 'Try to import dotenv variable "' + importedId + '" which is not defined in any ' + configFile - + ' files or as environment variable.' + + ' files or as an environment variable.' ) } var binding = path.scope.getBinding(localId); binding.referencePaths.forEach(function(refPath){ - refPath.replaceWith(t.valueToNode(process.env[importedId] || config[importedId])) + refPath.replaceWith(t.valueToNode(environmentCopy[importedId] || config[importedId])) }); }) diff --git a/babel-plugin-dotenv/test/fixtures/override-value/.babelrc b/babel-plugin-dotenv/test/fixtures/override-value/.babelrc new file mode 100644 index 0000000..dcc1689 --- /dev/null +++ b/babel-plugin-dotenv/test/fixtures/override-value/.babelrc @@ -0,0 +1,10 @@ +{ + "plugins": [ + "babel-plugin-transform-es2015-modules-commonjs", + ["../../../", { + "replacedModuleName": "babel-dotenv", + "configDir": "test/fixtures/default" + }] + ] +} + diff --git a/babel-plugin-dotenv/test/fixtures/override-value/.env b/babel-plugin-dotenv/test/fixtures/override-value/.env new file mode 100644 index 0000000..5572516 --- /dev/null +++ b/babel-plugin-dotenv/test/fixtures/override-value/.env @@ -0,0 +1,2 @@ +FOO_ENV=abc123 + diff --git a/babel-plugin-dotenv/test/fixtures/override-value/source.js b/babel-plugin-dotenv/test/fixtures/override-value/source.js new file mode 100644 index 0000000..48d8077 --- /dev/null +++ b/babel-plugin-dotenv/test/fixtures/override-value/source.js @@ -0,0 +1,2 @@ +import { FOO_ENV } from 'babel-dotenv'; +console.log(FOO_ENV); diff --git a/babel-plugin-dotenv/test/test.js b/babel-plugin-dotenv/test/test.js index b49ca1c..0e29076 100644 --- a/babel-plugin-dotenv/test/test.js +++ b/babel-plugin-dotenv/test/test.js @@ -11,11 +11,19 @@ var createPluginsWithConfigDir = function(configDir) { } describe('myself in some tests', function() { - it('should throw if variable not exist', function() { + before(function() { + process.env.FOO_ENV = 'foo' + }) + + after(function() { + delete process.env.FOO_ENV + }) + + it('should throw if variable not exist and no environemnt variable is defined', function() { expect(function(){ babel.transformFileSync('test/fixtures/variable-not-exist/source.js') }).to.throwException(function (e) { - expect(e.message).to.contain("Try to import dotenv variable \"foo\" which is not defined in any .env files."); + expect(e.message).to.contain("Try to import dotenv variable \"foo\" which is not defined in any .env files or as an environment variable."); }); }); @@ -27,6 +35,11 @@ describe('myself in some tests', function() { }); }); + it('should override values defined in .env with environment variable', function() { + var result = babel.transformFileSync('test/fixtures/override-value/source.js') + expect(result.code).to.be('\'use strict\';\n\nconsole.log(\'foo\');') + }) + it('should load default env from .env', function(){ var result = babel.transformFileSync('test/fixtures/default/source.js') expect(result.code).to.be('\'use strict\';\n\nconsole.log(\'abc123\');\nconsole.log(\'username\');') From 41654803d7e9087bd9ef2b5ff7b49b9df9fa3b96 Mon Sep 17 00:00:00 2001 From: Aakash Sigdel Date: Sun, 30 Jun 2019 09:49:21 +0200 Subject: [PATCH 3/3] depend on local copy of babel-plugin-dotenv --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index da53eeb..3e1820e 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,7 @@ var path = require('path'); module.exports = () => ({ plugins: [ - [require('babel-plugin-dotenv'), { + [require('./babel-plugin-dotenv'), { replacedModuleName: 'react-native-dotenv', configDir: path.resolve(__dirname, "../../") }],