question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support function-type webpack configs

See original GitHub issue

Function-type config support added in webpack 2 is super useful for making conditional configs. I wrote a tiny package that wraps webpack-merge and adds support for merging function configs, but it seems like it could very easily be a supported feature in webpack-merge.

Here’s how I’d envision it working:

// this needs a better name
const webpackMerge = require('webpack-merge/config-function');

const mergeStrategy = {/* ... */};

module.exports = webpackMerge.smartStrategy(mergeStrategy)(
  (env, options) => ({
    output: {
      filename: env.useHash ? '[name]-[].js' : '[name].js',
    },
  }),
  // ...
);

Merge functions imported from webpack-merge/config-function (name extremely WIP) would be the same merge functions provided by webpack-merge, but each function would return a config function instead of a config object. This returned function would take the parameters given to it by the webpack/webpack-dev-server CLI and pass them on to each config function. Functions would be called and the result would be merged using the merge functions from webpack-merge. Essentially, each merge function would be a lightweight wrapper around the parent merge function.

If this is something that’s of interest to you, I can cook up a PR.

This could very easily be extended to support Promise configs as well (as mentioned in #81).

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:29 (11 by maintainers)

github_iconTop GitHub Comments

35reactions
Joonpark13commented, Sep 29, 2018

@5arias a workaround that I’m using right now is as follows:

in my webpack.dev.js I do

module.exports = env => merge(common(env), {
    ...
});

and in my webpack.common.js

module.exports = env = ({
    ...
    ---USE env HERE---
});

That said, I’d love to see this functionality added!

3reactions
vitaletscommented, Jul 9, 2019

@Joonpark13

@5arias a workaround that I’m using right now is as follows:

in my webpack.dev.js I do

module.exports = env => merge(common(env), {
    ...
});

and in my webpack.common.js

module.exports = env = ({
    ...
    ---USE env HERE---
});

That said, I’d love to see this functionality added!

This looks great but how do you run your builds from npm scripts? Having

"build:dev": "webpack --config webpack.dev.js --env development",
"build:prod": "webpack --config webpack.prod.js --env production"

looks like over-configuration: both dev / development and prod / production in command. The config itself should define all needed params.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuration Types - webpack
Instead of exporting a single configuration object/function, you may export multiple configurations (multiple functions are supported since webpack 3.1.0).
Read more >
How to describe types for webpack config exporting as a ...
I use webpack config as a function with webpack-merge export default (env) => { // working with env data return mergeWithRules({ module: ...
Read more >
Setting TypeScript For Modern React Projects Using Webpack
Create React App is an officially supported way to create single-page React applications. It offers a modern build setup with no configuration.
Read more >
typescript-cheatsheet - GitHub Pages
In TypeScript, we support much the same types as you would expect in JavaScript, ... Webpack webpack.config.js file configuration to handle TypeScript:.
Read more >
@prefresh/webpack | Yarn - Package Manager
Prefresh-webpack. npm version. Setup. npm i -s @prefresh/webpack ## OR yarn add @prefresh/webpack. Then add it to your webpack config by doing
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found