Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

268 righe
8.7 KiB

  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports["default"] = void 0;
  6. var _dotenvDefaults = _interopRequireDefault(require("dotenv-defaults"));
  7. var _fs = _interopRequireDefault(require("fs"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  9. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  10. 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); } }
  11. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
  12. // Mostly taken from here: https://github.com/motdotla/dotenv-expand/blob/master/lib/main.js#L4
  13. var interpolate = function interpolate(env, vars) {
  14. var matches = env.match(/\$([a-zA-Z0-9_]+)|\${([a-zA-Z0-9_]+)}/g) || [];
  15. matches.forEach(function (match) {
  16. var key = match.replace(/\$|{|}/g, '');
  17. var variable = vars[key] || '';
  18. variable = interpolate(variable, vars);
  19. env = env.replace(match, variable);
  20. });
  21. return env;
  22. };
  23. var isMainThreadElectron = function isMainThreadElectron(target) {
  24. return target.startsWith('electron') && target.endsWith('main');
  25. };
  26. var Dotenv = /*#__PURE__*/function () {
  27. /**
  28. * The dotenv-webpack plugin.
  29. * @param {Object} options - The parameters.
  30. * @param {String} [options.path=./.env] - The location of the environment variable.
  31. * @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.
  32. * @param {Boolean} [options.systemvars=false] - If true, load system environment variables.
  33. * @param {Boolean} [options.silent=false] - If true, suppress warnings, if false, display warnings.
  34. * @param {String} [options.prefix=process.env.] - The prefix, used to denote environment variables.
  35. * @returns {webpack.DefinePlugin}
  36. */
  37. function Dotenv() {
  38. var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  39. _classCallCheck(this, Dotenv);
  40. this.config = Object.assign({}, {
  41. path: './.env',
  42. prefix: 'process.env.'
  43. }, config);
  44. }
  45. _createClass(Dotenv, [{
  46. key: "apply",
  47. value: function apply(compiler) {
  48. var _compiler$options$tar;
  49. var variables = this.gatherVariables();
  50. var target = (_compiler$options$tar = compiler.options.target) !== null && _compiler$options$tar !== void 0 ? _compiler$options$tar : 'web';
  51. var version = compiler.webpack && compiler.webpack.version || '4';
  52. var data = this.formatData({
  53. variables: variables,
  54. target: target,
  55. version: version
  56. });
  57. var DefinePlugin = compiler.webpack && compiler.webpack.DefinePlugin || require('webpack').DefinePlugin;
  58. new DefinePlugin(data).apply(compiler);
  59. }
  60. }, {
  61. key: "gatherVariables",
  62. value: function gatherVariables() {
  63. var _this$config = this.config,
  64. safe = _this$config.safe,
  65. allowEmptyValues = _this$config.allowEmptyValues;
  66. var vars = this.initializeVars();
  67. var _this$getEnvs = this.getEnvs(),
  68. env = _this$getEnvs.env,
  69. blueprint = _this$getEnvs.blueprint;
  70. Object.keys(blueprint).forEach(function (key) {
  71. var value = Object.prototype.hasOwnProperty.call(vars, key) ? vars[key] : env[key];
  72. var isMissing = typeof value === 'undefined' || value === null || !allowEmptyValues && value === '';
  73. if (safe && isMissing) {
  74. throw new Error("Missing environment variable: ".concat(key));
  75. } else {
  76. vars[key] = value;
  77. }
  78. }); // add the leftovers
  79. if (safe) {
  80. Object.keys(env).forEach(function (key) {
  81. if (!Object.prototype.hasOwnProperty.call(vars, key)) {
  82. vars[key] = env[key];
  83. }
  84. });
  85. }
  86. return vars;
  87. }
  88. }, {
  89. key: "initializeVars",
  90. value: function initializeVars() {
  91. return this.config.systemvars ? Object.assign({}, process.env) : {};
  92. }
  93. }, {
  94. key: "getEnvs",
  95. value: function getEnvs() {
  96. var _this$config2 = this.config,
  97. path = _this$config2.path,
  98. silent = _this$config2.silent,
  99. safe = _this$config2.safe;
  100. var env = _dotenvDefaults["default"].parse(this.loadFile({
  101. file: path,
  102. silent: silent
  103. }), this.getDefaults());
  104. var blueprint = env;
  105. if (safe) {
  106. var file = "".concat(path, ".example");
  107. if (safe !== true) {
  108. file = safe;
  109. }
  110. blueprint = _dotenvDefaults["default"].parse(this.loadFile({
  111. file: file,
  112. silent: silent
  113. }));
  114. }
  115. return {
  116. env: env,
  117. blueprint: blueprint
  118. };
  119. }
  120. }, {
  121. key: "getDefaults",
  122. value: function getDefaults() {
  123. var _this$config3 = this.config,
  124. path = _this$config3.path,
  125. silent = _this$config3.silent,
  126. defaults = _this$config3.defaults;
  127. if (defaults) {
  128. return this.loadFile({
  129. file: defaults === true ? "".concat(path, ".defaults") : defaults,
  130. silent: silent
  131. });
  132. }
  133. return '';
  134. }
  135. }, {
  136. key: "formatData",
  137. value: function formatData(_ref) {
  138. var _ref$variables = _ref.variables,
  139. variables = _ref$variables === void 0 ? {} : _ref$variables,
  140. target = _ref.target,
  141. version = _ref.version;
  142. var _this$config4 = this.config,
  143. expand = _this$config4.expand,
  144. prefix = _this$config4.prefix;
  145. var formatted = Object.keys(variables).reduce(function (obj, key) {
  146. var v = variables[key];
  147. var vKey = "".concat(prefix).concat(key);
  148. var vValue;
  149. if (expand) {
  150. if (v.substring(0, 2) === '\\$') {
  151. vValue = v.substring(1);
  152. } else if (v.indexOf('\\$') > 0) {
  153. vValue = v.replace(/\\\$/g, '$');
  154. } else {
  155. vValue = interpolate(v, variables);
  156. }
  157. } else {
  158. vValue = v;
  159. }
  160. obj[vKey] = JSON.stringify(vValue);
  161. return obj;
  162. }, {}); // We have to stub any remaining `process.env`s due to Webpack 5 not polyfilling it anymore
  163. // https://github.com/mrsteele/dotenv-webpack/issues/240#issuecomment-710231534
  164. // However, if someone targets Node or Electron `process.env` still exists, and should therefore be kept
  165. // https://webpack.js.org/configuration/target
  166. if (this.shouldStub({
  167. target: target,
  168. version: version
  169. })) {
  170. // Results in `"MISSING_ENV_VAR".NAME` which is valid JS
  171. formatted['process.env'] = '"MISSING_ENV_VAR"';
  172. }
  173. return formatted;
  174. }
  175. }, {
  176. key: "shouldStub",
  177. value: function shouldStub(_ref2) {
  178. var _this = this;
  179. var targetInput = _ref2.target,
  180. version = _ref2.version;
  181. if (!version.startsWith('5')) {
  182. return false;
  183. }
  184. var targets = Array.isArray(targetInput) ? targetInput : [targetInput];
  185. return targets.every(function (target) {
  186. return (// If configured prefix is 'process.env'
  187. _this.config.prefix === 'process.env.' && // If we're not configured to never stub
  188. _this.config.ignoreStub !== true && ( // And
  189. // We are configured to always stub
  190. _this.config.ignoreStub === false || // Or if we should according to the target
  191. !target.includes('node') && !isMainThreadElectron(target))
  192. );
  193. });
  194. }
  195. /**
  196. * Load a file.
  197. * @param {String} config.file - The file to load.
  198. * @param {Boolean} config.silent - If true, suppress warnings, if false, display warnings.
  199. * @returns {Object}
  200. */
  201. }, {
  202. key: "loadFile",
  203. value: function loadFile(_ref3) {
  204. var file = _ref3.file,
  205. silent = _ref3.silent;
  206. try {
  207. return _fs["default"].readFileSync(file, 'utf8');
  208. } catch (err) {
  209. this.warn("Failed to load ".concat(file, "."), silent);
  210. return {};
  211. }
  212. }
  213. /**
  214. * Displays a console message if 'silent' is falsey
  215. * @param {String} msg - The message.
  216. * @param {Boolean} silent - If true, display the message, if false, suppress the message.
  217. */
  218. }, {
  219. key: "warn",
  220. value: function warn(msg, silent) {
  221. !silent && console.warn(msg);
  222. }
  223. }]);
  224. return Dotenv;
  225. }();
  226. var _default = Dotenv;
  227. exports["default"] = _default;