Skip to content
This repository was archived by the owner on Jul 8, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions babel-plugin-dotenv/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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']);

Choose a reason for hiding this comment

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

Cant we use a different ´env´ then BABEL_ENV to solve the issue with react-native overridin it?

var platformPath = configFile + (environments[process.env.ENV_FILE] ||

then you can run or build you app with $ ENV_FILE =.env.staging npm run ios

Choose a reason for hiding this comment

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

My bad didn't see that it was in the babel plugin. So not possible I guess

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.')
Expand Down
10 changes: 10 additions & 0 deletions babel-plugin-dotenv/test/fixtures/stage-env/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"plugins": [
"babel-plugin-transform-es2015-modules-commonjs",
["../../../", {
"replacedModuleName": "babel-dotenv",
"configDir": "test/fixtures/stage-env"
}],
]
}

2 changes: 2 additions & 0 deletions babel-plugin-dotenv/test/fixtures/stage-env/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
API_KEY=abc123
DEV_USERNAME=username
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DEV_USERNAME=userdonthavename
1 change: 1 addition & 0 deletions babel-plugin-dotenv/test/fixtures/stage-env/.env.staging
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DEV_USERNAME=iamstaging
3 changes: 3 additions & 0 deletions babel-plugin-dotenv/test/fixtures/stage-env/source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { API_KEY, DEV_USERNAME } from 'babel-dotenv';
console.log(API_KEY);
console.log(DEV_USERNAME);
14 changes: 14 additions & 0 deletions babel-plugin-dotenv/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down