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.

Merging config with multiple targets

See original GitHub issue

I’m after using multiple targets to have a server side config and a client side config packaged. Where one targets node and the other uses the default web target.

webpack.base.js

let clientConfig = {
  entry: "./src/website/js/App.tsx"
}

let serverConfig = {
  entry: "./src/server/server.js",
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "[name].js",
  },
  target: "node"
};
}

webpack.config.js

let clientConfig = {
  devtool: "source-map"
};

module.exports = webpackMerge(config, [clientConfig]);

Before the merge the json looks healthy:

[ { entry: './src/website/js/App.tsx' },
  { entry: './src/server/server.js',
    output: { path: 'C:\\Dev\\dist', filename: '[name].js' },
    target: 'node' } ]

But the output from the merge looks like it’s become a single object and hence now fails with the typical errors 'output.filename' is required ...

{ '0':
   { entry: './src/website/js/App.tsx',
     devtool: 'source-map',
     module: { rules: [Object] },
     output: { filename: '[name].js' },
     plugins: [ [Object], [Object] ] },
  '1':
   { entry: './src/server/server.js',
     output: { path: 'C:\\Dev\\dist', filename: '[name].js' },
     target: 'node' } }

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:18 (17 by maintainers)

github_iconTop GitHub Comments

1reaction
bebrawcommented, Mar 15, 2017

Thanks. I think I see a way that could be a good compromise:

  1. Change base.js to export like module.exports = { clientConfig, serverConfig };.
  2. dev.js - module.exports = [merge(base.clientConfig, clientConfig), base.serverConfig];
  3. prod.js - module.exports = [base.clientConfig, base.serverConfig];

It’s a bit more code than in the ideal solution. The key insight is that base.js doesn’t have to care about composition at all. It’s enough if it exposes the configuration fragments you compose later. In production you can skip merging entirely as earlier. During development you tweak the client configuration. If you needed to tweak the server too, it would be possible there.

I thought about expanding webpack-merge to support what you need but that would lead to a special case to document (also something for users to remember). That said, it could be sensible to write a multi-compiler friendly variant which performs merge based on array position like in your example.

0reactions
bebrawcommented, Feb 19, 2019

@ggirard07 Yeah, let’s close. Good call.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Combined documentation of multiple targets - Swift-DocC
When merging documentation archives, cross-target links to documentation archives that that are not passed as input will be transformed by DocC.
Read more >
How to build for multiple targets with webpack? - Stack Overflow
In my common config, I was using clean-webpack-plugin: plugins: [ new CleanWebpackPlugin(), ... ] Obviously, when building the second target ...
Read more >
Understanding and using the multi-target exporter pattern
This guide will introduce you to the multi-target exporter pattern. To achieve this we will: describe the multi-target exporter pattern and why it...
Read more >
Composing Configuration - SurviveJS
In this case, you would point to the targets through webpack --config parameter and merge common configuration through module.exports = merge(common, config); ....
Read more >
Configure multi-targeting - NET MAUI - Microsoft Learn
Combine filename and folder multi-targeting ; <!-- Android --> · Condition="$(TargetFramework.StartsWith('net7.0-android')) != true"> · Remove="**\ ...
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