Dashboard sipadu mbip
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

MixDefinitionsPlugin.js 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. let webpack = require('webpack');
  2. let dotenv = require('dotenv');
  3. let expand = require('dotenv-expand');
  4. /**
  5. * Create a new plugin instance.
  6. *
  7. * @param {string} envPath
  8. */
  9. function MixDefinitionsPlugin(envPath) {
  10. expand(
  11. dotenv.config({
  12. path: envPath || Mix.paths.root('.env')
  13. })
  14. );
  15. }
  16. /**
  17. * Build up the necessary definitions and add them to the DefinePlugin.
  18. *
  19. * @param {Object|null} merge
  20. */
  21. MixDefinitionsPlugin.build = function(merge = {}) {
  22. return new webpack.DefinePlugin(
  23. new MixDefinitionsPlugin().getDefinitions(merge)
  24. );
  25. };
  26. /**
  27. * Build all MIX_ definitions for Webpack's DefinePlugin.
  28. *
  29. * @param {object} merge
  30. */
  31. MixDefinitionsPlugin.prototype.getDefinitions = function(merge) {
  32. let regex = /^MIX_/i;
  33. // Filter out env vars that don't begin with MIX_.
  34. let env = Object.keys(process.env)
  35. .filter(key => regex.test(key))
  36. .reduce((value, key) => {
  37. value[key] = process.env[key];
  38. return value;
  39. }, {});
  40. let values = Object.assign(env, merge);
  41. return (
  42. Object.keys(values)
  43. // Stringify all values so they can be fed into Webpack's DefinePlugin.
  44. .reduce((value, key) => {
  45. value[`process.env.${key}`] = JSON.stringify(values[key]);
  46. return value;
  47. }, {})
  48. );
  49. };
  50. module.exports = MixDefinitionsPlugin;