webpack / webpack-cli

Webpack's Command Line Interface
https://webpack.js.org/api/cli
MIT License
2.57k stars 601 forks source link

serve does not work with multi-server #2408

Open Christian24 opened 3 years ago

Christian24 commented 3 years ago

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

alexander-akait commented 3 years ago

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.

Christian24 commented 3 years ago

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.

alexander-akait commented 3 years ago

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

Christian24 commented 3 years ago

@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.

alexander-akait commented 3 years ago

@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 commented 3 years ago

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

Christian24 commented 3 years ago

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

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 commented 3 years ago

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?

alexander-akait commented 3 years ago

@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 commented 3 years ago

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

Christian24 commented 3 years ago

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?

alexander-akait commented 3 years ago

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

yowainwright commented 3 years ago

@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! 🙏

alexander-akait commented 3 years ago

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

1950195 commented 3 years ago

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!

Christian24 commented 3 years ago

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

1950195 commented 3 years ago

@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.😂

twinh commented 3 years ago

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;
alexander-akait commented 3 years ago

We need:

webpack-bot commented 2 years ago

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.

alexander-akait commented 2 years ago

bump

webpack-bot commented 2 years ago

Issue was closed because of inactivity.

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

alexander-akait commented 2 years ago

@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

evenstensberg commented 4 months ago

@alexander-akait any update on this?

alexander-akait commented 4 months ago

No, we need implement plugin support for devserver