dotenv-webpack 1.6.0 → 1.7.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 +5 -3
- package/dist/index.js +135 -65
- package/package.json +2 -3
package/README.md
CHANGED
|
@@ -80,7 +80,7 @@ Note: the `.env` values for `DB_PASS` and `S3_API` are **NOT** present in our b
|
|
|
80
80
|
|
|
81
81
|
### How Secure?
|
|
82
82
|
|
|
83
|
-
By allowing you to define exactly where you are loading environment variables from
|
|
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.
|
|
84
84
|
|
|
85
85
|
###### Recommended
|
|
86
86
|
|
|
@@ -93,8 +93,9 @@ Use the following properties to configure your instance.
|
|
|
93
93
|
* **path** (`'./.env'`) - The path to your environment variables.
|
|
94
94
|
* **safe** (`false`) - If false ignore safe-mode, if true load `'./.env.example'`, if a string load that file as the sample.
|
|
95
95
|
* **systemvars** (`false`) - Set to true if you would rather load all system variables as well (useful for CI purposes).
|
|
96
|
-
* **silent** (`false`) - If true, all warnings will be
|
|
96
|
+
* **silent** (`false`) - If true, all warnings will be suppressed.
|
|
97
97
|
* **expand** (`false`) - Allows your variables to be "expanded" for reusability within your `.env` file.
|
|
98
|
+
* **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.
|
|
98
99
|
|
|
99
100
|
The following example shows how to set any/all arguments.
|
|
100
101
|
|
|
@@ -106,7 +107,8 @@ module.exports = {
|
|
|
106
107
|
path: './some.other.env', // load this now instead of the ones in '.env'
|
|
107
108
|
safe: true, // load '.env.example' to verify the '.env' variables are all set. Can also be a string to a different file.
|
|
108
109
|
systemvars: true, // load all the predefined 'process.env' variables which will trump anything local per dotenv specs.
|
|
109
|
-
silent: true // hide any errors
|
|
110
|
+
silent: true, // hide any errors
|
|
111
|
+
defaults: false // load '.env.defaults' as the default values if empty.
|
|
110
112
|
})
|
|
111
113
|
]
|
|
112
114
|
...
|
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
|
|
|
@@ -45,90 +47,158 @@ var Dotenv = function () {
|
|
|
45
47
|
* @returns {webpack.DefinePlugin}
|
|
46
48
|
*/
|
|
47
49
|
function Dotenv() {
|
|
48
|
-
var
|
|
49
|
-
_ref$path = _ref.path,
|
|
50
|
-
path = _ref$path === undefined ? './.env' : _ref$path,
|
|
51
|
-
safe = _ref.safe,
|
|
52
|
-
systemvars = _ref.systemvars,
|
|
53
|
-
silent = _ref.silent,
|
|
54
|
-
sample = _ref.sample,
|
|
55
|
-
_ref$expand = _ref.expand,
|
|
56
|
-
expand = _ref$expand === undefined ? false : _ref$expand;
|
|
50
|
+
var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
57
51
|
|
|
58
52
|
_classCallCheck(this, Dotenv);
|
|
59
53
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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);
|
|
64
77
|
}
|
|
65
|
-
this.warn('dotenv-webpack: "options.sample" is a deprecated property. Please update your configuration to use "options.safe" instead.', silent);
|
|
66
78
|
}
|
|
79
|
+
}, {
|
|
80
|
+
key: 'gatherVariables',
|
|
81
|
+
value: function gatherVariables() {
|
|
82
|
+
var safe = this.config.safe;
|
|
67
83
|
|
|
68
|
-
|
|
69
|
-
if (systemvars) {
|
|
70
|
-
Object.keys(process.env).map(function (key) {
|
|
71
|
-
vars[key] = process.env[key];
|
|
72
|
-
});
|
|
73
|
-
}
|
|
84
|
+
var vars = this.initializeVars();
|
|
74
85
|
|
|
75
|
-
|
|
86
|
+
var _getEnvs = this.getEnvs(),
|
|
87
|
+
env = _getEnvs.env,
|
|
88
|
+
blueprint = _getEnvs.blueprint;
|
|
76
89
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
90
|
+
Object.keys(blueprint).map(function (key) {
|
|
91
|
+
var value = vars.hasOwnProperty(key) ? vars[key] : env[key];
|
|
92
|
+
if (!value && safe) {
|
|
93
|
+
throw new Error('Missing environment variable: ' + key);
|
|
94
|
+
} else {
|
|
95
|
+
vars[key] = value;
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
// add the leftovers
|
|
100
|
+
if (safe) {
|
|
101
|
+
_extends(vars, env);
|
|
82
102
|
}
|
|
83
|
-
|
|
103
|
+
|
|
104
|
+
return vars;
|
|
105
|
+
}
|
|
106
|
+
}, {
|
|
107
|
+
key: 'initializeVars',
|
|
108
|
+
value: function initializeVars() {
|
|
109
|
+
return this.config.systemvars ? _extends({}, process.env) : {};
|
|
84
110
|
}
|
|
111
|
+
}, {
|
|
112
|
+
key: 'getEnvs',
|
|
113
|
+
value: function getEnvs() {
|
|
114
|
+
var _config2 = this.config,
|
|
115
|
+
path = _config2.path,
|
|
116
|
+
silent = _config2.silent,
|
|
117
|
+
safe = _config2.safe;
|
|
85
118
|
|
|
86
|
-
|
|
87
|
-
var
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
var vKey = 'process.env.' + key;
|
|
98
|
-
var vValue = void 0;
|
|
99
|
-
if (expand) {
|
|
100
|
-
if (v.substring(0, 2) === '\\$') {
|
|
101
|
-
vValue = v.substring(1);
|
|
102
|
-
} else if (v.indexOf('\\$') > 0) {
|
|
103
|
-
vValue = v.replace(/\\\$/g, '$');
|
|
104
|
-
} else {
|
|
105
|
-
vValue = interpolate(v, vars);
|
|
119
|
+
|
|
120
|
+
var env = _dotenvDefaults2.default.parse(this.loadFile({
|
|
121
|
+
file: path,
|
|
122
|
+
silent: silent
|
|
123
|
+
}), this.getDefaults());
|
|
124
|
+
|
|
125
|
+
var blueprint = env;
|
|
126
|
+
if (safe) {
|
|
127
|
+
var file = './.env.example';
|
|
128
|
+
if (safe !== true) {
|
|
129
|
+
file = safe;
|
|
106
130
|
}
|
|
107
|
-
|
|
108
|
-
|
|
131
|
+
blueprint = _dotenvDefaults2.default.parse(this.loadFile({
|
|
132
|
+
file: file,
|
|
133
|
+
silent: silent
|
|
134
|
+
}));
|
|
109
135
|
}
|
|
110
136
|
|
|
111
|
-
|
|
137
|
+
return {
|
|
138
|
+
env: env,
|
|
139
|
+
blueprint: blueprint
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
}, {
|
|
143
|
+
key: 'getDefaults',
|
|
144
|
+
value: function getDefaults() {
|
|
145
|
+
var _config3 = this.config,
|
|
146
|
+
silent = _config3.silent,
|
|
147
|
+
defaults = _config3.defaults;
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
if (defaults) {
|
|
151
|
+
return this.loadFile({
|
|
152
|
+
file: defaults === true ? './.env.defaults' : defaults,
|
|
153
|
+
silent: silent
|
|
154
|
+
});
|
|
155
|
+
}
|
|
112
156
|
|
|
113
|
-
return
|
|
114
|
-
}
|
|
157
|
+
return '';
|
|
158
|
+
}
|
|
159
|
+
}, {
|
|
160
|
+
key: 'formatData',
|
|
161
|
+
value: function formatData() {
|
|
162
|
+
var vars = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
163
|
+
var expand = this.config.expand;
|
|
164
|
+
|
|
165
|
+
return Object.keys(vars).reduce(function (obj, key) {
|
|
166
|
+
var v = vars[key];
|
|
167
|
+
var vKey = 'process.env.' + key;
|
|
168
|
+
var vValue = void 0;
|
|
169
|
+
if (expand) {
|
|
170
|
+
if (v.substring(0, 2) === '\\$') {
|
|
171
|
+
vValue = v.substring(1);
|
|
172
|
+
} else if (v.indexOf('\\$') > 0) {
|
|
173
|
+
vValue = v.replace(/\\\$/g, '$');
|
|
174
|
+
} else {
|
|
175
|
+
vValue = interpolate(v, vars);
|
|
176
|
+
}
|
|
177
|
+
} else {
|
|
178
|
+
vValue = v;
|
|
179
|
+
}
|
|
115
180
|
|
|
116
|
-
|
|
117
|
-
}
|
|
181
|
+
obj[vKey] = JSON.stringify(vValue);
|
|
118
182
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
* @param {Boolean} silent - If true, suppress warnings, if false, display warnings.
|
|
123
|
-
* @returns {Object}
|
|
124
|
-
*/
|
|
183
|
+
return obj;
|
|
184
|
+
}, {});
|
|
185
|
+
}
|
|
125
186
|
|
|
187
|
+
/**
|
|
188
|
+
* Load a file.
|
|
189
|
+
* @param {String} config.file - The file to load.
|
|
190
|
+
* @param {Boolean} config.silent - If true, suppress warnings, if false, display warnings.
|
|
191
|
+
* @returns {Object}
|
|
192
|
+
*/
|
|
126
193
|
|
|
127
|
-
|
|
194
|
+
}, {
|
|
128
195
|
key: 'loadFile',
|
|
129
|
-
value: function loadFile(
|
|
196
|
+
value: function loadFile(_ref) {
|
|
197
|
+
var file = _ref.file,
|
|
198
|
+
silent = _ref.silent;
|
|
199
|
+
|
|
130
200
|
try {
|
|
131
|
-
return
|
|
201
|
+
return _fs2.default.readFileSync(file, 'utf8');
|
|
132
202
|
} catch (err) {
|
|
133
203
|
this.warn('Failed to load ' + file + '.', silent);
|
|
134
204
|
return {};
|
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.7.0",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"precommit": "npm run lint",
|
|
@@ -42,8 +42,7 @@
|
|
|
42
42
|
"webpack": "^1 || ^2 || ^3 || ^4"
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"dotenv": "^
|
|
46
|
-
"dotenv-expand": "^4.0.1"
|
|
45
|
+
"dotenv-defaults": "^1.0.2"
|
|
47
46
|
},
|
|
48
47
|
"devDependencies": {
|
|
49
48
|
"babel-cli": "^6.26.0",
|