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

listeningApp is defined late and cannot be used in before/after functions #1569

Closed
1 of 2 tasks
cinderblock opened this issue Nov 12, 2018 · 1 comment · Fixed by #1930
Closed
1 of 2 tasks

listeningApp is defined late and cannot be used in before/after functions #1569

cinderblock opened this issue Nov 12, 2018 · 1 comment · Fixed by #1930

Comments

@cinderblock
Copy link

  • Operating System: Windows 10
  • Node Version: v8.11.2
  • NPM Version: 5.6.0
  • webpack Version: 4.25.1
  • webpack-dev-server Version: 3.1.10
  • This is a bug
  • This is a modification request

Code

Fails: Cannot read property 'address' of undefined

// webpack.config.js

module.exports = {
  devServer: {
    port: 9001,
    host: '0.0.0.0',
    before(app, server) {
      const port = server.listeningApp.address().port;

      console.log('Listening on port:', port);
    },
  },
  // ...
};

Yes, in this simplistic example, I recognize that I could hard code or add a variable port that is shared between the devServer configuration object and my before function. However I'm trying to support other cases where this is not practical.

Problem

listeningApp is set after before function is executed.

#476 Almost addressed this, however creating the server instance is not controllable in the features list and is always done last.

I think it would be a relatively small change in the startup sequence to add creating the server to the features list and allowing it to be controlled. I don't see how it would affect other parts of the code. If this change is undesirable, I still have a workaround that works for me but I bet others might like this change aswell.

Workaround

Use setImmediate() to delay execution inside before function. This happens to work for my particular use case but this introduces other annoyances.

// webpack.config.js

module.exports = {
  devServer: {
    port: 9001,
    host: '0.0.0.0',
    before(app, server) {
      setImmediate(() => {
        const port = server.listeningApp.address().port;

        console.log('Listening on port:', port);
      });
    },
  },
  // ...
};
@alexander-akait
Copy link
Member

alexander-akait commented Apr 9, 2019

We need implement onListening option for you case, feel free to send a PR

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

Successfully merging a pull request may close this issue.

2 participants