Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webpack compiler cannot be reused across server instances #3033

Closed
1 of 2 tasks
ylemkimon opened this issue Feb 19, 2021 · 4 comments · Fixed by #3314
Closed
1 of 2 tasks

webpack compiler cannot be reused across server instances #3033

ylemkimon opened this issue Feb 19, 2021 · 4 comments · Fixed by #3314

Comments

@ylemkimon
Copy link
Contributor

ylemkimon commented Feb 19, 2021

  • Operating System: Ubuntu
  • Node Version: 12.19.0
  • NPM Version: 6.14.8
  • webpack Version: 5.23.0
  • webpack-dev-server Version: 4.0.0-beta.0
  • Browser: Chrome 86.0.4240.193
  • This is a bug
  • This is a modification request

Code

Reproducible repo: https://github.com/ylemkimon/multiple-dev-server-instance

const webpack = require('webpack');
const Server = require('webpack-dev-server');
const config = require('./webpack.config');

const compiler = webpack(config);

const server1 = new Server(compiler, {hot: true});
server1.listen(port, host);

// after server 1 is closed
const server2 = new Server(compiler, {hot: false}); // note the hot is disabled
server2.listen(port, host);

Expected Behavior

In the second server:

  1. the compilation log is printed only once
  2. the hot entry should not be included (entries should be updated)

Actual Behavior

In the second server:

  1. the compilation log is printed twice:
asset main.js 218 KiB [emitted] (name: main)
runtime modules 432 bytes 3 modules
cacheable modules 193 KiB
  modules by path ./node_modules/webpack-dev-server/client/ 71 KiB 12 modules
  modules by path ./node_modules/html-entities/lib/*.js 61 KiB 5 modules
  modules by path ./node_modules/url/ 37.4 KiB 3 modules
  modules by path ./node_modules/querystring/*.js 4.51 KiB 3 modules
  modules by path ./node_modules/webpack/hot/*.js 1.42 KiB
    ./node_modules/webpack/hot/emitter.js 75 bytes [built] [code generated]
    ./node_modules/webpack/hot/log.js 1.34 KiB [built] [code generated]
  ./foo.js 36 bytes [built] [code generated]
  ./node_modules/ansi-html/index.js 4.16 KiB [built] [code generated]
  ./node_modules/events/events.js 13.8 KiB [built] [code generated]
./node_modules/webpack/hot/ sync nonrecursive ^\.\/log$ 170 bytes [built] [code generated]
webpack 5.23.0 compiled successfully in 79 ms
asset main.js 218 KiB [emitted] (name: main)
runtime modules 432 bytes 3 modules
cacheable modules 193 KiB
  modules by path ./node_modules/webpack-dev-server/client/ 71 KiB 12 modules
  modules by path ./node_modules/html-entities/lib/*.js 61 KiB 5 modules
  modules by path ./node_modules/url/ 37.4 KiB 3 modules
  modules by path ./node_modules/querystring/*.js 4.51 KiB 3 modules
  modules by path ./node_modules/webpack/hot/*.js 1.42 KiB
    ./node_modules/webpack/hot/emitter.js 75 bytes [built] [code generated]
    ./node_modules/webpack/hot/log.js 1.34 KiB [built] [code generated]
  ./foo.js 36 bytes [built] [code generated]
  ./node_modules/ansi-html/index.js 4.16 KiB [built] [code generated]
  ./node_modules/events/events.js 13.8 KiB [built] [code generated]
./node_modules/webpack/hot/ sync nonrecursive ^\.\/log$ 170 bytes [built] [code generated]
webpack 5.23.0 compiled successfully in 79 ms
2. the hot entry is still included (entries are not updated)

For Bugs; How can we reproduce the behavior?

git clone https://github.com/ylemkimon/multiple-dev-server-instance.git
cd multiple-dev-server-instance
npm install
npm start

Additional information

This is because webpack hooks cannot be modified or removed after they've been added. So hooks of the middleware and entry are added twice and/or not updated:

new EntryPlugin(compiler.context, additionalEntry, {
// eslint-disable-next-line no-undefined
name: undefined,
}).apply(compiler);

I think this can be resolved in two ways:

  1. make hooks disableable or modifiable
  2. disallow a compiler instance to be used across dev-server instances
@alexander-akait
Copy link
Member

It is limitation, you need to create two compiler for this case

@alexander-akait
Copy link
Member

It is not safe to use compiler in two places, many internals changed due watching, so you potential can have problems

@ylemkimon
Copy link
Contributor Author

ylemkimon commented Feb 19, 2021

@alexander-akait Thank you for the quick reply. This was to elaborate on #2920 (review).

The related discussion was #2723 (review) and it seems this has worked before and I remember there was a test case to test this.

I agree it'd be hard to implement and maintain the ability to reuse it. What do you think about raising an error if the compiler instance is reused, i.e., Server is created with the compiler which server has already been added to?

@alexander-akait
Copy link
Member

I agree it'd be hard to implement and maintain the ability to reuse it. What do you think about raising an error if the compiler instance is reused, i.e., Server is created with the compiler which server has already been added to?

Sounds good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants