Skip to content

Commit

Permalink
[allow-undefined] closes zetachang#41
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil Barber committed Dec 30, 2019
1 parent a56b304 commit 8bdb6ab
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 10 deletions.
4 changes: 0 additions & 4 deletions babel-plugin-dotenv/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ 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.')
}

var binding = path.scope.getBinding(localId);
binding.referencePaths.forEach(function(refPath){
refPath.replaceWith(t.valueToNode(config[importedId]))
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import { foo } from "babel-dotenv";
console.log(foo);
10 changes: 4 additions & 6 deletions babel-plugin-dotenv/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ var createPluginsWithConfigDir = function(configDir) {
}

describe('myself in some tests', function() {
it('should throw if variable not exist', 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.");
});
it('should set to undefined if variable does not exist', function() {
const result = babel.transformFileSync('test/fixtures/variable-not-exist/source.js')
console.log(result.code)
expect(result.code).to.be('"use strict";\n\nconsole.log(undefined);')
});

it('should throw if default is imported', function() {
Expand Down

0 comments on commit 8bdb6ab

Please sign in to comment.