Skip to content

Instantly share code, notes, and snippets.

@schalkneethling
Last active January 27, 2019 15:29
Show Gist options
  • Save schalkneethling/95c0f8322ba02220ecc482a260077068 to your computer and use it in GitHub Desktop.
Save schalkneethling/95c0f8322ba02220ecc482a260077068 to your computer and use it in GitHub Desktop.
Small tweak to Sean Larkins' LoadPresets script for Webpack
// Original: https://github.com/TheLarkInn/webpack-workshop-2018/blob/feature/build-utils/build-utils/loadPresets.js
/**
* Usage:
* Use via NPM script
* Passing a single preset:
* "prod:compress": "npm run prod -- --env.presets compress",
* "prod:analyze": "npm run prod -- --env.presets analyze",
* Passing a multiple preset as a , separate list:
* "prod:compress-analyze": "npm run prod -- --env.presets compress,analyze",
*/
const webpackMerge = require("webpack-merge");
const applyPresets = (env = { presets: [] }) => {
let presets = env.presets || [];
// only split if the array has more than one item
if (presets.length) {
presets = presets.split(",");
}
/** @type {string[]} */
const mergedPresets = [].concat(...[presets]);
const mergedConfigs = mergedPresets.map(presetName =>
require(`./presets/webpack.${presetName}`)(env)
);
return webpackMerge({}, ...mergedConfigs);
};
module.exports = applyPresets;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment