webpack.config.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. const commonConfig = require("./build-utils/webpack.common");
  2. const webpackMerge = require("webpack-merge");
  3. const defaultEnv = { env: "prod" };
  4. /**
  5. *
  6. * @param {string|Array} addonsArg
  7. * @return {unknown[]}
  8. */
  9. const addons = addonsArg => {
  10. // Normalize array of addons (flatten).
  11. let addons = [].concat
  12. .apply([], [addonsArg])
  13. // If addons is undefined, filter it out.
  14. .filter(Boolean);
  15. return addons.map(addonName =>
  16. require(`./build-utils/addons/webpack.${addonName}.js`)
  17. );
  18. };
  19. // Export a function returning config instead of a static config.
  20. // The purpose is to allow use of the environment.
  21. module.exports = (env = defaultEnv) => {
  22. if (!env || !env.env) {
  23. throw new Error(
  24. "You must pass an -env.env flag into your build for webpack to work"
  25. );
  26. }
  27. console.log("Env: ", env);
  28. const envConfig = require(`./build-utils/webpack.${env.env}`);
  29. // The rightmost overwrites (merges into) its left sibling.
  30. const mergedConfig = webpackMerge(
  31. commonConfig,
  32. envConfig,
  33. ...addons(env.addons)
  34. );
  35. console.log(mergedConfig);
  36. return mergedConfig;
  37. };