简体   繁体   English

webpack - 服务器上的热模块重新加载

[英]webpack - hot module reload on the server

Webpack hot reloading (webpack-hot-middleware) works wonderfully for the client; Webpack热重载(webpack-hot-middleware)非常适合客户端; it rebuilds and updates assets on the client whenever a file is changed. 每当文件发生更改时,它都会在客户端上重建和更新资产。 But for universal/isomorphic servers, where the server needs to prerender the HTML response, this is very difficult. 但对于通用/同构服务器,服务器需要预呈现HTML响应,这非常困难。

The naive solution is to restart (eg with nodemon) the server whenever a file is changed, but this closes all client connections, and for large servers, this is very slow. 天真的解决方案是每当文件被更改时重启(例如使用nodemon)服务器,但这会关闭所有客户端连接,而对于大型服务器,这非常慢。

A slightly better solution is to manually watch assets (eg with chokidar), and clear cache/require again once a file is changed. 稍微好一点的解决方案是手动监视资产(例如使用chokidar),并在文件更改后再次清除缓存/需求。 But this requires additional complexity when the dependencies also need to be watched; 但是,当需要监视依赖关系时,这需要额外的复杂性; files must be parsed to determine what they require. 必须解析文件以确定它们需要什么。

Furthermore, if the codebase is written in a compile-to-js language, it is best to run a compiled server for production (no more babel-node). 此外,如果代码库是用compile-to-js语言编写的,那么最好运行一个编译服务器进行生产(不再是babel-node)。 With a compiled server, it is no longer possible to use the mechanism described above, because: 使用已编译的服务器,不再可能使用上述机制,因为:

  • webpack has poor support for dynamic requires: require(variable) as opposed to require('./file.js') webpack对动态需求的支持很差: require(variable)而不是require('./file.js')
  • node cannot natively require the code 节点本身不能要求代码

In my case, I've abstracted the require function into a package that uses a babel-register ed require (previously I used the babel API, but it relied on a lot of undocumented node source). 在我的例子中,我已经将require函数抽象为一个使用babel-register ed require的包(之前我使用过babel API,但它依赖于很多未记录的节点源)。

This is the solution I currently use in https://github.com/edge/cyc , which works somewhat, but it is haphazard and has plenty of caveats. 这是我目前在https://github.com/edge/cyc中使用的解决方案,它在某种程度上有效,但它是偶然的并且有很多警告。 In general, the more custom code written parallel to webpack, the further one departs from the desired behavior. 通常,与webpack并行编写的自定义代码越多,进一步偏离了所需的行为。

Is there a robust way to more easily duplicate webpack's behavior? 有没有一种强大的方法可以更轻松地复制webpack的行为?

I have written a package which may help a bit. 我写了一个可能有所帮助的软件包 You can use if (module.hot) {...} in your server codes. 您可以在服务器代码中使用if (module.hot) {...}

In brief, in a webpack config, this function create a server as a forked process. 简而言之,在webpack配置中,此函数将服务器创建为分叉进程。 If source files change, webpack re-compile and send a signal to the child process. 如果源文件发生更改,webpack将重新编译并向子进程发送信号。 In your server codes, you can include if (module.hot) {... module.hot.accept(...) ...} to accept or reject the update. 在您的服务器代码中,您可以包含if (module.hot) {... module.hot.accept(...) ...}来接受或拒绝更新。

If you accept the update, however, just remember to remove all possible side effects. 但是,如果您接受更新,请记住删除所有可能的副作用。 If rejecting, the server will be restarted again. 如果拒绝,将再次重新启动服务器。

I am still learning... hope this helps 我还在学习...希望这会有所帮助

I recently put out webpack-hot-server-middleware which is designed to be used alongside webpack-dev-middleware (and optionally webpack-hot-middleware ). 我最近推出了webpack-hot-server-middleware ,它与webpack-dev-middleware (以及webpack-hot-middleware )一起使用。

It takes a very simple approach allowing you to hot update your server rendering bundle without having to restart the process or do anything particularly special with your code. 它采用一种非常简单的方法,允许您热更新服务器渲染包,而无需重新启动进程或对代码执行任何特别的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM