forked from tusbar/babel-plugin-dotenv-import
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (53 loc) · 1.99 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const {readFileSync} = require('fs')
const dotenv = require('dotenv')
module.exports = ({types: t}) => ({
name: 'dotenv-import',
pre() {
this.opts = Object.assign({
moduleName: '@env',
path: '.env',
whitelist: null,
blacklist: null,
safe: false,
allowUndefined: false
}, this.opts)
if (this.opts.safe) {
this.env = dotenv.parse(readFileSync(this.opts.path))
} else {
dotenv.config({
path: this.opts.path
})
this.env = process.env
}
},
visitor: {
ImportDeclaration(path, {opts}) {
if (path.node.source.value === opts.moduleName) {
path.node.specifiers.forEach((specifier, idx) => {
if (specifier.type === 'ImportDefaultSpecifier') {
throw path.get('specifiers')[idx].buildCodeFrameError('Default import is not supported')
}
if (specifier.type === 'ImportNamespaceSpecifier') {
throw path.get('specifiers')[idx].buildCodeFrameError('Wildcard import is not supported')
}
const importedId = specifier.imported.name
const localId = specifier.local.name
if (Array.isArray(opts.whitelist) && !opts.whitelist.includes(importedId)) {
throw path.get('specifiers')[idx].buildCodeFrameError(`"${importedId}" was not whitelisted`)
}
if (Array.isArray(opts.blacklist) && opts.blacklist.includes(importedId)) {
throw path.get('specifiers')[idx].buildCodeFrameError(`"${importedId}" was blacklisted`)
}
if (!opts.allowUndefined && !Object.prototype.hasOwnProperty.call(this.env, importedId)) {
throw path.get('specifiers')[idx].buildCodeFrameError(`"${importedId}" is not defined in ${opts.path}`)
}
const binding = path.scope.getBinding(localId)
binding.referencePaths.forEach(refPath => {
refPath.replaceWith(t.valueToNode(this.env[importedId]))
})
})
path.remove()
}
}
}
})