dotenv-webpack 1.5.6 → 1.8.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 +11 -9
- package/browser.js +3 -0
- package/dist/index.js +161 -54
- package/package.json +11 -10
package/README.md
CHANGED
|
@@ -7,16 +7,13 @@ A secure webpack plugin that supports dotenv and other environment variables and
|
|
|
7
7
|
<a href="https://travis-ci.org/mrsteele/dotenv-webpack" target="_blank">
|
|
8
8
|
<img alt="Travis" src="https://travis-ci.org/mrsteele/dotenv-webpack.svg?branch=master" />
|
|
9
9
|
</a>
|
|
10
|
-
<a href="https://codecov.io/gh/mrsteele/dotenv-webpack" target="_blank">
|
|
11
|
-
<img alt="codecov" src="https://codecov.io/gh/mrsteele/dotenv-webpack/branch/master/graph/badge.svg" />
|
|
12
|
-
</a>
|
|
13
10
|
<a href="https://david-dm.org/mrsteele/dotenv-webpack" target="_blank">
|
|
14
11
|
<img alt="Dependency Status" src="https://david-dm.org/mrsteele/dotenv-webpack.svg" />
|
|
15
12
|
</a>
|
|
16
13
|
<a href="https://david-dm.org/mrsteele/dotenv-webpack?type=dev" target="_blank">
|
|
17
14
|
<img alt="devDependency Status" src="https://david-dm.org/mrsteele/dotenv-webpack/dev-status.svg" />
|
|
18
15
|
</a>
|
|
19
|
-
|
|
16
|
+
|
|
20
17
|
<h1>
|
|
21
18
|
<img width="30" heigth="30" src="https://raw.githubusercontent.com/motdotla/dotenv/master/dotenv.png" alt="dotenv" />
|
|
22
19
|
<img width="30" heigth="30" src="https://webpack.js.org/assets/icon-square-big.svg" alt="webpack">
|
|
@@ -28,7 +25,7 @@ A secure webpack plugin that supports dotenv and other environment variables and
|
|
|
28
25
|
|
|
29
26
|
Include the package locally in your repository.
|
|
30
27
|
|
|
31
|
-
`npm install dotenv-webpack --save`
|
|
28
|
+
`npm install dotenv-webpack --save-dev`
|
|
32
29
|
|
|
33
30
|
### Description
|
|
34
31
|
|
|
@@ -83,7 +80,7 @@ Note: the `.env` values for `DB_PASS` and `S3_API` are **NOT** present in our b
|
|
|
83
80
|
|
|
84
81
|
### How Secure?
|
|
85
82
|
|
|
86
|
-
|
|
83
|
+
By allowing you to define exactly where you are loading environment variables from and bundling only variables in your project that are explicitly referenced in your code, you can be sure that only what you need is included and you do not accidentally leak anything sensitive.
|
|
87
84
|
|
|
88
85
|
###### Recommended
|
|
89
86
|
|
|
@@ -94,9 +91,12 @@ Add `.env` to your `.gitignore` file
|
|
|
94
91
|
Use the following properties to configure your instance.
|
|
95
92
|
|
|
96
93
|
* **path** (`'./.env'`) - The path to your environment variables.
|
|
97
|
-
* **safe** (`false`) - If
|
|
94
|
+
* **safe** (`false`) - If true, load '.env.example' to verify the '.env' variables are all set. Can also be a string to a different file.
|
|
95
|
+
* **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).
|
|
98
96
|
* **systemvars** (`false`) - Set to true if you would rather load all system variables as well (useful for CI purposes).
|
|
99
|
-
* **silent** (`false`) - If true, all warnings will be
|
|
97
|
+
* **silent** (`false`) - If true, all warnings will be suppressed.
|
|
98
|
+
* **expand** (`false`) - Allows your variables to be "expanded" for reusability within your `.env` file.
|
|
99
|
+
* **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 https://www.npmjs.com/package/dotenv-defaults.
|
|
100
100
|
|
|
101
101
|
The following example shows how to set any/all arguments.
|
|
102
102
|
|
|
@@ -107,8 +107,10 @@ module.exports = {
|
|
|
107
107
|
new Dotenv({
|
|
108
108
|
path: './some.other.env', // load this now instead of the ones in '.env'
|
|
109
109
|
safe: true, // load '.env.example' to verify the '.env' variables are all set. Can also be a string to a different file.
|
|
110
|
+
allowEmptyValues: true, // allow empty variables (e.g. `FOO=`) (treat it as empty string, rather than missing)
|
|
110
111
|
systemvars: true, // load all the predefined 'process.env' variables which will trump anything local per dotenv specs.
|
|
111
|
-
silent: true // hide any errors
|
|
112
|
+
silent: true, // hide any errors
|
|
113
|
+
defaults: false // load '.env.defaults' as the default values if empty.
|
|
112
114
|
})
|
|
113
115
|
]
|
|
114
116
|
...
|
package/browser.js
ADDED
package/dist/index.js
CHANGED
|
@@ -4,11 +4,13 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
|
|
7
|
+
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
|
|
8
|
+
|
|
7
9
|
var _createClass = function () { 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
|
8
10
|
|
|
9
|
-
var
|
|
11
|
+
var _dotenvDefaults = require('dotenv-defaults');
|
|
10
12
|
|
|
11
|
-
var
|
|
13
|
+
var _dotenvDefaults2 = _interopRequireDefault(_dotenvDefaults);
|
|
12
14
|
|
|
13
15
|
var _fs = require('fs');
|
|
14
16
|
|
|
@@ -20,83 +22,188 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
20
22
|
|
|
21
23
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
22
24
|
|
|
25
|
+
// Mostly taken from here: https://github.com/motdotla/dotenv-expand/blob/master/lib/main.js#L4
|
|
26
|
+
var interpolate = function interpolate(env, vars) {
|
|
27
|
+
var matches = env.match(/\$([a-zA-Z0-9_]+)|\${([a-zA-Z0-9_]+)}/g) || [];
|
|
28
|
+
|
|
29
|
+
matches.forEach(function (match) {
|
|
30
|
+
var key = match.replace(/\$|{|}/g, '');
|
|
31
|
+
var variable = vars[key] || '';
|
|
32
|
+
variable = interpolate(variable, vars);
|
|
33
|
+
env = env.replace(match, variable);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
return env;
|
|
37
|
+
};
|
|
38
|
+
|
|
23
39
|
var Dotenv = function () {
|
|
24
40
|
/**
|
|
25
41
|
* The dotenv-webpack plugin.
|
|
26
42
|
* @param {Object} options - The parameters.
|
|
27
43
|
* @param {String} [options.path=./.env] - The location of the environment variable.
|
|
28
|
-
* @param {
|
|
29
|
-
* @param {
|
|
30
|
-
* @param {
|
|
44
|
+
* @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.
|
|
45
|
+
* @param {Boolean} [options.systemvars=false] - If true, load system environment variables.
|
|
46
|
+
* @param {Boolean} [options.silent=false] - If true, suppress warnings, if false, display warnings.
|
|
31
47
|
* @returns {webpack.DefinePlugin}
|
|
32
48
|
*/
|
|
33
49
|
function Dotenv() {
|
|
34
|
-
var
|
|
35
|
-
_ref$path = _ref.path,
|
|
36
|
-
path = _ref$path === undefined ? './.env' : _ref$path,
|
|
37
|
-
safe = _ref.safe,
|
|
38
|
-
systemvars = _ref.systemvars,
|
|
39
|
-
silent = _ref.silent,
|
|
40
|
-
sample = _ref.sample;
|
|
50
|
+
var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
41
51
|
|
|
42
52
|
_classCallCheck(this, Dotenv);
|
|
43
53
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
54
|
+
this.config = _extends({}, {
|
|
55
|
+
path: './.env'
|
|
56
|
+
}, config);
|
|
57
|
+
|
|
58
|
+
this.checkDeprecation();
|
|
59
|
+
|
|
60
|
+
return new _webpack.DefinePlugin(this.formatData(this.gatherVariables()));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
_createClass(Dotenv, [{
|
|
64
|
+
key: 'checkDeprecation',
|
|
65
|
+
value: function checkDeprecation() {
|
|
66
|
+
var _config = this.config,
|
|
67
|
+
sample = _config.sample,
|
|
68
|
+
safe = _config.safe,
|
|
69
|
+
silent = _config.silent;
|
|
70
|
+
// Catch older packages, but hold their hand (just for a bit)
|
|
71
|
+
|
|
72
|
+
if (sample) {
|
|
73
|
+
if (safe) {
|
|
74
|
+
this.config.safe = sample;
|
|
75
|
+
}
|
|
76
|
+
this.warn('dotenv-webpack: "options.sample" is a deprecated property. Please update your configuration to use "options.safe" instead.', silent);
|
|
48
77
|
}
|
|
49
|
-
this.warn('dotenv-webpack: "options.sample" is a deprecated property. Please update your configuration to use "options.safe" instead.', silent);
|
|
50
78
|
}
|
|
79
|
+
}, {
|
|
80
|
+
key: 'gatherVariables',
|
|
81
|
+
value: function gatherVariables() {
|
|
82
|
+
var _config2 = this.config,
|
|
83
|
+
safe = _config2.safe,
|
|
84
|
+
allowEmptyValues = _config2.allowEmptyValues;
|
|
51
85
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
86
|
+
var vars = this.initializeVars();
|
|
87
|
+
|
|
88
|
+
var _getEnvs = this.getEnvs(),
|
|
89
|
+
env = _getEnvs.env,
|
|
90
|
+
blueprint = _getEnvs.blueprint;
|
|
91
|
+
|
|
92
|
+
Object.keys(blueprint).map(function (key) {
|
|
93
|
+
var value = Object.prototype.hasOwnProperty.call(vars, key) ? vars[key] : env[key];
|
|
58
94
|
|
|
59
|
-
|
|
95
|
+
var isMissing = typeof value === 'undefined' || value === null || !allowEmptyValues && value === '';
|
|
60
96
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
97
|
+
if (safe && isMissing) {
|
|
98
|
+
throw new Error('Missing environment variable: ' + key);
|
|
99
|
+
} else {
|
|
100
|
+
vars[key] = value;
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// add the leftovers
|
|
105
|
+
if (safe) {
|
|
106
|
+
_extends(vars, env);
|
|
66
107
|
}
|
|
67
|
-
|
|
108
|
+
|
|
109
|
+
return vars;
|
|
110
|
+
}
|
|
111
|
+
}, {
|
|
112
|
+
key: 'initializeVars',
|
|
113
|
+
value: function initializeVars() {
|
|
114
|
+
return this.config.systemvars ? _extends({}, process.env) : {};
|
|
68
115
|
}
|
|
116
|
+
}, {
|
|
117
|
+
key: 'getEnvs',
|
|
118
|
+
value: function getEnvs() {
|
|
119
|
+
var _config3 = this.config,
|
|
120
|
+
path = _config3.path,
|
|
121
|
+
silent = _config3.silent,
|
|
122
|
+
safe = _config3.safe;
|
|
69
123
|
|
|
70
|
-
Object.keys(blueprint).map(function (key) {
|
|
71
|
-
var value = vars.hasOwnProperty(key) ? vars[key] : env[key];
|
|
72
|
-
if (!value && safe) {
|
|
73
|
-
throw new Error('Missing environment variable: ' + key);
|
|
74
|
-
} else {
|
|
75
|
-
vars[key] = value;
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
124
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
125
|
+
var env = _dotenvDefaults2.default.parse(this.loadFile({
|
|
126
|
+
file: path,
|
|
127
|
+
silent: silent
|
|
128
|
+
}), this.getDefaults());
|
|
83
129
|
|
|
84
|
-
|
|
85
|
-
|
|
130
|
+
var blueprint = env;
|
|
131
|
+
if (safe) {
|
|
132
|
+
var file = './.env.example';
|
|
133
|
+
if (safe !== true) {
|
|
134
|
+
file = safe;
|
|
135
|
+
}
|
|
136
|
+
blueprint = _dotenvDefaults2.default.parse(this.loadFile({
|
|
137
|
+
file: file,
|
|
138
|
+
silent: silent
|
|
139
|
+
}));
|
|
140
|
+
}
|
|
86
141
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
142
|
+
return {
|
|
143
|
+
env: env,
|
|
144
|
+
blueprint: blueprint
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
}, {
|
|
148
|
+
key: 'getDefaults',
|
|
149
|
+
value: function getDefaults() {
|
|
150
|
+
var _config4 = this.config,
|
|
151
|
+
silent = _config4.silent,
|
|
152
|
+
defaults = _config4.defaults;
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
if (defaults) {
|
|
156
|
+
return this.loadFile({
|
|
157
|
+
file: defaults === true ? './.env.defaults' : defaults,
|
|
158
|
+
silent: silent
|
|
159
|
+
});
|
|
160
|
+
}
|
|
93
161
|
|
|
162
|
+
return '';
|
|
163
|
+
}
|
|
164
|
+
}, {
|
|
165
|
+
key: 'formatData',
|
|
166
|
+
value: function formatData() {
|
|
167
|
+
var vars = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
168
|
+
var expand = this.config.expand;
|
|
169
|
+
|
|
170
|
+
return Object.keys(vars).reduce(function (obj, key) {
|
|
171
|
+
var v = vars[key];
|
|
172
|
+
var vKey = 'process.env.' + key;
|
|
173
|
+
var vValue = void 0;
|
|
174
|
+
if (expand) {
|
|
175
|
+
if (v.substring(0, 2) === '\\$') {
|
|
176
|
+
vValue = v.substring(1);
|
|
177
|
+
} else if (v.indexOf('\\$') > 0) {
|
|
178
|
+
vValue = v.replace(/\\\$/g, '$');
|
|
179
|
+
} else {
|
|
180
|
+
vValue = interpolate(v, vars);
|
|
181
|
+
}
|
|
182
|
+
} else {
|
|
183
|
+
vValue = v;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
obj[vKey] = JSON.stringify(vValue);
|
|
187
|
+
|
|
188
|
+
return obj;
|
|
189
|
+
}, {});
|
|
190
|
+
}
|
|
94
191
|
|
|
95
|
-
|
|
192
|
+
/**
|
|
193
|
+
* Load a file.
|
|
194
|
+
* @param {String} config.file - The file to load.
|
|
195
|
+
* @param {Boolean} config.silent - If true, suppress warnings, if false, display warnings.
|
|
196
|
+
* @returns {Object}
|
|
197
|
+
*/
|
|
198
|
+
|
|
199
|
+
}, {
|
|
96
200
|
key: 'loadFile',
|
|
97
|
-
value: function loadFile(
|
|
201
|
+
value: function loadFile(_ref) {
|
|
202
|
+
var file = _ref.file,
|
|
203
|
+
silent = _ref.silent;
|
|
204
|
+
|
|
98
205
|
try {
|
|
99
|
-
return
|
|
206
|
+
return _fs2.default.readFileSync(file, 'utf8');
|
|
100
207
|
} catch (err) {
|
|
101
208
|
this.warn('Failed to load ' + file + '.', silent);
|
|
102
209
|
return {};
|
|
@@ -106,7 +213,7 @@ var Dotenv = function () {
|
|
|
106
213
|
/**
|
|
107
214
|
* Displays a console message if 'silent' is falsey
|
|
108
215
|
* @param {String} msg - The message.
|
|
109
|
-
* @param {
|
|
216
|
+
* @param {Boolean} silent - If true, display the message, if false, suppress the message.
|
|
110
217
|
*/
|
|
111
218
|
|
|
112
219
|
}, {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dotenv-webpack",
|
|
3
3
|
"description": "A simple webpack plugin to support dotenv.",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.8.0",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"precommit": "npm run lint",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"predoc": "rimraf docs",
|
|
11
11
|
"doc": "esdoc -c config/doc.json",
|
|
12
12
|
"pretest": "npm run build",
|
|
13
|
-
"test": "nyc _mocha --
|
|
13
|
+
"test": "nyc _mocha --require babel-core/register",
|
|
14
14
|
"posttest": "nyc report --reporter=lcov",
|
|
15
15
|
"prebuild": "rimraf dist",
|
|
16
16
|
"build": "babel --copy-files --out-dir dist src",
|
|
@@ -42,22 +42,23 @@
|
|
|
42
42
|
"webpack": "^1 || ^2 || ^3 || ^4"
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"dotenv": "^
|
|
45
|
+
"dotenv-defaults": "^1.0.2"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
48
|
"babel-cli": "^6.26.0",
|
|
49
|
-
"babel-eslint": "^
|
|
49
|
+
"babel-eslint": "^10.0.1",
|
|
50
50
|
"babel-plugin-transform-object-assign": "^6.22.0",
|
|
51
51
|
"babel-preset-env": "^1.6.1",
|
|
52
52
|
"babel-register": "^6.26.0",
|
|
53
53
|
"chai": "^4.1.2",
|
|
54
54
|
"esdoc": "^1.0.4",
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"
|
|
55
|
+
"esdoc-standard-plugin": "^1.0.0",
|
|
56
|
+
"husky": "^3.0.0",
|
|
57
|
+
"mocha": "^6.0.0",
|
|
58
|
+
"nyc": "^14.0.0",
|
|
59
|
+
"rimraf": "^3.0.0",
|
|
60
|
+
"sinon": "^7.0.0",
|
|
61
|
+
"standard": "^14.0.0",
|
|
61
62
|
"webpack": "^4.6.0"
|
|
62
63
|
},
|
|
63
64
|
"files": [
|