webpack-cli icon indicating copy to clipboard operation
webpack-cli copied to clipboard

serve does not work with multi-server

Open Christian24 opened this issue 4 years ago • 25 comments

Describe the bug

We're having issues updating to webpack 5 and webpack serve. Our project is divided into multiple webpack configurations. Say for example we have one configuration for a SPA /login/ , /portal/, our application for end users and /admin/ for our administrators. We do this, so that every SPA when hosted belongs into its own folder and we can easily make sure that critical code like in our /admin/ SPA is never served outside of intranet. Now to test locally, we use webpack-dev-server's multi project configuration. Every SPA is identifiable by an unique publicPath (e. g. /admin). This worked beautifully with webpack-dev-server, but with webpack 5 and webpack-cli this is no longer the case. We use multiple webpack configurations to test our

We used to use webpack-dev-server to start our development server directly, however as I understand that is no longer working with webpack 5.

What is the current behavior?

Webpack-cli outputs a an error message:

 Error: Unique ports must be specified for each devServer option inyour webpack configuration. Alternatively, run only 1 devServer config using the--config-name flag to specify your desired config.
    at Object.startDevServer [as default] 

To Reproduce

Steps to reproduce the behavior: https://github.com/webpack/webpack-cli/blob/master/test/serve/basic/multi-dev-server.config.js without the explicit ports does show my use case perfectly.

Expected behavior

If publicPaths for configurations are unique, webpack serve should be able to serve them from the same port.

Screenshots

Please paste the results of webpack-cli info here, and mention other relevant information

Additional context

Maybe this was first introduced with https://github.com/webpack/webpack-cli/pull/1649 A possible solution might be to change https://github.com/webpack/webpack-cli/blob/ec3baa84479984921e40cd7113e4ab6a14723a7a/packages/serve/src/startDevServer.ts#L14 to add an additional check for unique publicPaths if ports are not unique.

Thanks a lot, Christian

Christian24 avatar Feb 04 '21 08:02 Christian24

yep, because it is impossible, each compiler should have unique port, it is limitation, you can use multi entry syntax:

{
  entry: {
    pageOne: './src/page-one.js',
    pageTwo: './src/page-two.js'
  }
}

Even more - I don't think it is real can be solved, we don't know your files after compilation, and filename and publicPath and other can be functions so we even predict it.

alexander-akait avatar Feb 04 '21 12:02 alexander-akait

Well, multiple entries wouldn't work in our case, since that wouldn't allow us to specify the correct publicPath per entry, right?

I am a bit confused why this no longer works, since this worked just fine in webpack@4 and I am honestly a bit uncertain as to what our upgrade path can be.

Christian24 avatar Feb 05 '21 08:02 Christian24

@Christian24 Can you provide example with webpack@4? I want to look at this, maybe be I am don't understand you correctly

alexander-akait avatar Feb 05 '21 10:02 alexander-akait

@alexander-akait sure. https://github.com/Christian24/webpack-dev-server-multi-config/tree/main This should give you a very basic example of what we are doing with webpack4 at the moment, but I am unable to recreate using webpack5. If you need additional details, please let me know.

Christian24 avatar Feb 05 '21 13:02 Christian24

@Christian24 hm, works fine with webpack v5 and webpack-cli v4, my command is webpack serve --config ./webpack.config.js, when you set two devServe options, it means you need two dev servers, but in you case you don't need it

alexander-akait avatar Feb 06 '21 15:02 alexander-akait

Here some architecture limitations, because webpack-dev-server should be plugin, and you can apply it for all configurations and for the each configuration, it is on roadmap for webpack-dev-server v4, right now if you need to set port please use webpack serve --config ./path/to/webpack.config.js --port 8081

And here your case - you need apply webpack-dev-server on multi compiler level and set the port option. Right now you can set port using the devServer option for first compiler.

In future we will fix this limitation

alexander-akait avatar Feb 06 '21 15:02 alexander-akait

First of all thank you, @alexander-akait! You are very right that my example works fine with webpack5, I just checked. However, you were also correct that the multiple devServer configurations cause my issue. I always thought they would apply per configuration and not globally.

To everyone running into the same issue I have adapted my initial example into a

  • broken: https://github.com/Christian24/webpack-dev-server-multi-config/tree/broken
  • fixed: https://github.com/Christian24/webpack-dev-server-multi-config/tree/fixed

