neutrinojs / neutrino

Create and build modern JavaScript projects with zero initial configuration.
https://neutrinojs.org
Mozilla Public License 2.0
3.95k stars 213 forks source link

start-server plugin should restart server when HMR is off #1074

Closed djmitche closed 8 months ago

djmitche commented 6 years ago

For a node app, HMR is difficult to get right and likely to introduce bugs. It's easier to just stop and restart the service with each change.

An app using the node preset, with hot: false, will currently start the server after the first build, then happily announce "Build completed" on further changes, but won't actually do anything with that new build. That's confusing because the output suggests that it is running the newly-saved code!

It turns out that based on a feature of start-server-webpack-plugin, you can type rs\n and it will kill the old process and start a new one.

Is it possible to automatically kill-and-restart the service every time it is rebuilt?

djmitche commented 6 years ago

start-server-webpack-plugin has a signal option, but experimentally setting that to 'SIGINT' doesn't seem to help: it happily kills the process on HMR change, but doesn't restart it.

The webpack plugin is pretty simple -- maybe we could just reimplement that within the neutrino plugin and add a restartOnRebuild config that kills and restarts the process on build completion?

djmitche commented 6 years ago

I wrote https://github.com/djmitche/neutrino-middleware-restart-server which serves this purpose. So maybe the fix here is to use that plugin instead in the node preset?

eliperelman commented 6 years ago

Oooooooo, this looks sooo nice! @edmorley thoughts on swapping out?

@djmitche thoughts on publishing just the plugin to npm for us to consume?

djmitche commented 6 years ago

It's on npm.. https://www.npmjs.com/package/neutrino-middleware-restart-server

eliperelman commented 6 years ago

@djmitche I mean just the plugin, not the middleware portion.

djmitche commented 6 years ago

Ah, so break up the webpack plugin and the neutrino middleware?

eliperelman commented 6 years ago

@djmitche yep! The monorepo doesn't rely on any external middleware, but we can rely on external plugins.

djmitche commented 6 years ago

Although, if we do that, maybe we should at least start by asking the start-server author if they are willing to support restart as well.

djmitche commented 6 years ago

https://github.com/ericclemmons/start-server-webpack-plugin/issues/28

djmitche commented 6 years ago

Looks like there's a decent chance we can get start-server to behave this way, which would save a lot of trouble :)

constgen commented 6 years ago

Guys, lets separate "restart a server" and "restart a process". Restarting of a process may brake the expirience for those developers who already successfully setup HMR on their NodeJS projects. So please don't include it to @neutrino/node as many projects already depend on it and this is not what expected. @djmitche Take look https://www.npmjs.com/package/neutrino-preset-koa. Probably this may be the preset you are looking for.

djmitche commented 6 years ago

I don't know what Koa is (and it has no README to tell me), but I'm not using it, so that doesn't seem like the right choice.

This would always be an optional behavior, both in the webpack plugin and in the neutrino middleware that enables it. Existing behavior should not change.

You suggest a distinction between "restart a server" and "restart a process" but you don't actually describe the difference. What do you mean?

Finally, we're not all male, so let's not use words like "guys" -- thanks.

constgen commented 6 years ago

Not all NodeJS projects are servers. I mean they don't start HTTP endpoints on any host and port. So you can use NodeJS platform for different kind of applications that require different Webpack setup (say different Neutrino presets). @neutrino/node is for general NodeJS projects like libraries, midlewares, CLI tools, others. What you are asking requires extra setup for such frameworks as Express, Koa, Hapi that start servers on HTTP protocol. What is needed here is to restart a server instance during HMR but not the process.

Koa is an Express competitor. It is newer and developed with the same people. They just had such breaking ideas that decided to split it to a different framework. It should be easy to everyone to quickly jump into Koa if he/she already knows Express. There is its documentation https://koajs.com/

djmitche commented 6 years ago

Yes, the use-case here is the not-a-server use-case you mention, so I think we agree?

constgen commented 6 years ago

Which point agree with? I still see that HMR is enough for general purposes. Restarting in this case is just an unnecessary step. All changed modules and dependency trees will be relaunched if you setup everything correctly in index.js. Lets discuss your particular example.

djmitche commented 6 years ago

I've already stated that HMR is not a solution for me. Stating that it is and therefore HMR is enough is not helping.

constgen commented 6 years ago

For edge cases I think we need another preset as a wrapper around @neutrino/node. This what I did for Koa framework. You can do the same for your case incapsulating only relstart functionality in your preset and reusing @neutrino/node.