dotenv-webpack 9.0.0 → 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 -13
- package/package.json +36 -36
- package/src/index.js +116 -136
- package/types/index.d.ts +44 -3
package/README.md
CHANGED
|
@@ -10,28 +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
16
|
</div>
|
|
23
17
|
|
|
24
18
|
## Installation
|
|
25
19
|
|
|
26
20
|
Include the package locally in your repository.
|
|
27
21
|
|
|
28
|
-
|
|
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).
|
|
29
27
|
|
|
30
28
|
## Description
|
|
31
29
|
|
|
32
|
-
`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`.
|
|
33
31
|
|
|
34
|
-
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.
|
|
35
33
|
|
|
36
34
|
Interested in taking your environments to the next level? Check out the [Dotenv Organization](https://www.dotenv.org/get-started?r=3).
|
|
37
35
|
|
|
@@ -64,6 +62,17 @@ module.exports = {
|
|
|
64
62
|
};
|
|
65
63
|
```
|
|
66
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
|
+
|
|
67
76
|
### Use in your code
|
|
68
77
|
|
|
69
78
|
```javascript
|
|
@@ -94,7 +103,7 @@ Due to the fact that we use `webpack.DefinePlugin` under the hood, we cannot sup
|
|
|
94
103
|
|
|
95
104
|
## `process.env` stubbing / replacing
|
|
96
105
|
|
|
97
|
-
`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).
|
|
98
107
|
|
|
99
108
|
We automatically replace any remaining `process.env`s in these environments with `"MISSING_ENV_VAR"` to avoid these errors.
|
|
100
109
|
|
|
@@ -106,12 +115,12 @@ If you are running into issues where you or another package you use interfaces w
|
|
|
106
115
|
|
|
107
116
|
Use the following properties to configure your instance.
|
|
108
117
|
|
|
109
|
-
* **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).
|
|
110
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.
|
|
111
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).
|
|
112
121
|
* **systemvars** (`false`) - Set to true if you would rather load all system variables as well (useful for CI purposes).
|
|
113
122
|
* **silent** (`false`) - If true, all warnings will be suppressed.
|
|
114
|
-
* **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`.
|
|
115
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).
|
|
116
125
|
* **ignoreStub** (`false`) - Override the automatic check whether to stub `process.env`. [Read more here](#user-content-processenv-stubbing--replacing).
|
|
117
126
|
* **prefix** (`'process.env.'`) - The prefix to use before the name of your env variables.
|
|
@@ -127,8 +136,10 @@ module.exports = {
|
|
|
127
136
|
safe: true, // load '.env.example' to verify the '.env' variables are all set. Can also be a string to a different file.
|
|
128
137
|
allowEmptyValues: true, // allow empty variables (e.g. `FOO=`) (treat it as empty string, rather than missing)
|
|
129
138
|
systemvars: true, // load all the predefined 'process.env' variables which will trump anything local per dotenv specs.
|
|
130
|
-
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
|
|
131
141
|
defaults: false, // load '.env.defaults' as the default values if empty.
|
|
142
|
+
ignoreStub: true, // never replace leftover `process.env` references
|
|
132
143
|
prefix: 'import.meta.env.' // reference your env variables as 'import.meta.env.ENV_VAR'.
|
|
133
144
|
})
|
|
134
145
|
]
|
package/package.json
CHANGED
|
@@ -1,28 +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
|
-
"test": "jest",
|
|
13
|
-
"build": "node -e \"require('./src')\"",
|
|
14
|
-
"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"
|
|
15
28
|
},
|
|
16
29
|
"repository": {
|
|
17
30
|
"type": "git",
|
|
18
|
-
"url": "https://github.com/mrsteele/dotenv-webpack.git"
|
|
19
|
-
},
|
|
20
|
-
"jest": {
|
|
21
|
-
"coverageDirectory": "./coverage/",
|
|
22
|
-
"collectCoverage": true,
|
|
23
|
-
"watchPathIgnorePatterns": [
|
|
24
|
-
"output/.*?"
|
|
25
|
-
]
|
|
31
|
+
"url": "git+https://github.com/mrsteele/dotenv-webpack.git"
|
|
26
32
|
},
|
|
27
33
|
"keywords": [
|
|
28
34
|
"dotenv",
|
|
@@ -42,28 +48,22 @@
|
|
|
42
48
|
"url": "https://github.com/mrsteele/dotenv-webpack/issues"
|
|
43
49
|
},
|
|
44
50
|
"homepage": "https://github.com/mrsteele/dotenv-webpack#readme",
|
|
45
|
-
"
|
|
46
|
-
"
|
|
51
|
+
"publishConfig": {
|
|
52
|
+
"access": "public",
|
|
53
|
+
"provenance": true
|
|
47
54
|
},
|
|
48
55
|
"peerDependencies": {
|
|
49
|
-
"webpack": "^
|
|
56
|
+
"webpack": "^5"
|
|
50
57
|
},
|
|
51
58
|
"dependencies": {
|
|
52
|
-
"dotenv-defaults": "^
|
|
59
|
+
"dotenv-defaults": "^7.0.0"
|
|
53
60
|
},
|
|
54
61
|
"devDependencies": {
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
|
|
62
|
-
},
|
|
63
|
-
"files": [
|
|
64
|
-
"src",
|
|
65
|
-
"types/index.d.ts"
|
|
66
|
-
],
|
|
67
|
-
"browser": "src/index.js",
|
|
68
|
-
"types": "types/index.d.ts"
|
|
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"
|
|
68
|
+
}
|
|
69
69
|
}
|
package/src/index.js
CHANGED
|
@@ -1,19 +1,38 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
+
}
|
|
14
27
|
|
|
15
|
-
|
|
16
|
-
|
|
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
|
+
})
|
|
17
36
|
|
|
18
37
|
const isMainThreadElectron = (target) =>
|
|
19
38
|
target.startsWith('electron') && target.endsWith('main')
|
|
@@ -21,137 +40,97 @@ const isMainThreadElectron = (target) =>
|
|
|
21
40
|
class Dotenv {
|
|
22
41
|
/**
|
|
23
42
|
* The dotenv-webpack plugin.
|
|
24
|
-
* @param {
|
|
25
|
-
* @param {String} [options.path=./.env] - The location of the environment variable.
|
|
26
|
-
* @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.
|
|
27
|
-
* @param {Boolean} [options.systemvars=false] - If true, load system environment variables.
|
|
28
|
-
* @param {Boolean} [options.silent=false] - If true, suppress warnings, if false, display warnings.
|
|
29
|
-
* @param {String} [options.prefix=process.env.] - The prefix, used to denote environment variables.
|
|
30
|
-
* @returns {webpack.DefinePlugin}
|
|
43
|
+
* @param {import('../types').DotenvOptions} [config] - The options.
|
|
31
44
|
*/
|
|
32
45
|
constructor (config = {}) {
|
|
33
|
-
this.config =
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
46
|
+
this.config = {
|
|
47
|
+
...config,
|
|
48
|
+
path: config.path ?? DEFAULT_PATH,
|
|
49
|
+
prefix: config.prefix ?? DEFAULT_PREFIX
|
|
50
|
+
}
|
|
37
51
|
}
|
|
38
52
|
|
|
53
|
+
/**
|
|
54
|
+
* @param {import('webpack').Compiler} compiler
|
|
55
|
+
*/
|
|
39
56
|
apply (compiler) {
|
|
40
|
-
const
|
|
41
|
-
const target = compiler.options.target ?? 'web'
|
|
42
|
-
const version = (compiler.webpack && compiler.webpack.version) || '4'
|
|
57
|
+
const { DefinePlugin } = compiler.webpack
|
|
43
58
|
const data = this.formatData({
|
|
44
|
-
variables,
|
|
45
|
-
target
|
|
46
|
-
version
|
|
59
|
+
variables: this.gatherVariables(),
|
|
60
|
+
target: compiler.options.target ?? 'web'
|
|
47
61
|
})
|
|
48
|
-
const DefinePlugin = (compiler.webpack && compiler.webpack.DefinePlugin) || require('webpack').DefinePlugin
|
|
49
62
|
|
|
50
63
|
new DefinePlugin(data).apply(compiler)
|
|
51
64
|
}
|
|
52
65
|
|
|
53
66
|
gatherVariables () {
|
|
54
|
-
const { safe, allowEmptyValues } = this.config
|
|
55
|
-
const vars = this.initializeVars()
|
|
56
|
-
|
|
67
|
+
const { safe, allowEmptyValues, systemvars } = this.config
|
|
57
68
|
const { env, blueprint } = this.getEnvs()
|
|
58
69
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const isMissing = typeof value === 'undefined' || value === null ||
|
|
63
|
-
(!allowEmptyValues && value === '')
|
|
64
|
-
|
|
65
|
-
if (safe && isMissing) {
|
|
66
|
-
throw new Error(`Missing environment variable: ${key}`)
|
|
67
|
-
} else {
|
|
68
|
-
vars[key] = value
|
|
69
|
-
}
|
|
70
|
-
})
|
|
70
|
+
// System variables win over anything read from a file.
|
|
71
|
+
const vars = systemvars ? { ...env, ...process.env } : { ...env }
|
|
71
72
|
|
|
72
|
-
// add the leftovers
|
|
73
73
|
if (safe) {
|
|
74
|
-
Object.keys(
|
|
75
|
-
|
|
76
|
-
|
|
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}`)
|
|
77
78
|
}
|
|
78
|
-
}
|
|
79
|
+
}
|
|
79
80
|
}
|
|
80
81
|
|
|
81
82
|
return vars
|
|
82
83
|
}
|
|
83
84
|
|
|
84
|
-
initializeVars () {
|
|
85
|
-
return (this.config.systemvars) ? Object.assign({}, process.env) : {}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
85
|
getEnvs () {
|
|
89
|
-
const {
|
|
90
|
-
|
|
91
|
-
const env = dotenv.parse(this.loadFile({
|
|
92
|
-
file: path,
|
|
93
|
-
silent
|
|
94
|
-
}), this.getDefaults())
|
|
86
|
+
const { safe } = this.config
|
|
87
|
+
const env = parse(this.loadFile(this.config.path), this.getDefaults())
|
|
95
88
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
let file = `${path}.example`
|
|
99
|
-
if (safe !== true) {
|
|
100
|
-
file = safe
|
|
101
|
-
}
|
|
102
|
-
blueprint = dotenv.parse(this.loadFile({
|
|
103
|
-
file,
|
|
104
|
-
silent
|
|
105
|
-
}))
|
|
89
|
+
if (!safe) {
|
|
90
|
+
return { env, blueprint: env }
|
|
106
91
|
}
|
|
107
92
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
blueprint
|
|
111
|
-
}
|
|
93
|
+
const file = safe === true ? `${this.basePath()}.example` : safe
|
|
94
|
+
return { env, blueprint: parse(this.loadFile(file)) }
|
|
112
95
|
}
|
|
113
96
|
|
|
114
97
|
getDefaults () {
|
|
115
|
-
const {
|
|
98
|
+
const { defaults } = this.config
|
|
116
99
|
|
|
117
|
-
if (defaults) {
|
|
118
|
-
return
|
|
119
|
-
file: defaults === true ? `${path}.defaults` : defaults,
|
|
120
|
-
silent
|
|
121
|
-
})
|
|
100
|
+
if (!defaults) {
|
|
101
|
+
return ''
|
|
122
102
|
}
|
|
123
103
|
|
|
124
|
-
return
|
|
104
|
+
return this.loadFile(defaults === true ? `${this.basePath()}.defaults` : defaults)
|
|
125
105
|
}
|
|
126
106
|
|
|
127
|
-
|
|
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 }) {
|
|
128
118
|
const { expand, prefix } = this.config
|
|
129
|
-
const formatted =
|
|
130
|
-
const v = variables[key]
|
|
131
|
-
const vKey = `${prefix}${key}`
|
|
132
|
-
let vValue
|
|
133
|
-
if (expand) {
|
|
134
|
-
if (v.substring(0, 2) === '\\$') {
|
|
135
|
-
vValue = v.substring(1)
|
|
136
|
-
} else if (v.indexOf('\\$') > 0) {
|
|
137
|
-
vValue = v.replace(/\\\$/g, '$')
|
|
138
|
-
} else {
|
|
139
|
-
vValue = interpolate(v, variables)
|
|
140
|
-
}
|
|
141
|
-
} else {
|
|
142
|
-
vValue = v
|
|
143
|
-
}
|
|
119
|
+
const formatted = {}
|
|
144
120
|
|
|
145
|
-
|
|
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
|
|
146
125
|
|
|
147
|
-
|
|
148
|
-
}
|
|
126
|
+
formatted[`${prefix}${key}`] = JSON.stringify(expanded)
|
|
127
|
+
}
|
|
149
128
|
|
|
150
129
|
// We have to stub any remaining `process.env`s due to Webpack 5 not polyfilling it anymore
|
|
151
130
|
// https://github.com/mrsteele/dotenv-webpack/issues/240#issuecomment-710231534
|
|
152
131
|
// However, if someone targets Node or Electron `process.env` still exists, and should therefore be kept
|
|
153
132
|
// https://webpack.js.org/configuration/target
|
|
154
|
-
if (this.shouldStub({ target
|
|
133
|
+
if (this.shouldStub({ target })) {
|
|
155
134
|
// Results in `"MISSING_ENV_VAR".NAME` which is valid JS
|
|
156
135
|
formatted['process.env'] = '"MISSING_ENV_VAR"'
|
|
157
136
|
}
|
|
@@ -159,51 +138,52 @@ class Dotenv {
|
|
|
159
138
|
return formatted
|
|
160
139
|
}
|
|
161
140
|
|
|
162
|
-
shouldStub ({ target
|
|
163
|
-
|
|
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) {
|
|
164
146
|
return false
|
|
165
147
|
}
|
|
166
148
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
(
|
|
177
|
-
// We are configured to always stub
|
|
178
|
-
this.config.ignoreStub === false ||
|
|
179
|
-
// Or if we should according to the target
|
|
180
|
-
(!target.includes('node') && !isMainThreadElectron(target))
|
|
181
|
-
)
|
|
182
|
-
)
|
|
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))
|
|
183
158
|
}
|
|
184
159
|
|
|
185
160
|
/**
|
|
186
|
-
*
|
|
187
|
-
*
|
|
188
|
-
* @param {
|
|
189
|
-
* @returns {
|
|
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}
|
|
190
165
|
*/
|
|
191
|
-
loadFile (
|
|
166
|
+
loadFile (file) {
|
|
167
|
+
if (file === false) {
|
|
168
|
+
return ''
|
|
169
|
+
}
|
|
170
|
+
|
|
192
171
|
try {
|
|
193
|
-
return
|
|
194
|
-
} catch
|
|
195
|
-
this.warn(`Failed to load ${file}
|
|
196
|
-
return
|
|
172
|
+
return readFileSync(file, 'utf8')
|
|
173
|
+
} catch {
|
|
174
|
+
this.warn(`Failed to load ${file}.`)
|
|
175
|
+
return ''
|
|
197
176
|
}
|
|
198
177
|
}
|
|
199
178
|
|
|
200
179
|
/**
|
|
201
|
-
*
|
|
202
|
-
* @param {
|
|
203
|
-
* @param {Boolean} silent - If true, display the message, if false, suppress the message.
|
|
180
|
+
* Prints a warning unless the `silent` option is set.
|
|
181
|
+
* @param {string} msg - The message.
|
|
204
182
|
*/
|
|
205
|
-
warn (msg
|
|
206
|
-
!silent
|
|
183
|
+
warn (msg) {
|
|
184
|
+
if (!this.config.silent) {
|
|
185
|
+
console.warn(msg)
|
|
186
|
+
}
|
|
207
187
|
}
|
|
208
188
|
}
|
|
209
189
|
|
package/types/index.d.ts
CHANGED
|
@@ -1,22 +1,63 @@
|
|
|
1
1
|
import type { Compiler } from 'webpack'
|
|
2
2
|
|
|
3
|
-
declare namespace
|
|
3
|
+
declare namespace Dotenv {
|
|
4
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
|
+
*/
|
|
5
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
|
+
*/
|
|
6
16
|
safe?: boolean | string
|
|
17
|
+
/**
|
|
18
|
+
* In safe mode, treat empty values (`FOO=`) as set instead of missing.
|
|
19
|
+
* @default false
|
|
20
|
+
*/
|
|
7
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
|
+
*/
|
|
8
27
|
systemvars?: boolean
|
|
28
|
+
/**
|
|
29
|
+
* Don't print a warning when a file can't be read.
|
|
30
|
+
* @default false
|
|
31
|
+
*/
|
|
9
32
|
silent?: boolean
|
|
33
|
+
/**
|
|
34
|
+
* Expand `$NAME` and `${NAME}` references in values.
|
|
35
|
+
* @default false
|
|
36
|
+
*/
|
|
10
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
|
+
*/
|
|
11
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
|
+
*/
|
|
12
49
|
ignoreStub?: boolean
|
|
50
|
+
/**
|
|
51
|
+
* What variables are referenced through in your code.
|
|
52
|
+
* @default 'process.env.'
|
|
53
|
+
*/
|
|
13
54
|
prefix?: string
|
|
14
55
|
}
|
|
15
56
|
}
|
|
16
57
|
|
|
17
58
|
declare class Dotenv {
|
|
18
|
-
constructor(config?:
|
|
19
|
-
apply(compiler: Compiler): void
|
|
59
|
+
constructor (config?: Dotenv.DotenvOptions)
|
|
60
|
+
apply (compiler: Compiler): void
|
|
20
61
|
}
|
|
21
62
|
|
|
22
63
|
export = Dotenv
|