diff --git a/babel-plugin-dotenv/index.js b/babel-plugin-dotenv/index.js index d43d29b..c73d84e 100644 --- a/babel-plugin-dotenv/index.js +++ b/babel-plugin-dotenv/index.js @@ -6,6 +6,12 @@ var process = require('process'); module.exports = function (data) { var t = data.types; + var environments = { + development: '.development', + production: '.production', + staging: '.staging' + } + return { visitor: { ImportDeclaration: function(path, state) { @@ -19,11 +25,8 @@ module.exports = function (data) { if (path.node.source.value === options.replacedModuleName) { var config = dotEnv.config({ path: sysPath.join(configDir, configFile), silent: true }) || {}; - var platformPath = (process.env.BABEL_ENV === 'development' || process.env.BABEL_ENV === undefined) - ? configFile + '.development' - : configFile + '.production'; + var platformPath = configFile + (environments[process.env.BABEL_ENV] || environments['development']); var config = Object.assign(config, dotEnv.config({ path: sysPath.join(configDir, platformPath), silent: true })); - path.node.specifiers.forEach(function(specifier, idx){ if (specifier.type === "ImportDefaultSpecifier") { throw path.get('specifiers')[idx].buildCodeFrameError('Import dotenv as default is not supported.') diff --git a/babel-plugin-dotenv/test/fixtures/stage-env/.babelrc b/babel-plugin-dotenv/test/fixtures/stage-env/.babelrc new file mode 100644 index 0000000..83c4dd8 --- /dev/null +++ b/babel-plugin-dotenv/test/fixtures/stage-env/.babelrc @@ -0,0 +1,10 @@ +{ + "plugins": [ + "babel-plugin-transform-es2015-modules-commonjs", + ["../../../", { + "replacedModuleName": "babel-dotenv", + "configDir": "test/fixtures/stage-env" + }], + ] + } + \ No newline at end of file diff --git a/babel-plugin-dotenv/test/fixtures/stage-env/.env b/babel-plugin-dotenv/test/fixtures/stage-env/.env new file mode 100644 index 0000000..0a8c6a9 --- /dev/null +++ b/babel-plugin-dotenv/test/fixtures/stage-env/.env @@ -0,0 +1,2 @@ +API_KEY=abc123 +DEV_USERNAME=username diff --git a/babel-plugin-dotenv/test/fixtures/stage-env/.env.development b/babel-plugin-dotenv/test/fixtures/stage-env/.env.development new file mode 100644 index 0000000..1b46714 --- /dev/null +++ b/babel-plugin-dotenv/test/fixtures/stage-env/.env.development @@ -0,0 +1 @@ +DEV_USERNAME=userdonthavename diff --git a/babel-plugin-dotenv/test/fixtures/stage-env/.env.staging b/babel-plugin-dotenv/test/fixtures/stage-env/.env.staging new file mode 100644 index 0000000..77d09cb --- /dev/null +++ b/babel-plugin-dotenv/test/fixtures/stage-env/.env.staging @@ -0,0 +1 @@ +DEV_USERNAME=iamstaging diff --git a/babel-plugin-dotenv/test/fixtures/stage-env/source.js b/babel-plugin-dotenv/test/fixtures/stage-env/source.js new file mode 100644 index 0000000..5b6a022 --- /dev/null +++ b/babel-plugin-dotenv/test/fixtures/stage-env/source.js @@ -0,0 +1,3 @@ +import { API_KEY, DEV_USERNAME } from 'babel-dotenv'; +console.log(API_KEY); +console.log(DEV_USERNAME); diff --git a/babel-plugin-dotenv/test/test.js b/babel-plugin-dotenv/test/test.js index c2f7725..6266099 100644 --- a/babel-plugin-dotenv/test/test.js +++ b/babel-plugin-dotenv/test/test.js @@ -47,6 +47,20 @@ describe('myself in some tests', function() { var result = babel.transformFileSync('test/fixtures/prod-env/source.js') expect(result.code).to.be('\'use strict\';\n\nconsole.log(\'abc123\');\nconsole.log(\'foobar\');') process.env['BABEL_ENV'] = undefined; + }) + + it('should load let .env.staging overwrite .env', function(){ + process.env['BABEL_ENV'] = 'staging'; + var result = babel.transformFileSync('test/fixtures/stage-env/source.js'); + expect(result.code).to.be('\'use strict\';\n\nconsole.log(\'abc123\');\nconsole.log(\'iamstaging\');'); + process.env['BABEL_ENV'] = undefined; + }) + + it('should load .env.development if .env.environment does not exist', function(){ + process.env['BABEL_ENV'] = 'stage'; + var result = babel.transformFileSync('test/fixtures/stage-env/source.js'); + expect(result.code).to.be('\'use strict\';\n\nconsole.log(\'abc123\');\nconsole.log(\'userdonthavename\');'); + process.env['BABEL_ENV'] = undefined; }) it('should support `as alias` import syntax', function(){