dotenv-webpack 1.5.5 → 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 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">
@@ -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
- Be 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.
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
 
@@ -96,7 +93,9 @@ Use the following properties to configure your instance.
96
93
  * **path** (`'./.env'`) - The path to your environment variables.
97
94
  * **safe** (`false`) - If false ignore safe-mode, if true load `'./.env.example'`, if a string load that file as the sample.
98
95
  * **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 surpressed.
96
+ * **silent** (`false`) - If true, all warnings will be suppressed.
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.
100
99
 
101
100
  The following example shows how to set any/all arguments.
102
101
 
@@ -108,7 +107,8 @@ module.exports = {
108
107
  path: './some.other.env', // load this now instead of the ones in '.env'
109
108
  safe: true, // load '.env.example' to verify the '.env' variables are all set. Can also be a string to a different file.
110
109
  systemvars: true, // load all the predefined 'process.env' variables which will trump anything local per dotenv specs.
111
- silent: true // hide any errors
110
+ silent: true, // hide any errors
111
+ defaults: false // load '.env.defaults' as the default values if empty.
112
112
  })
113
113
  ]
114
114
  ...
package/browser.js ADDED
@@ -0,0 +1,3 @@
1
+ var plugin = require('./dist').default
2
+
3
+ module.exports = plugin
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 _dotenv = require('dotenv');
11
+ var _dotenvDefaults = require('dotenv-defaults');
10
12
 
11
- var _dotenv2 = _interopRequireDefault(_dotenv);
13
+ var _dotenvDefaults2 = _interopRequireDefault(_dotenvDefaults);
12
14
 
13
15
  var _fs = require('fs');
14
16
 
@@ -20,83 +22,183 @@ 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 {Bool|String} [options.safe=false] - If false ignore safe-mode, if true load `'./.env.example'`, if a string load that file as the sample.
29
- * @param {Bool} [options.systemvars=false] - If true, load system environment variables.
30
- * @param {Bool} [options.silent=false] - If true, suppress warnings, if false, display warnings.
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 _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
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
- // Catch older packages, but hold their hand (just for a bit)
45
- if (sample) {
46
- if (safe) {
47
- safe = sample;
48
- }
49
- this.warn('dotenv-webpack: "options.sample" is a deprecated property. Please update your configuration to use "options.safe" instead.', silent);
50
- }
54
+ this.config = _extends({}, {
55
+ path: './.env'
56
+ }, config);
51
57
 
52
- var vars = {};
53
- if (systemvars) {
54
- Object.keys(process.env).map(function (key) {
55
- vars[key] = process.env[key];
56
- });
57
- }
58
+ this.checkDeprecation();
58
59
 
59
- var env = this.loadFile(path, silent);
60
+ return new _webpack.DefinePlugin(this.formatData(this.gatherVariables()));
61
+ }
60
62
 
61
- var blueprint = env;
62
- if (safe) {
63
- var file = './.env.example';
64
- if (safe !== true) {
65
- file = safe;
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);
66
77
  }
67
- blueprint = this.loadFile(file, silent);
68
78
  }
79
+ }, {
80
+ key: 'gatherVariables',
81
+ value: function gatherVariables() {
82
+ var safe = this.config.safe;
83
+
84
+ var vars = this.initializeVars();
85
+
86
+ var _getEnvs = this.getEnvs(),
87
+ env = _getEnvs.env,
88
+ blueprint = _getEnvs.blueprint;
89
+
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
+ });
69
98
 
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;
99
+ // add the leftovers
100
+ if (safe) {
101
+ _extends(vars, env);
76
102
  }
77
- });
78
103
 
79
- var formatData = Object.keys(vars).reduce(function (obj, key) {
80
- obj['process.env.' + key] = JSON.stringify(vars[key]);
81
- return obj;
82
- }, {});
104
+ return vars;
105
+ }
106
+ }, {
107
+ key: 'initializeVars',
108
+ value: function initializeVars() {
109
+ return this.config.systemvars ? _extends({}, process.env) : {};
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;
83
118
 
84
- return new _webpack.DefinePlugin(formatData);
85
- }
86
119
 
87
- /**
88
- * Load and parses a file.
89
- * @param {String} file - The file to load.
90
- * @param {Bool} silent - If true, suppress warnings, if false, display warnings.
91
- * @returns {Object}
92
- */
120
+ var env = _dotenvDefaults2.default.parse(this.loadFile({
121
+ file: path,
122
+ silent: silent
123
+ }), this.getDefaults());
93
124
 
125
+ var blueprint = env;
126
+ if (safe) {
127
+ var file = './.env.example';
128
+ if (safe !== true) {
129
+ file = safe;
130
+ }
131
+ blueprint = _dotenvDefaults2.default.parse(this.loadFile({
132
+ file: file,
133
+ silent: silent
134
+ }));
135
+ }
94
136
 
95
- _createClass(Dotenv, [{
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
+ }
156
+
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
+ }
180
+
181
+ obj[vKey] = JSON.stringify(vValue);
182
+
183
+ return obj;
184
+ }, {});
185
+ }
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
+ */
193
+
194
+ }, {
96
195
  key: 'loadFile',
97
- value: function loadFile(file, silent) {
196
+ value: function loadFile(_ref) {
197
+ var file = _ref.file,
198
+ silent = _ref.silent;
199
+
98
200
  try {
99
- return _dotenv2.default.parse(_fs2.default.readFileSync(file));
201
+ return _fs2.default.readFileSync(file, 'utf8');
100
202
  } catch (err) {
101
203
  this.warn('Failed to load ' + file + '.', silent);
102
204
  return {};
@@ -106,7 +208,7 @@ var Dotenv = function () {
106
208
  /**
107
209
  * Displays a console message if 'silent' is falsey
108
210
  * @param {String} msg - The message.
109
- * @param {Bool} silent - If true, display the message, if false, suppress the message.
211
+ * @param {Boolean} silent - If true, display the message, if false, suppress the message.
110
212
  */
111
213
 
112
214
  }, {
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.5.5",
4
+ "version": "1.7.0",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
7
  "precommit": "npm run lint",
@@ -42,23 +42,24 @@
42
42
  "webpack": "^1 || ^2 || ^3 || ^4"
43
43
  },
44
44
  "dependencies": {
45
- "dotenv": "^5.0.1"
45
+ "dotenv-defaults": "^1.0.2"
46
46
  },
47
47
  "devDependencies": {
48
48
  "babel-cli": "^6.26.0",
49
- "babel-eslint": "^8.2.2",
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
- "husky": "^0.14.3",
55
+ "esdoc-standard-plugin": "^1.0.0",
56
+ "husky": "^1.1.2",
56
57
  "mocha": "^5.0.1",
57
- "nyc": "^11.3.0",
58
+ "nyc": "^13.1.0",
58
59
  "rimraf": "^2.6.2",
59
- "sinon": "^4.4.2",
60
- "standard": "^11.0.0",
61
- "webpack": "^3.10.0"
60
+ "sinon": "^7.0.0",
61
+ "standard": "^12.0.0",
62
+ "webpack": "^4.6.0"
62
63
  },
63
64
  "files": [
64
65
  "dist"