-
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathindex.js
More file actions
211 lines (181 loc) · 6.92 KB
/
Copy pathindex.js
File metadata and controls
211 lines (181 loc) · 6.92 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
const fs = require('fs')
const path = require('path')
const dotenv = require('dotenv')
function parseDotenvFile (filepath, verbose = false) {
try {
const content = fs.readFileSync(filepath)
return { parsed: dotenv.parse(content), exists: true }
} catch (error) {
// The env file does not exist.
if (verbose) {
console.error('react-native-dotenv', error)
}
return { parsed: {}, exists: false }
}
}
// Babel may re-instantiate this plugin per file (e.g. api.cache(false)). Log once
// per process for the same message so Metro workers do not spam stderr.
const loggedInjectMessages = new Set()
function logInjectedEnv (fileEnv, loadedPaths) {
const keysCount = Object.keys(fileEnv).length
const message = loadedPaths.length > 0
? `◇ injected env (${keysCount}) from ${loadedPaths.join(', ')}`
: `◇ injected env (${keysCount})`
if (loggedInjectMessages.has(message)) {
return
}
loggedInjectMessages.add(message)
console.error(message)
}
function undefObjectAssign (targetObject, sourceObject) {
const keys = Object.keys(sourceObject)
for (const key of keys) {
if (sourceObject[key]) {
targetObject[key] = sourceObject[key]
}
}
return targetObject
}
function safeObjectAssign (targetObject, sourceObject, exceptions = []) {
const keys = Object.keys(targetObject)
for (const key of keys) {
if (targetObject[key] && sourceObject[key]) {
targetObject[key] = sourceObject[key]
}
}
for (const exception of exceptions) {
if (sourceObject[exception]) {
targetObject[exception] = sourceObject[exception]
}
}
return targetObject
}
function mtime (filePath) {
try {
return fs.statSync(filePath).mtimeMs
} catch {
return null
}
}
module.exports = (api, options) => {
const t = api.types
let env = {}
options = {
envName: 'APP_ENV',
moduleName: '@env',
path: '.env',
allowlist: null,
blocklist: null,
safe: false,
allowUndefined: true,
verbose: false,
quiet: false,
...options
}
const babelMode = process.env[options.envName] || (process.env.BABEL_ENV && process.env.BABEL_ENV !== 'undefined' && process.env.BABEL_ENV !== 'development' && process.env.BABEL_ENV) || process.env.NODE_ENV || 'development'
const localFilePath = options.path + '.local'
const modeFilePath = options.path + '.' + babelMode
const modeLocalFilePath = options.path + '.' + babelMode + '.local'
if (options.verbose) {
console.error(`◇ dotenvMode ${babelMode}`)
if (process.env[options.envName] === 'production' || process.env[options.envName] === 'development') {
console.error('APP_ENV error', 'cannot use APP_ENV=development or APP_ENV=production')
}
}
api.cache.using(() => mtime(options.path))
api.cache.using(() => mtime(modeFilePath))
api.cache.using(() => mtime(localFilePath))
api.cache.using(() => mtime(modeLocalFilePath))
const dotenvTemporary = undefObjectAssign({}, process.env)
const parsedFile = parseDotenvFile(options.path, options.verbose)
const localFile = parseDotenvFile(localFilePath, options.verbose)
const modeFile = parseDotenvFile(modeFilePath, options.verbose)
const modeLocalFile = parseDotenvFile(modeLocalFilePath, options.verbose)
const modeExceptions = ['NODE_ENV', 'BABEL_ENV', options.envName]
const fileEnv = undefObjectAssign(
undefObjectAssign(
undefObjectAssign(parsedFile.parsed, modeFile.parsed),
localFile.parsed
),
modeLocalFile.parsed
)
// process.env.X is only inlined for keys from .env files (+ mode exceptions).
// That stops build-tooling pollution (e.g. Metro jest-worker) without hardcoding
// tool-specific names. Host/CI values still flow through @env imports below.
const processEnvInlineKeys = new Set(Object.keys(fileEnv))
for (const exception of modeExceptions) {
processEnvInlineKeys.add(exception)
}
env = (options.safe)
? safeObjectAssign(undefObjectAssign({}, fileEnv), dotenvTemporary, modeExceptions)
: undefObjectAssign(undefObjectAssign({}, fileEnv), dotenvTemporary)
if (options.verbose || !options.quiet) {
const loadedPaths = [
[options.path, parsedFile.exists],
[modeFilePath, modeFile.exists],
[localFilePath, localFile.exists],
[modeLocalFilePath, modeLocalFile.exists]
]
.filter(([, exists]) => exists)
.map(([filepath]) => filepath)
logInjectedEnv(fileEnv, loadedPaths)
}
if (typeof api.addExternalDependency === 'function') {
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',
visitor: {
ImportDeclaration (filepath) {
if (filepath.node.source.value !== options.moduleName) {
return
}
for (const [index, specifier] of filepath.node.specifiers.entries()) {
if (specifier.type === 'ImportDefaultSpecifier') {
throw filepath.get('specifiers')[index].buildCodeFrameError('Default import is not supported')
}
if (specifier.type === 'ImportNamespaceSpecifier') {
throw filepath.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 filepath.get('specifiers')[index].buildCodeFrameError(`"${importedId}" was not present in allowlist`)
}
if (Array.isArray(options.blocklist) && options.blocklist.includes(importedId)) {
throw filepath.get('specifiers')[index].buildCodeFrameError(`"${importedId}" was not present in blocklist`)
}
if (!options.allowUndefined && !Object.hasOwn(env, importedId)) {
throw filepath.get('specifiers')[index].buildCodeFrameError(`"${importedId}" is not defined in ${options.path}`)
}
const binding = filepath.scope.getBinding(localId)
for (const referencePath of binding.referencePaths) {
referencePath.replaceWith(t.valueToNode(env[importedId]))
}
}
}
filepath.remove()
},
MemberExpression (filepath) {
if (!filepath.get('object').matchesPattern('process.env')) {
return
}
const key = filepath.toComputedKey()
if (t.isStringLiteral(key)) {
const importedId = key.value
if (!processEnvInlineKeys.has(importedId)) {
return
}
const value = Object.hasOwn(env, importedId) ? env[importedId] : process.env[importedId]
if (value !== undefined) {
filepath.replaceWith(t.valueToNode(value))
}
}
}
}
})
}