Skip to content

Instantly share code, notes, and snippets.

@developit
Created January 2, 2019 13:49
Show Gist options
  • Save developit/ea457f2e01b5e300c2b749d788a13b09 to your computer and use it in GitHub Desktop.
Save developit/ea457f2e01b5e300c2b749d788a13b09 to your computer and use it in GitHub Desktop.
export function loadConfigModule(filename) {
// sub your own
return require(filename);
}
/**
* Modify a Webpack configuration by applying a mutator module/function.
* @param {Object} options
* @param {Object} options.config Webpack configuration object to modify in-place
* @param {Object} options.module A configuration function of the form `(config, env)=>`,
* or an Object with a `webpack` method of that form.
* @param {String} [options.target] "client" for browser bundles, "server" for Node/SSR/prerendering
* @param {Boolean} [options.production] Defaults to `process.env.NODE_ENV==='production'`
* @param {Boolean} [options.watch] Whether the bundle is being built for HMR/watch usage
* @param {Object} [options.env] Environment values to expose as options to the config function
* @param {Object} [options.helpers] Utility functions to pass as a 3rd arg to the config function
* Example: github.com/developit/preact-cli/blob/master/src/lib/webpack/transform-config.js#L29
*/
export function modifyConfig({
config,
module,
target = 'client',
production = process.env.NODE_ENV === 'production',
watch = false,
env = {},
helpers = {}
}) {
const webpackMutator = module.webpack || module;
if (typeof webpackMutator !== 'function') {
throw Error(`Webpack configuration plugin must be a function of the form (config, { target }) => void`);
}
const ssr = target === 'server';
const options = {
production,
watch,
target,
...env,
// Compat aliasdes for Next.js, Preact CLI, etc
ssr,
isProd: production,
isWatch: watch,
isServer: ssr,
};
webpackMutator(config, options, helpers);
return config;
}
@willhoney7
Copy link

Whether it's in webpack or not, webpack-merge seems like a good starting point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment