dotenv-webpack 6.0.4 → 7.0.3

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.
Files changed (3) hide show
  1. package/README.md +9 -0
  2. package/dist/index.js +72 -36
  3. package/package.json +14 -7
package/README.md CHANGED
@@ -95,6 +95,14 @@ Add `.env` to your `.gitignore` file
95
95
 
96
96
  Due to the fact that we use `webpack.DefinePlugin` under the hood, we cannot support destructing as that breaks how this plugin is meant to be used. Because of this, please reference your variables without destructing. For more information about this, please review the issue [here](https://github.com/mrsteele/dotenv-webpack/issues/70).
97
97
 
98
+ ## `process.env` stubbing / replacing
99
+
100
+ `process.env` is not polyfilled in Webpack 5+, leading to errors in environments where `process` is `null` (browsers).
101
+
102
+ We automatically replace any remaining `process.env`s in these environments with `"MISSING_ENV_VAR"` to avoid these errors.
103
+
104
+ If you are running into issues where you or another package you use interfaces with `process.env`, it might be best to set `ignoreStub: true` and make sure you always reference variables that exist within your code (See [this issue](https://github.com/mrsteele/dotenv-webpack/issues/271) for more information).
105
+
98
106
  ## Properties
99
107
 
100
108
  Use the following properties to configure your instance.
@@ -106,6 +114,7 @@ Use the following properties to configure your instance.
106
114
  * **silent** (`false`) - If true, all warnings will be suppressed.
107
115
  * **expand** (`false`) - Allows your variables to be "expanded" for reusability within your `.env` file.
108
116
  * **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).
117
+ * **ignoreStub** (`false`) - Override the automatic check whether to stub `process.env`. [Read more here](#user-content-processenv-stubbing--replacing).
109
118
 
110
119
  The following example shows how to set any/all arguments.
111
120
 
package/dist/index.js CHANGED
@@ -31,6 +31,10 @@ var interpolate = function interpolate(env, vars) {
31
31
  return env;
32
32
  };
33
33
 
34
+ var isMainThreadElectron = function isMainThreadElectron(target) {
35
+ return target.startsWith('electron') && target.endsWith('main');
36
+ };
37
+
34
38
  var Dotenv = /*#__PURE__*/function () {
35
39
  /**
36
40
  * The dotenv-webpack plugin.
@@ -49,32 +53,29 @@ var Dotenv = /*#__PURE__*/function () {
49
53
  this.config = Object.assign({}, {
50
54
  path: './.env'
51
55
  }, config);
52
- this.checkDeprecation();
53
- return new _webpack.DefinePlugin(this.formatData(this.gatherVariables()));
54
56
  }
55
57
 
56
58
  _createClass(Dotenv, [{
57
- key: "checkDeprecation",
58
- value: function checkDeprecation() {
59
- var _this$config = this.config,
60
- sample = _this$config.sample,
61
- safe = _this$config.safe,
62
- silent = _this$config.silent; // Catch older packages, but hold their hand (just for a bit)
63
-
64
- if (sample) {
65
- if (safe) {
66
- this.config.safe = sample;
67
- }
68
-
69
- this.warn('dotenv-webpack: "options.sample" is a deprecated property. Please update your configuration to use "options.safe" instead.', silent);
70
- }
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
+ new _webpack.DefinePlugin(data).apply(compiler);
71
72
  }
72
73
  }, {
73
74
  key: "gatherVariables",
74
75
  value: function gatherVariables() {
75
- var _this$config2 = this.config,
76
- safe = _this$config2.safe,
77
- allowEmptyValues = _this$config2.allowEmptyValues;
76
+ var _this$config = this.config,
77
+ safe = _this$config.safe,
78
+ allowEmptyValues = _this$config.allowEmptyValues;
78
79
  var vars = this.initializeVars();
79
80
 
80
81
  var _this$getEnvs = this.getEnvs(),
@@ -110,10 +111,10 @@ var Dotenv = /*#__PURE__*/function () {
110
111
  }, {
111
112
  key: "getEnvs",
112
113
  value: function getEnvs() {
113
- var _this$config3 = this.config,
114
- path = _this$config3.path,
115
- silent = _this$config3.silent,
116
- safe = _this$config3.safe;
114
+ var _this$config2 = this.config,
115
+ path = _this$config2.path,
116
+ silent = _this$config2.silent,
117
+ safe = _this$config2.safe;
117
118
 
118
119
  var env = _dotenvDefaults["default"].parse(this.loadFile({
119
120
  file: path,
@@ -143,9 +144,9 @@ var Dotenv = /*#__PURE__*/function () {
143
144
  }, {
144
145
  key: "getDefaults",
145
146
  value: function getDefaults() {
146
- var _this$config4 = this.config,
147
- silent = _this$config4.silent,
148
- defaults = _this$config4.defaults;
147
+ var _this$config3 = this.config,
148
+ silent = _this$config3.silent,
149
+ defaults = _this$config3.defaults;
149
150
 
150
151
  if (defaults) {
151
152
  return this.loadFile({
@@ -158,11 +159,14 @@ var Dotenv = /*#__PURE__*/function () {
158
159
  }
159
160
  }, {
160
161
  key: "formatData",
161
- value: function formatData() {
162
- var vars = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
162
+ value: function formatData(_ref) {
163
+ var _ref$variables = _ref.variables,
164
+ variables = _ref$variables === void 0 ? {} : _ref$variables,
165
+ target = _ref.target,
166
+ version = _ref.version;
163
167
  var expand = this.config.expand;
164
- var formatted = Object.keys(vars).reduce(function (obj, key) {
165
- var v = vars[key];
168
+ var formatted = Object.keys(variables).reduce(function (obj, key) {
169
+ var v = variables[key];
166
170
  var vKey = "process.env.".concat(key);
167
171
  var vValue;
168
172
 
@@ -172,7 +176,7 @@ var Dotenv = /*#__PURE__*/function () {
172
176
  } else if (v.indexOf('\\$') > 0) {
173
177
  vValue = v.replace(/\\\$/g, '$');
174
178
  } else {
175
- vValue = interpolate(v, vars);
179
+ vValue = interpolate(v, variables);
176
180
  }
177
181
  } else {
178
182
  vValue = v;
@@ -180,11 +184,43 @@ var Dotenv = /*#__PURE__*/function () {
180
184
 
181
185
  obj[vKey] = JSON.stringify(vValue);
182
186
  return obj;
183
- }, {}); // fix in case of missing
187
+ }, {}); // We have to stub any remaining `process.env`s due to Webpack 5 not polyfilling it anymore
188
+ // https://github.com/mrsteele/dotenv-webpack/issues/240#issuecomment-710231534
189
+ // However, if someone targets Node or Electron `process.env` still exists, and should therefore be kept
190
+ // https://webpack.js.org/configuration/target
191
+
192
+ if (this.shouldStub({
193
+ target: target,
194
+ version: version
195
+ })) {
196
+ // Results in `"MISSING_ENV_VAR".NAME` which is valid JS
197
+ formatted['process.env'] = '"MISSING_ENV_VAR"';
198
+ }
184
199
 
185
- formatted['process.env'] = '{}';
186
200
  return formatted;
187
201
  }
202
+ }, {
203
+ key: "shouldStub",
204
+ value: function shouldStub(_ref2) {
205
+ var _this = this;
206
+
207
+ var targetInput = _ref2.target,
208
+ version = _ref2.version;
209
+
210
+ if (!version.startsWith('5')) {
211
+ return false;
212
+ }
213
+
214
+ var targets = Array.isArray(targetInput) ? targetInput : [targetInput];
215
+ return targets.every(function (target) {
216
+ return (// If we're not configured to never stub
217
+ _this.config.ignoreStub !== true && ( // And
218
+ // We are configured to always stub
219
+ _this.config.ignoreStub === false || // Or if we should according to the target
220
+ !target.includes('node') && !isMainThreadElectron(target))
221
+ );
222
+ });
223
+ }
188
224
  /**
189
225
  * Load a file.
190
226
  * @param {String} config.file - The file to load.
@@ -194,9 +230,9 @@ var Dotenv = /*#__PURE__*/function () {
194
230
 
195
231
  }, {
196
232
  key: "loadFile",
197
- value: function loadFile(_ref) {
198
- var file = _ref.file,
199
- silent = _ref.silent;
233
+ value: function loadFile(_ref3) {
234
+ var file = _ref3.file,
235
+ silent = _ref3.silent;
200
236
 
201
237
  try {
202
238
  return _fs["default"].readFileSync(file, 'utf8');
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": "6.0.4",
4
+ "version": "7.0.3",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
7
  "precommit": "npm run lint",
@@ -21,7 +21,10 @@
21
21
  },
22
22
  "jest": {
23
23
  "coverageDirectory": "./coverage/",
24
- "collectCoverage": true
24
+ "collectCoverage": true,
25
+ "watchPathIgnorePatterns": [
26
+ "output/.*?"
27
+ ]
25
28
  },
26
29
  "keywords": [
27
30
  "dotenv",
@@ -41,20 +44,24 @@
41
44
  "url": "https://github.com/mrsteele/dotenv-webpack/issues"
42
45
  },
43
46
  "homepage": "https://github.com/mrsteele/dotenv-webpack#readme",
47
+ "engines": {
48
+ "node": ">=10"
49
+ },
44
50
  "peerDependencies": {
45
- "webpack": "^1 || ^2 || ^3 || ^4 || ^5"
51
+ "webpack": "^4 || ^5"
46
52
  },
47
53
  "dependencies": {
48
- "dotenv-defaults": "^2.0.1"
54
+ "dotenv-defaults": "^2.0.2"
49
55
  },
50
56
  "devDependencies": {
51
57
  "@babel/cli": "^7.13.0",
52
58
  "@babel/core": "^7.13.8",
53
59
  "@babel/preset-env": "^7.13.9",
54
60
  "@babel/register": "^7.13.8",
55
- "husky": "^5.1.3",
56
- "jest": "^25.5.4",
57
- "jsdoc": "^3.6.6",
61
+ "fs-extra": "^9.1.0",
62
+ "husky": "^6.0.0",
63
+ "jest": "^26.6.3",
64
+ "jsdoc": "^3.6.7",
58
65
  "rimraf": "^3.0.0",
59
66
  "sinon": "^9.0.3",
60
67
  "standard": "^16.0.3",