Skip to content
This repository has been archived by the owner on Jul 8, 2020. It is now read-only.

Add optional whitelist and defaults #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion babel-plugin-dotenv/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ module.exports = function (data) {

var configDir = options.configDir ? options.configDir : './';
var configFile = options.filename ? options.filename : '.env';
var whitelist = options.whitelist;
var defaults = options.defaults;

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 config = Object.assign(config, dotEnv.config({ path: sysPath.join(configDir, platformPath), silent: true }));
config = Object.assign(defaults || {}, config, dotEnv.config({ path: sysPath.join(configDir, platformPath), silent: true }));

path.node.specifiers.forEach(function(specifier, idx){
if (specifier.type === "ImportDefaultSpecifier") {
Expand All @@ -33,6 +35,9 @@ module.exports = function (data) {
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 (whitelist && !whitelist.includes(importedId)) {
throw path.get('specifiers')[idx].buildCodeFrameError('Try to import dotenv variable "' + importedId + '" which is not included in the whitelist.')
}

var binding = path.scope.getBinding(localId);
binding.referencePaths.forEach(function(refPath){
Expand Down