diff --git a/babel-plugin-dotenv/index.js b/babel-plugin-dotenv/index.js index cff3a39..590950b 100644 --- a/babel-plugin-dotenv/index.js +++ b/babel-plugin-dotenv/index.js @@ -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") { @@ -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){