dotenv-webpack 8.1.1 → 10.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -16
- package/package.json +36 -45
- package/src/index.js +190 -0
- package/types/index.d.ts +63 -0
- package/browser.js +0 -3
- package/dist/index.js +0 -268
- package/index.js +0 -16
package/README.md
CHANGED
|
@@ -10,31 +10,26 @@ A secure webpack plugin that supports dotenv and other environment variables and
|
|
|
10
10
|
<a href="https://www.npmjs.com/package/dotenv-webpack" target="_blank">
|
|
11
11
|
<img alt="npm" src="https://img.shields.io/npm/v/dotenv-webpack.svg?maxAge=0&style=flat" />
|
|
12
12
|
</a>
|
|
13
|
-
<a href="https://codecov.io/gh/mrsteele/dotenv-webpack" target="_blank">
|
|
14
|
-
<img alt="codecov" src="https://codecov.io/gh/mrsteele/dotenv-webpack/branch/master/graph/badge.svg" />
|
|
15
|
-
</a>
|
|
16
13
|
<a href="https://github.com/mrsteele/dotenv-webpack/actions/workflows/main.yml" target="_blank">
|
|
17
14
|
<img alt="Main" src="https://github.com/mrsteele/dotenv-webpack/actions/workflows/main.yml/badge.svg" />
|
|
18
15
|
</a>
|
|
19
|
-
<img alt="Vulnerabilities Score" src="https://snyk-widget.herokuapp.com/badge/npm/dotenv-webpack/badge.svg" />
|
|
20
|
-
<img alt="Known Vulnerabilities" src="https://snyk.io/test/github/mrsteele/dotenv-webpack/badge.svg" />
|
|
21
|
-
<object id="badge" data="https://snyk-widget.herokuapp.com/badge/npm/dotenv-webpack/badge.svg" type="image/svg+xml"></object>
|
|
22
|
-
<a href="https://www.dotenv.org/get-started?r=3" target="_blank">
|
|
23
|
-
<img alt="dotenv-vault" src="https://badge.dotenv.org/works-with.svg?r=3" />
|
|
24
|
-
</a>
|
|
25
16
|
</div>
|
|
26
17
|
|
|
27
18
|
## Installation
|
|
28
19
|
|
|
29
20
|
Include the package locally in your repository.
|
|
30
21
|
|
|
31
|
-
|
|
22
|
+
```bash
|
|
23
|
+
npm install dotenv-webpack --save-dev
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Requires Node.js 20 or later and webpack 5. Upgrading from v9? See the [migration guide](MIGRATION.md).
|
|
32
27
|
|
|
33
28
|
## Description
|
|
34
29
|
|
|
35
|
-
`dotenv-webpack` wraps `dotenv` and `
|
|
30
|
+
`dotenv-webpack` wraps [`dotenv-defaults`](https://www.npmjs.com/package/dotenv-defaults) (and so `dotenv`) and webpack's `DefinePlugin`. As such, it does a text replace in the resulting bundle for any instances of `process.env`.
|
|
36
31
|
|
|
37
|
-
Your `.env` files can include sensitive information. Because of this
|
|
32
|
+
Your `.env` files can include sensitive information. Because of this, `dotenv-webpack` will only expose environment variables that are **explicitly referenced in your code** to your final bundle.
|
|
38
33
|
|
|
39
34
|
Interested in taking your environments to the next level? Check out the [Dotenv Organization](https://www.dotenv.org/get-started?r=3).
|
|
40
35
|
|
|
@@ -67,6 +62,17 @@ module.exports = {
|
|
|
67
62
|
};
|
|
68
63
|
```
|
|
69
64
|
|
|
65
|
+
ES module configs work too:
|
|
66
|
+
|
|
67
|
+
```javascript
|
|
68
|
+
// webpack.config.mjs
|
|
69
|
+
import Dotenv from 'dotenv-webpack';
|
|
70
|
+
|
|
71
|
+
export default {
|
|
72
|
+
plugins: [new Dotenv()]
|
|
73
|
+
};
|
|
74
|
+
```
|
|
75
|
+
|
|
70
76
|
### Use in your code
|
|
71
77
|
|
|
72
78
|
```javascript
|
|
@@ -97,7 +103,7 @@ Due to the fact that we use `webpack.DefinePlugin` under the hood, we cannot sup
|
|
|
97
103
|
|
|
98
104
|
## `process.env` stubbing / replacing
|
|
99
105
|
|
|
100
|
-
`process.env` is not polyfilled in
|
|
106
|
+
`process.env` is not polyfilled in webpack 5, leading to errors in environments where `process` is not defined (browsers).
|
|
101
107
|
|
|
102
108
|
We automatically replace any remaining `process.env`s in these environments with `"MISSING_ENV_VAR"` to avoid these errors.
|
|
103
109
|
|
|
@@ -109,12 +115,12 @@ If you are running into issues where you or another package you use interfaces w
|
|
|
109
115
|
|
|
110
116
|
Use the following properties to configure your instance.
|
|
111
117
|
|
|
112
|
-
* **path** (`'./.env'`) - The path to your environment variables. This same path applies to the `.env.example` and `.env.defaults` files. [Read more here](#about-path-settings).
|
|
118
|
+
* **path** (`'./.env'`) - The path to your environment variables. This same path applies to the `.env.example` and `.env.defaults` files. Set to `false` to skip the env file (for example, to use only `systemvars` or `defaults`). [Read more here](#about-path-settings).
|
|
113
119
|
* **safe** (`false`) - If true, load '.env.example' to verify the '.env' variables are all set. Can also be a string to a different file.
|
|
114
120
|
* **allowEmptyValues** (`false`) - Whether to allow empty strings in safe mode. If false, will throw an error if any env variables are empty (but only if safe mode is enabled).
|
|
115
121
|
* **systemvars** (`false`) - Set to true if you would rather load all system variables as well (useful for CI purposes).
|
|
116
122
|
* **silent** (`false`) - If true, all warnings will be suppressed.
|
|
117
|
-
* **expand** (`false`) - Allows your variables to be "expanded" for reusability within your `.env` file.
|
|
123
|
+
* **expand** (`false`) - Allows your variables to be "expanded" for reusability within your `.env` file: `$NAME` and `${NAME}` are replaced with that variable's value (or an empty string if it isn't set). Write `\$NAME` to keep a literal `$NAME`.
|
|
118
124
|
* **defaults** (`false`) - Adds support for `dotenv-defaults`. If set to `true`, uses `./.env.defaults`. If a string, uses that location for a defaults file. Read more at [npm](https://www.npmjs.com/package/dotenv-defaults).
|
|
119
125
|
* **ignoreStub** (`false`) - Override the automatic check whether to stub `process.env`. [Read more here](#user-content-processenv-stubbing--replacing).
|
|
120
126
|
* **prefix** (`'process.env.'`) - The prefix to use before the name of your env variables.
|
|
@@ -130,8 +136,10 @@ module.exports = {
|
|
|
130
136
|
safe: true, // load '.env.example' to verify the '.env' variables are all set. Can also be a string to a different file.
|
|
131
137
|
allowEmptyValues: true, // allow empty variables (e.g. `FOO=`) (treat it as empty string, rather than missing)
|
|
132
138
|
systemvars: true, // load all the predefined 'process.env' variables which will trump anything local per dotenv specs.
|
|
133
|
-
silent: true, // hide any
|
|
139
|
+
silent: true, // hide any warnings about files that can't be read
|
|
140
|
+
expand: true, // expand `$NAME` and `${NAME}` references in values
|
|
134
141
|
defaults: false, // load '.env.defaults' as the default values if empty.
|
|
142
|
+
ignoreStub: true, // never replace leftover `process.env` references
|
|
135
143
|
prefix: 'import.meta.env.' // reference your env variables as 'import.meta.env.ENV_VAR'.
|
|
136
144
|
})
|
|
137
145
|
]
|
package/package.json
CHANGED
|
@@ -1,30 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dotenv-webpack",
|
|
3
3
|
"description": "A simple webpack plugin to support dotenv.",
|
|
4
|
-
"version": "
|
|
5
|
-
"
|
|
4
|
+
"version": "10.0.0",
|
|
5
|
+
"type": "commonjs",
|
|
6
|
+
"main": "./src/index.js",
|
|
7
|
+
"types": "./types/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./types/index.d.ts",
|
|
11
|
+
"default": "./src/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./package.json": "./package.json"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"src",
|
|
17
|
+
"types/index.d.ts"
|
|
18
|
+
],
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=20"
|
|
21
|
+
},
|
|
6
22
|
"scripts": {
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"pretest": "npm run build",
|
|
13
|
-
"test": "jest",
|
|
14
|
-
"prebuild": "rimraf dist",
|
|
15
|
-
"build": "babel --copy-files --out-dir dist src",
|
|
16
|
-
"travis": "npm run lint && npm t"
|
|
23
|
+
"lint": "eslint .",
|
|
24
|
+
"typecheck": "tsc -p test/types/tsconfig.json",
|
|
25
|
+
"test": "node --test --test-reporter=spec test/index.test.js",
|
|
26
|
+
"coverage": "node --test --experimental-test-coverage --test-coverage-include=src/** test/index.test.js",
|
|
27
|
+
"check:package": "publint --strict"
|
|
17
28
|
},
|
|
18
29
|
"repository": {
|
|
19
30
|
"type": "git",
|
|
20
|
-
"url": "https://github.com/mrsteele/dotenv-webpack.git"
|
|
21
|
-
},
|
|
22
|
-
"jest": {
|
|
23
|
-
"coverageDirectory": "./coverage/",
|
|
24
|
-
"collectCoverage": true,
|
|
25
|
-
"watchPathIgnorePatterns": [
|
|
26
|
-
"output/.*?"
|
|
27
|
-
]
|
|
31
|
+
"url": "git+https://github.com/mrsteele/dotenv-webpack.git"
|
|
28
32
|
},
|
|
29
33
|
"keywords": [
|
|
30
34
|
"dotenv",
|
|
@@ -38,41 +42,28 @@
|
|
|
38
42
|
"webpack",
|
|
39
43
|
"plugin"
|
|
40
44
|
],
|
|
41
|
-
"author": "Matt Steele <
|
|
45
|
+
"author": "Matt Steele <hi@mrsteele.dev> (http://mrsteele.dev/)",
|
|
42
46
|
"license": "MIT",
|
|
43
47
|
"bugs": {
|
|
44
48
|
"url": "https://github.com/mrsteele/dotenv-webpack/issues"
|
|
45
49
|
},
|
|
46
50
|
"homepage": "https://github.com/mrsteele/dotenv-webpack#readme",
|
|
47
|
-
"
|
|
48
|
-
"
|
|
51
|
+
"publishConfig": {
|
|
52
|
+
"access": "public",
|
|
53
|
+
"provenance": true
|
|
49
54
|
},
|
|
50
55
|
"peerDependencies": {
|
|
51
|
-
"webpack": "^
|
|
56
|
+
"webpack": "^5"
|
|
52
57
|
},
|
|
53
58
|
"dependencies": {
|
|
54
|
-
"dotenv-defaults": "^
|
|
59
|
+
"dotenv-defaults": "^7.0.0"
|
|
55
60
|
},
|
|
56
61
|
"devDependencies": {
|
|
57
|
-
"@
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
"jsdoc": "^3.6.10",
|
|
64
|
-
"rimraf": "^3.0.0",
|
|
65
|
-
"sinon": "^11.1.1",
|
|
66
|
-
"standard": "^16.0.3",
|
|
67
|
-
"webpack": "^5.24.3"
|
|
68
|
-
},
|
|
69
|
-
"files": [
|
|
70
|
-
"dist"
|
|
71
|
-
],
|
|
72
|
-
"browser": "browser.js",
|
|
73
|
-
"babel": {
|
|
74
|
-
"presets": [
|
|
75
|
-
"@babel/preset-env"
|
|
76
|
-
]
|
|
62
|
+
"@types/node": "^20.19.43",
|
|
63
|
+
"eslint": "^10.16.0",
|
|
64
|
+
"neostandard": "^0.14.1",
|
|
65
|
+
"publint": "^0.3.25",
|
|
66
|
+
"typescript": "^6.0.3",
|
|
67
|
+
"webpack": "^5.113.0"
|
|
77
68
|
}
|
|
78
69
|
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { readFileSync } = require('node:fs')
|
|
4
|
+
const { parse } = require('dotenv-defaults')
|
|
5
|
+
|
|
6
|
+
const DEFAULT_PATH = './.env'
|
|
7
|
+
const DEFAULT_PREFIX = 'process.env.'
|
|
8
|
+
|
|
9
|
+
// `$NAME` or `${NAME}`, optionally escaped with a leading backslash (`\$NAME`).
|
|
10
|
+
const VARIABLE = /(\\)?\$(?:\{(\w+)\}|(\w+))/g
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Expands `$NAME` and `${NAME}` references in a value. Unknown names expand
|
|
14
|
+
* to an empty string, `\$NAME` is kept as a literal `$NAME`, and a reference
|
|
15
|
+
* back to a variable that is already being expanded (`A=$B`, `B=$A`) expands
|
|
16
|
+
* to an empty string instead of recursing forever.
|
|
17
|
+
* @param {string} value - The value to expand.
|
|
18
|
+
* @param {Record<string, string | undefined>} vars - The variables to read from.
|
|
19
|
+
* @param {Set<string>} [seen] - The names being expanded, to detect cycles.
|
|
20
|
+
* @returns {string}
|
|
21
|
+
*/
|
|
22
|
+
const interpolate = (value, vars, seen = new Set()) =>
|
|
23
|
+
value.replace(VARIABLE, (match, escaped, braced, bare) => {
|
|
24
|
+
if (escaped) {
|
|
25
|
+
return match.slice(1)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const key = braced ?? bare
|
|
29
|
+
const next = Object.hasOwn(vars, key) ? vars[key] : undefined
|
|
30
|
+
if (next == null || seen.has(key)) {
|
|
31
|
+
return ''
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return interpolate(next, vars, new Set(seen).add(key))
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
const isMainThreadElectron = (target) =>
|
|
38
|
+
target.startsWith('electron') && target.endsWith('main')
|
|
39
|
+
|
|
40
|
+
class Dotenv {
|
|
41
|
+
/**
|
|
42
|
+
* The dotenv-webpack plugin.
|
|
43
|
+
* @param {import('../types').DotenvOptions} [config] - The options.
|
|
44
|
+
*/
|
|
45
|
+
constructor (config = {}) {
|
|
46
|
+
this.config = {
|
|
47
|
+
...config,
|
|
48
|
+
path: config.path ?? DEFAULT_PATH,
|
|
49
|
+
prefix: config.prefix ?? DEFAULT_PREFIX
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @param {import('webpack').Compiler} compiler
|
|
55
|
+
*/
|
|
56
|
+
apply (compiler) {
|
|
57
|
+
const { DefinePlugin } = compiler.webpack
|
|
58
|
+
const data = this.formatData({
|
|
59
|
+
variables: this.gatherVariables(),
|
|
60
|
+
target: compiler.options.target ?? 'web'
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
new DefinePlugin(data).apply(compiler)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
gatherVariables () {
|
|
67
|
+
const { safe, allowEmptyValues, systemvars } = this.config
|
|
68
|
+
const { env, blueprint } = this.getEnvs()
|
|
69
|
+
|
|
70
|
+
// System variables win over anything read from a file.
|
|
71
|
+
const vars = systemvars ? { ...env, ...process.env } : { ...env }
|
|
72
|
+
|
|
73
|
+
if (safe) {
|
|
74
|
+
for (const key of Object.keys(blueprint)) {
|
|
75
|
+
const value = Object.hasOwn(vars, key) ? vars[key] : undefined
|
|
76
|
+
if (value == null || (!allowEmptyValues && value === '')) {
|
|
77
|
+
throw new Error(`Missing environment variable: ${key}`)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return vars
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
getEnvs () {
|
|
86
|
+
const { safe } = this.config
|
|
87
|
+
const env = parse(this.loadFile(this.config.path), this.getDefaults())
|
|
88
|
+
|
|
89
|
+
if (!safe) {
|
|
90
|
+
return { env, blueprint: env }
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const file = safe === true ? `${this.basePath()}.example` : safe
|
|
94
|
+
return { env, blueprint: parse(this.loadFile(file)) }
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
getDefaults () {
|
|
98
|
+
const { defaults } = this.config
|
|
99
|
+
|
|
100
|
+
if (!defaults) {
|
|
101
|
+
return ''
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return this.loadFile(defaults === true ? `${this.basePath()}.defaults` : defaults)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* The path that `.example` and `.defaults` are appended to. With
|
|
109
|
+
* `path: false` no env file is read, but `./.env.example` and
|
|
110
|
+
* `./.env.defaults` can still be.
|
|
111
|
+
* @returns {string}
|
|
112
|
+
*/
|
|
113
|
+
basePath () {
|
|
114
|
+
return this.config.path || DEFAULT_PATH
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
formatData ({ variables = {}, target }) {
|
|
118
|
+
const { expand, prefix } = this.config
|
|
119
|
+
const formatted = {}
|
|
120
|
+
|
|
121
|
+
for (const [key, value] of Object.entries(variables)) {
|
|
122
|
+
const expanded = expand && typeof value === 'string'
|
|
123
|
+
? interpolate(value, variables, new Set([key]))
|
|
124
|
+
: value
|
|
125
|
+
|
|
126
|
+
formatted[`${prefix}${key}`] = JSON.stringify(expanded)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// We have to stub any remaining `process.env`s due to Webpack 5 not polyfilling it anymore
|
|
130
|
+
// https://github.com/mrsteele/dotenv-webpack/issues/240#issuecomment-710231534
|
|
131
|
+
// However, if someone targets Node or Electron `process.env` still exists, and should therefore be kept
|
|
132
|
+
// https://webpack.js.org/configuration/target
|
|
133
|
+
if (this.shouldStub({ target })) {
|
|
134
|
+
// Results in `"MISSING_ENV_VAR".NAME` which is valid JS
|
|
135
|
+
formatted['process.env'] = '"MISSING_ENV_VAR"'
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return formatted
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
shouldStub ({ target }) {
|
|
142
|
+
const { prefix, ignoreStub } = this.config
|
|
143
|
+
|
|
144
|
+
// Only stub `process.env` itself, and never when told not to.
|
|
145
|
+
if (prefix !== DEFAULT_PREFIX || ignoreStub === true) {
|
|
146
|
+
return false
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (ignoreStub === false) {
|
|
150
|
+
return true
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// `target: false` (or anything that isn't a target name) gives us nothing
|
|
154
|
+
// to go on, so treat it like the browser and stub.
|
|
155
|
+
const targets = [target].flat().filter((t) => typeof t === 'string')
|
|
156
|
+
|
|
157
|
+
return targets.every((t) => !t.includes('node') && !isMainThreadElectron(t))
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Reads a file, warning (unless silent) and returning an empty string if
|
|
162
|
+
* it can't be read. `false` skips the file without a warning.
|
|
163
|
+
* @param {string | false} file - The file to load.
|
|
164
|
+
* @returns {string}
|
|
165
|
+
*/
|
|
166
|
+
loadFile (file) {
|
|
167
|
+
if (file === false) {
|
|
168
|
+
return ''
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
try {
|
|
172
|
+
return readFileSync(file, 'utf8')
|
|
173
|
+
} catch {
|
|
174
|
+
this.warn(`Failed to load ${file}.`)
|
|
175
|
+
return ''
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Prints a warning unless the `silent` option is set.
|
|
181
|
+
* @param {string} msg - The message.
|
|
182
|
+
*/
|
|
183
|
+
warn (msg) {
|
|
184
|
+
if (!this.config.silent) {
|
|
185
|
+
console.warn(msg)
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
module.exports = Dotenv
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { Compiler } from 'webpack'
|
|
2
|
+
|
|
3
|
+
declare namespace Dotenv {
|
|
4
|
+
interface DotenvOptions {
|
|
5
|
+
/**
|
|
6
|
+
* The env file to load. `.example` and `.defaults` are appended to this
|
|
7
|
+
* path when `safe` or `defaults` is `true`. `false` loads no env file.
|
|
8
|
+
* @default './.env'
|
|
9
|
+
*/
|
|
10
|
+
path?: string | false
|
|
11
|
+
/**
|
|
12
|
+
* Check that every variable in an example file is set. `true` uses
|
|
13
|
+
* `path` + `.example`; a string is the path to the example file.
|
|
14
|
+
* @default false
|
|
15
|
+
*/
|
|
16
|
+
safe?: boolean | string
|
|
17
|
+
/**
|
|
18
|
+
* In safe mode, treat empty values (`FOO=`) as set instead of missing.
|
|
19
|
+
* @default false
|
|
20
|
+
*/
|
|
21
|
+
allowEmptyValues?: boolean
|
|
22
|
+
/**
|
|
23
|
+
* Also expose the system environment (`process.env`). System variables
|
|
24
|
+
* win over values from the env files.
|
|
25
|
+
* @default false
|
|
26
|
+
*/
|
|
27
|
+
systemvars?: boolean
|
|
28
|
+
/**
|
|
29
|
+
* Don't print a warning when a file can't be read.
|
|
30
|
+
* @default false
|
|
31
|
+
*/
|
|
32
|
+
silent?: boolean
|
|
33
|
+
/**
|
|
34
|
+
* Expand `$NAME` and `${NAME}` references in values.
|
|
35
|
+
* @default false
|
|
36
|
+
*/
|
|
37
|
+
expand?: boolean
|
|
38
|
+
/**
|
|
39
|
+
* Fill in missing values from a defaults file. `true` uses `path` +
|
|
40
|
+
* `.defaults`; a string is the path to the defaults file.
|
|
41
|
+
* @default false
|
|
42
|
+
*/
|
|
43
|
+
defaults?: boolean | string
|
|
44
|
+
/**
|
|
45
|
+
* `true` never replaces remaining `process.env` references with
|
|
46
|
+
* `"MISSING_ENV_VAR"`, `false` always does. Unset, it depends on the
|
|
47
|
+
* webpack target.
|
|
48
|
+
*/
|
|
49
|
+
ignoreStub?: boolean
|
|
50
|
+
/**
|
|
51
|
+
* What variables are referenced through in your code.
|
|
52
|
+
* @default 'process.env.'
|
|
53
|
+
*/
|
|
54
|
+
prefix?: string
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
declare class Dotenv {
|
|
59
|
+
constructor (config?: Dotenv.DotenvOptions)
|
|
60
|
+
apply (compiler: Compiler): void
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export = Dotenv
|
package/browser.js
DELETED
package/dist/index.js
DELETED
|
@@ -1,268 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports["default"] = void 0;
|
|
7
|
-
|
|
8
|
-
var _dotenvDefaults = _interopRequireDefault(require("dotenv-defaults"));
|
|
9
|
-
|
|
10
|
-
var _fs = _interopRequireDefault(require("fs"));
|
|
11
|
-
|
|
12
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
13
|
-
|
|
14
|
-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
15
|
-
|
|
16
|
-
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
17
|
-
|
|
18
|
-
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
19
|
-
|
|
20
|
-
// Mostly taken from here: https://github.com/motdotla/dotenv-expand/blob/master/lib/main.js#L4
|
|
21
|
-
var interpolate = function interpolate(env, vars) {
|
|
22
|
-
var matches = env.match(/\$([a-zA-Z0-9_]+)|\${([a-zA-Z0-9_]+)}/g) || [];
|
|
23
|
-
matches.forEach(function (match) {
|
|
24
|
-
var key = match.replace(/\$|{|}/g, '');
|
|
25
|
-
var variable = vars[key] || '';
|
|
26
|
-
variable = interpolate(variable, vars);
|
|
27
|
-
env = env.replace(match, variable);
|
|
28
|
-
});
|
|
29
|
-
return env;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
var isMainThreadElectron = function isMainThreadElectron(target) {
|
|
33
|
-
return target.startsWith('electron') && target.endsWith('main');
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
var Dotenv = /*#__PURE__*/function () {
|
|
37
|
-
/**
|
|
38
|
-
* The dotenv-webpack plugin.
|
|
39
|
-
* @param {Object} options - The parameters.
|
|
40
|
-
* @param {String} [options.path=./.env] - The location of the environment variable.
|
|
41
|
-
* @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.
|
|
42
|
-
* @param {Boolean} [options.systemvars=false] - If true, load system environment variables.
|
|
43
|
-
* @param {Boolean} [options.silent=false] - If true, suppress warnings, if false, display warnings.
|
|
44
|
-
* @param {String} [options.prefix=process.env.] - The prefix, used to denote environment variables.
|
|
45
|
-
* @returns {webpack.DefinePlugin}
|
|
46
|
-
*/
|
|
47
|
-
function Dotenv() {
|
|
48
|
-
var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
49
|
-
|
|
50
|
-
_classCallCheck(this, Dotenv);
|
|
51
|
-
|
|
52
|
-
this.config = Object.assign({}, {
|
|
53
|
-
path: './.env',
|
|
54
|
-
prefix: 'process.env.'
|
|
55
|
-
}, config);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
_createClass(Dotenv, [{
|
|
59
|
-
key: "apply",
|
|
60
|
-
value: function apply(compiler) {
|
|
61
|
-
var _compiler$options$tar;
|
|
62
|
-
|
|
63
|
-
var variables = this.gatherVariables();
|
|
64
|
-
var target = (_compiler$options$tar = compiler.options.target) !== null && _compiler$options$tar !== void 0 ? _compiler$options$tar : 'web';
|
|
65
|
-
var version = compiler.webpack && compiler.webpack.version || '4';
|
|
66
|
-
var data = this.formatData({
|
|
67
|
-
variables: variables,
|
|
68
|
-
target: target,
|
|
69
|
-
version: version
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
var DefinePlugin = compiler.webpack && compiler.webpack.DefinePlugin || require('webpack').DefinePlugin;
|
|
73
|
-
|
|
74
|
-
new DefinePlugin(data).apply(compiler);
|
|
75
|
-
}
|
|
76
|
-
}, {
|
|
77
|
-
key: "gatherVariables",
|
|
78
|
-
value: function gatherVariables() {
|
|
79
|
-
var _this$config = this.config,
|
|
80
|
-
safe = _this$config.safe,
|
|
81
|
-
allowEmptyValues = _this$config.allowEmptyValues;
|
|
82
|
-
var vars = this.initializeVars();
|
|
83
|
-
|
|
84
|
-
var _this$getEnvs = this.getEnvs(),
|
|
85
|
-
env = _this$getEnvs.env,
|
|
86
|
-
blueprint = _this$getEnvs.blueprint;
|
|
87
|
-
|
|
88
|
-
Object.keys(blueprint).forEach(function (key) {
|
|
89
|
-
var value = Object.prototype.hasOwnProperty.call(vars, key) ? vars[key] : env[key];
|
|
90
|
-
var isMissing = typeof value === 'undefined' || value === null || !allowEmptyValues && value === '';
|
|
91
|
-
|
|
92
|
-
if (safe && isMissing) {
|
|
93
|
-
throw new Error("Missing environment variable: ".concat(key));
|
|
94
|
-
} else {
|
|
95
|
-
vars[key] = value;
|
|
96
|
-
}
|
|
97
|
-
}); // add the leftovers
|
|
98
|
-
|
|
99
|
-
if (safe) {
|
|
100
|
-
Object.keys(env).forEach(function (key) {
|
|
101
|
-
if (!Object.prototype.hasOwnProperty.call(vars, key)) {
|
|
102
|
-
vars[key] = env[key];
|
|
103
|
-
}
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
return vars;
|
|
108
|
-
}
|
|
109
|
-
}, {
|
|
110
|
-
key: "initializeVars",
|
|
111
|
-
value: function initializeVars() {
|
|
112
|
-
return this.config.systemvars ? Object.assign({}, process.env) : {};
|
|
113
|
-
}
|
|
114
|
-
}, {
|
|
115
|
-
key: "getEnvs",
|
|
116
|
-
value: function getEnvs() {
|
|
117
|
-
var _this$config2 = this.config,
|
|
118
|
-
path = _this$config2.path,
|
|
119
|
-
silent = _this$config2.silent,
|
|
120
|
-
safe = _this$config2.safe;
|
|
121
|
-
|
|
122
|
-
var env = _dotenvDefaults["default"].parse(this.loadFile({
|
|
123
|
-
file: path,
|
|
124
|
-
silent: silent
|
|
125
|
-
}), this.getDefaults());
|
|
126
|
-
|
|
127
|
-
var blueprint = env;
|
|
128
|
-
|
|
129
|
-
if (safe) {
|
|
130
|
-
var file = "".concat(path, ".example");
|
|
131
|
-
|
|
132
|
-
if (safe !== true) {
|
|
133
|
-
file = safe;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
blueprint = _dotenvDefaults["default"].parse(this.loadFile({
|
|
137
|
-
file: file,
|
|
138
|
-
silent: silent
|
|
139
|
-
}));
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
return {
|
|
143
|
-
env: env,
|
|
144
|
-
blueprint: blueprint
|
|
145
|
-
};
|
|
146
|
-
}
|
|
147
|
-
}, {
|
|
148
|
-
key: "getDefaults",
|
|
149
|
-
value: function getDefaults() {
|
|
150
|
-
var _this$config3 = this.config,
|
|
151
|
-
path = _this$config3.path,
|
|
152
|
-
silent = _this$config3.silent,
|
|
153
|
-
defaults = _this$config3.defaults;
|
|
154
|
-
|
|
155
|
-
if (defaults) {
|
|
156
|
-
return this.loadFile({
|
|
157
|
-
file: defaults === true ? "".concat(path, ".defaults") : defaults,
|
|
158
|
-
silent: silent
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
return '';
|
|
163
|
-
}
|
|
164
|
-
}, {
|
|
165
|
-
key: "formatData",
|
|
166
|
-
value: function formatData(_ref) {
|
|
167
|
-
var _ref$variables = _ref.variables,
|
|
168
|
-
variables = _ref$variables === void 0 ? {} : _ref$variables,
|
|
169
|
-
target = _ref.target,
|
|
170
|
-
version = _ref.version;
|
|
171
|
-
var _this$config4 = this.config,
|
|
172
|
-
expand = _this$config4.expand,
|
|
173
|
-
prefix = _this$config4.prefix;
|
|
174
|
-
var formatted = Object.keys(variables).reduce(function (obj, key) {
|
|
175
|
-
var v = variables[key];
|
|
176
|
-
var vKey = "".concat(prefix).concat(key);
|
|
177
|
-
var vValue;
|
|
178
|
-
|
|
179
|
-
if (expand) {
|
|
180
|
-
if (v.substring(0, 2) === '\\$') {
|
|
181
|
-
vValue = v.substring(1);
|
|
182
|
-
} else if (v.indexOf('\\$') > 0) {
|
|
183
|
-
vValue = v.replace(/\\\$/g, '$');
|
|
184
|
-
} else {
|
|
185
|
-
vValue = interpolate(v, variables);
|
|
186
|
-
}
|
|
187
|
-
} else {
|
|
188
|
-
vValue = v;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
obj[vKey] = JSON.stringify(vValue);
|
|
192
|
-
return obj;
|
|
193
|
-
}, {}); // We have to stub any remaining `process.env`s due to Webpack 5 not polyfilling it anymore
|
|
194
|
-
// https://github.com/mrsteele/dotenv-webpack/issues/240#issuecomment-710231534
|
|
195
|
-
// However, if someone targets Node or Electron `process.env` still exists, and should therefore be kept
|
|
196
|
-
// https://webpack.js.org/configuration/target
|
|
197
|
-
|
|
198
|
-
if (this.shouldStub({
|
|
199
|
-
target: target,
|
|
200
|
-
version: version
|
|
201
|
-
})) {
|
|
202
|
-
// Results in `"MISSING_ENV_VAR".NAME` which is valid JS
|
|
203
|
-
formatted['process.env'] = '"MISSING_ENV_VAR"';
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
return formatted;
|
|
207
|
-
}
|
|
208
|
-
}, {
|
|
209
|
-
key: "shouldStub",
|
|
210
|
-
value: function shouldStub(_ref2) {
|
|
211
|
-
var _this = this;
|
|
212
|
-
|
|
213
|
-
var targetInput = _ref2.target,
|
|
214
|
-
version = _ref2.version;
|
|
215
|
-
|
|
216
|
-
if (!version.startsWith('5')) {
|
|
217
|
-
return false;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
var targets = Array.isArray(targetInput) ? targetInput : [targetInput];
|
|
221
|
-
return targets.every(function (target) {
|
|
222
|
-
return (// If configured prefix is 'process.env'
|
|
223
|
-
_this.config.prefix === 'process.env.' && // If we're not configured to never stub
|
|
224
|
-
_this.config.ignoreStub !== true && ( // And
|
|
225
|
-
// We are configured to always stub
|
|
226
|
-
_this.config.ignoreStub === false || // Or if we should according to the target
|
|
227
|
-
!target.includes('node') && !isMainThreadElectron(target))
|
|
228
|
-
);
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
/**
|
|
232
|
-
* Load a file.
|
|
233
|
-
* @param {String} config.file - The file to load.
|
|
234
|
-
* @param {Boolean} config.silent - If true, suppress warnings, if false, display warnings.
|
|
235
|
-
* @returns {Object}
|
|
236
|
-
*/
|
|
237
|
-
|
|
238
|
-
}, {
|
|
239
|
-
key: "loadFile",
|
|
240
|
-
value: function loadFile(_ref3) {
|
|
241
|
-
var file = _ref3.file,
|
|
242
|
-
silent = _ref3.silent;
|
|
243
|
-
|
|
244
|
-
try {
|
|
245
|
-
return _fs["default"].readFileSync(file, 'utf8');
|
|
246
|
-
} catch (err) {
|
|
247
|
-
this.warn("Failed to load ".concat(file, "."), silent);
|
|
248
|
-
return {};
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
/**
|
|
252
|
-
* Displays a console message if 'silent' is falsey
|
|
253
|
-
* @param {String} msg - The message.
|
|
254
|
-
* @param {Boolean} silent - If true, display the message, if false, suppress the message.
|
|
255
|
-
*/
|
|
256
|
-
|
|
257
|
-
}, {
|
|
258
|
-
key: "warn",
|
|
259
|
-
value: function warn(msg, silent) {
|
|
260
|
-
!silent && console.warn(msg);
|
|
261
|
-
}
|
|
262
|
-
}]);
|
|
263
|
-
|
|
264
|
-
return Dotenv;
|
|
265
|
-
}();
|
|
266
|
-
|
|
267
|
-
var _default = Dotenv;
|
|
268
|
-
exports["default"] = _default;
|
package/index.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
let plugin
|
|
4
|
-
try {
|
|
5
|
-
plugin = require('./dist').default
|
|
6
|
-
} catch (err) {
|
|
7
|
-
if (err.code === 'MODULE_NOT_FOUND') {
|
|
8
|
-
require('babel-register')
|
|
9
|
-
plugin = require('./src').default
|
|
10
|
-
} else {
|
|
11
|
-
console.log(err)
|
|
12
|
-
process.exit(1)
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
module.exports = plugin
|