-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathindex.js
210 lines (177 loc) · 5.79 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
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
import dotenv from 'dotenv-defaults'
import fs from 'fs'
// Mostly taken from here: https://github.com/motdotla/dotenv-expand/blob/master/lib/main.js#L4
const interpolate = (env, vars) => {
const matches = env.match(/\$([a-zA-Z0-9_]+)|\${([a-zA-Z0-9_]+)}/g) || []
matches.forEach((match) => {
const key = match.replace(/\$|{|}/g, '')
let variable = vars[key] || ''
variable = interpolate(variable, vars)
env = env.replace(match, variable)
})
return env
}
const isMainThreadElectron = (target) =>
target.startsWith('electron') && target.endsWith('main')
class Dotenv {
/**
* The dotenv-webpack plugin.
* @param {Object} options - The parameters.
* @param {String} [options.path=./.env] - The location of the environment variable.
* @param {Boolean|String} [options.safe=false] - If false ignore safe-mode, if true load `'./.env.example'`, if a string load that file as the sample.
* @param {Boolean} [options.systemvars=false] - If true, load system environment variables.
* @param {Boolean} [options.silent=false] - If true, suppress warnings, if false, display warnings.
* @param {String} [options.prefix=process.env.] - The prefix, used to denote environment variables.
* @returns {webpack.DefinePlugin}
*/
constructor (config = {}) {
this.config = Object.assign({}, {
path: './.env',
prefix: 'process.env.'
}, config)
}
apply (compiler) {
const variables = this.gatherVariables()
const target = compiler.options.target ?? 'web'
const version = (compiler.webpack && compiler.webpack.version) || '4'
const data = this.formatData({
variables,
target,
version
})
const DefinePlugin = (compiler.webpack && compiler.webpack.DefinePlugin) || require('webpack').DefinePlugin
new DefinePlugin(data).apply(compiler)
}
gatherVariables () {
const { safe, allowEmptyValues } = this.config
const vars = this.initializeVars()
const { env, blueprint } = this.getEnvs()
Object.keys(blueprint).forEach(key => {
const value = Object.prototype.hasOwnProperty.call(vars, key) ? vars[key] : env[key]
const isMissing = typeof value === 'undefined' || value === null ||
(!allowEmptyValues && value === '')
if (safe && isMissing) {
throw new Error(`Missing environment variable: ${key}`)
} else {
vars[key] = value
}
})
// add the leftovers
if (safe) {
Object.keys(env).forEach((key) => {
if (!Object.prototype.hasOwnProperty.call(vars, key)) {
vars[key] = env[key]
}
})
}
return vars
}
initializeVars () {
return (this.config.systemvars) ? Object.assign({}, process.env) : {}
}
getEnvs () {
const { path, silent, safe } = this.config
const env = dotenv.parse(this.loadFile({
file: path,
silent
}), this.getDefaults())
let blueprint = env
if (safe) {
let file = `${path}.example`
if (safe !== true) {
file = safe
}
blueprint = dotenv.parse(this.loadFile({
file,
silent
}))
}
return {
env,
blueprint
}
}
getDefaults () {
const { path, silent, defaults } = this.config
if (defaults) {
return this.loadFile({
file: defaults === true ? `${path}.defaults` : defaults,
silent
})
}
return ''
}
formatData ({ variables = {}, target, version }) {
const { expand, prefix } = this.config
const formatted = Object.keys(variables).reduce((obj, key) => {
const v = variables[key]
const vKey = `${prefix}${key}`
let vValue
if (expand) {
if (v.substring(0, 2) === '\\$') {
vValue = v.substring(1)
} else if (v.indexOf('\\$') > 0) {
vValue = v.replace(/\\\$/g, '$')
} else {
vValue = interpolate(v, variables)
}
} else {
vValue = v
}
obj[vKey] = JSON.stringify(vValue)
return obj
}, {})
// We have to stub any remaining `process.env`s due to Webpack 5 not polyfilling it anymore
// https://github.com/mrsteele/dotenv-webpack/issues/240#issuecomment-710231534
// However, if someone targets Node or Electron `process.env` still exists, and should therefore be kept
// https://webpack.js.org/configuration/target
if (this.shouldStub({ target, version })) {
// Results in `"MISSING_ENV_VAR".NAME` which is valid JS
formatted['process.env'] = '"MISSING_ENV_VAR"'
}
return formatted
}
shouldStub ({ target: targetInput, version }) {
if (!version.startsWith('5')) {
return false
}
const targets = Array.isArray(targetInput) ? targetInput : [targetInput]
return targets.every(
target =>
// If configured prefix is 'process.env'
this.config.prefix === 'process.env.' &&
// If we're not configured to never stub
this.config.ignoreStub !== true &&
// And
(
// We are configured to always stub
this.config.ignoreStub === false ||
// Or if we should according to the target
(!target.includes('node') && !isMainThreadElectron(target))
)
)
}
/**
* Load a file.
* @param {String} config.file - The file to load.
* @param {Boolean} config.silent - If true, suppress warnings, if false, display warnings.
* @returns {Object}
*/
loadFile ({ file, silent }) {
try {
return fs.readFileSync(file, 'utf8')
} catch (err) {
this.warn(`Failed to load ${file}.`, silent)
return {}
}
}
/**
* Displays a console message if 'silent' is falsey
* @param {String} msg - The message.
* @param {Boolean} silent - If true, display the message, if false, suppress the message.
*/
warn (msg, silent) {
!silent && console.warn(msg)
}
}
export default Dotenv