Is it just me or is the the multi-compiler feature lacking documentation? I would be happy to help if hands are needed with that, since it is an invaluable feature for me.

Christian24 avatar Feb 06 '21 16:02 Christian24

I do not want to reinvent the wheel, but wouldn't a syntax like

{
devServer: ...,
configurations: [...]
}

be nicer since it would make make more explicit what part of the configuration is shared globally?

Christian24 avatar Feb 06 '21 16:02 Christian24

@Christian24 Yep, I think it should be like this, but changing syntax in config files will be a big problem, I need think about it

alexander-akait avatar Feb 06 '21 17:02 alexander-akait

Let's keep open, I want to search right way for this or update docs with the limitation (official)

alexander-akait avatar Feb 06 '21 17:02 alexander-akait

Sure, maybe here: https://webpack.js.org/configuration/configuration-types/#exporting-multiple-configurations? I wouldn't know if I would find it there though. I think the whole topic might need more documentation or is there more? Would it be a good idea to open an issue at https://github.com/webpack/webpack.js.org?

Christian24 avatar Feb 06 '21 17:02 Christian24

I will open an issue in docs repo don't worry, I need time to think how we can make it better

alexander-akait avatar Feb 08 '21 10:02 alexander-akait

@Christian24 @alexander-akait, thanks for your work on this issue!!!!

This same problem just came up for me this week.

@Christian24's detailed notes helped me get our local dev up and running again! 🙏

yowainwright avatar Feb 08 '21 21:02 yowainwright

I think we will do the same https://github.com/webpack/webpack/pull/12671, but for dev server

alexander-akait avatar Feb 14 '21 14:02 alexander-akait

I get this same problem now when I try to upgrade webpack from v4 to v5.

As @Christian24's detail, I have also use the multiple configs: https://webpack.js.org/configuration/configuration-types/#exporting-multiple-configurations, and works well on webpack@4, but get error on v5:

[webpack-cli] Error: Unique ports must be specified for each devServer option in your webpack configuration. Alternatively, run only 1 devServer config using the --config-name flag to specify your desired config.
    at Object.startDevServer [as default] (node_modules/@webpack-cli/serve/lib/startDevServer.js:94:23)
    at Command.<anonymous> (node_modules/@webpack-cli/serve/lib/index.js:93:57)
    at async Promise.all (index 1)

Thanks all of you!

1950195 avatar Mar 07 '21 09:03 1950195

@1950195 that is because you have multiple devServer options. One will work across all configurations

Christian24 avatar Mar 08 '21 20:03 Christian24

@1950195 that is because you have multiple devServer options. One will work across all configurations

Yes, agree. I’m trying to separate the different projects from one webpack config file now. Then may easy to merge some in one...

And also we have performance problems on running the build script, about 50 configs in one config. So it’s the time to separate them.😂

1950195 avatar Mar 09 '21 00:03 1950195

Thanks for your works, this is my script to remove the same devServers configs.

const glob = require('glob');
const util = require('util');

const configs = glob.sync('./plugins/*/webpack.config*.js').map(file => require(file));

if (!process.argv.includes('--config-name')) {
  // Remove same devServers to avoid
  // "Error: Unique ports must be specified for each devServer option in your webpack configuration."
  // @link https://github.com/webpack/webpack-cli/issues/2408
  const devServers = [];
  configs.forEach(config => {
    devServers.forEach(devServer => {
      if (util.isDeepStrictEqual(config.devServer, devServer)) {
        delete config.devServer;
      }
    });
    if (config.devServer) {
      devServers.push(config.devServer);
    }
  });
}

module.exports = configs;

twinh avatar Mar 20 '21 08:03 twinh

We need:

  • add supports for multiple dev server
  • supports devServer: false to disable dev server

alexander-akait avatar Jul 21 '21 20:07 alexander-akait

This issue had no activity for at least half a year.

It's subject to automatic issue closing if there is no activity in the next 15 days.

webpack-bot avatar Jan 20 '22 20:01 webpack-bot

bump

alexander-akait avatar Jan 21 '22 12:01 alexander-akait

Issue was closed because of inactivity.

If you think this is still a valid issue, please file a new issue with additional information.

webpack-bot avatar Aug 07 '22 05:08 webpack-bot

@webpack/cli-team I think we can implement it in better way - --no-dev-server [config-name] (only for webpack serve, so should be implement there), should solve most of problems

alexander-akait avatar Sep 07 '22 20:09 alexander-akait