Skip to content
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
90 changes: 29 additions & 61 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function parseDotenvFile(path, verbose = false) {
let content

try {
content = fs.readFileSync(path)
content = fs.readFileSync(path, 'utf8')
} catch (error) {
// The env file does not exist.
if (verbose) {
Expand Down Expand Up @@ -93,72 +93,40 @@ module.exports = (api, options) => {
const localParsed = parseDotenvFile(localFilePath, options.verbose)
const modeParsed = parseDotenvFile(modeFilePath, options.verbose)
const modeLocalParsed = parseDotenvFile(modeLocalFilePath, options.verbose)
env = (options.safe) ? safeObjectAssign(undefObjectAssign(undefObjectAssign(undefObjectAssign(parsed, modeParsed), localParsed), modeLocalParsed), dotenvTemporary, ['NODE_ENV', 'BABEL_ENV', options.envName])
: undefObjectAssign(undefObjectAssign(undefObjectAssign(undefObjectAssign(parsed, modeParsed), localParsed), modeLocalParsed), dotenvTemporary)
env = (options.safe) ? safeObjectAssign(undefObjectAssign(undefObjectAssign(undefObjectAssign(parsed, modeParsed), localParsed), modeLocalParsed), dotenvTemporary, ['NODE_ENV', 'BABEL_ENV', 'APP_ENV']) : undefObjectAssign(undefObjectAssign(undefObjectAssign(undefObjectAssign(parsed, modeParsed), localParsed), modeLocalParsed), dotenvTemporary)

api.addExternalDependency(path.resolve(options.path))
api.addExternalDependency(path.resolve(modeFilePath))
api.addExternalDependency(path.resolve(localFilePath))
api.addExternalDependency(path.resolve(modeLocalFilePath))

return ({
name: 'dotenv-import',
return {
visitor: {
ImportDeclaration(path) {
if (path.node.source.value === options.moduleName) {
for (const [index, specifier] of path.node.specifiers.entries()) {
if (specifier.type === 'ImportDefaultSpecifier') {
throw path.get('specifiers')[index].buildCodeFrameError('Default import is not supported')
}

if (specifier.type === 'ImportNamespaceSpecifier') {
throw path.get('specifiers')[index].buildCodeFrameError('Wildcard import is not supported')
}

if (specifier.imported && specifier.local) {
const importedId = specifier.imported.name
const localId = specifier.local.name

if (Array.isArray(options.allowlist) && !options.allowlist.includes(importedId)) {
throw path.get('specifiers')[index].buildCodeFrameError(`"${importedId}" was not present in allowlist`)
} else if (Array.isArray(options.whitelist) && !options.whitelist.includes(importedId)) {
console.warn('[DEPRECATION WARNING] This option is will be deprecated soon. Use allowlist instead')
throw path.get('specifiers')[index].buildCodeFrameError(`"${importedId}" was not whitelisted`)
}

if (Array.isArray(options.blocklist) && options.blocklist.includes(importedId)) {
throw path.get('specifiers')[index].buildCodeFrameError(`"${importedId}" was not present in blocklist`)
} else if (Array.isArray(options.blacklist) && options.blacklist.includes(importedId)) {
console.warn('[DEPRECATION WARNING] This option is will be deprecated soon. Use blocklist instead')
throw path.get('specifiers')[index].buildCodeFrameError(`"${importedId}" was blacklisted`)
}

if (!options.allowUndefined && !Object.hasOwn(env, importedId)) {
throw path.get('specifiers')[index].buildCodeFrameError(`"${importedId}" is not defined in ${options.path}`)
}

const binding = path.scope.getBinding(localId)
for (const referencePath of binding.referencePaths) {
referencePath.replaceWith(t.valueToNode(env[importedId]))
}
}
const specifiers = path.node.specifiers
const identifiers = specifiers.map(specifier => specifier.local.name)

if (specifiers.some(specifier => specifier.type === 'ImportDefaultSpecifier')) {
throw path.buildCodeFrameError('Default import is not supported')
}

path.remove()
}
},
MemberExpression(path) {
if (path.get('object').matchesPattern('process.env')) {
const key = path.toComputedKey()
if (t.isStringLiteral(key)) {
const importedId = key.value
const value = (env && importedId in env) ? env[importedId] : process.env[importedId]
if (value !== undefined) {
path.replaceWith(t.valueToNode(value))
}
if (specifiers.some(specifier => specifier.type === 'ImportNamespaceSpecifier')) {
throw path.buildCodeFrameError('Wildcard import is not supported')
}

const missingIdentifiers = identifiers.filter(identifier => !env[identifier])
if (missingIdentifiers.length > 0 && !options.allowUndefined) {
throw path.buildCodeFrameError(`"${missingIdentifiers.join(', ')}" is not defined in .env`)
}

path.replaceWithMultiple(
identifiers.map(identifier =>
t.variableDeclaration('const', [
t.variableDeclarator(
t.identifier(identifier),
t.stringLiteral(env[identifier] || '')
)
])
)
)
}
},
},
})
}
}
}
}