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

2021/04/09 - webpack热更新流程 #74

Open
lxinr opened this issue Apr 9, 2021 · 0 comments
Open

2021/04/09 - webpack热更新流程 #74

lxinr opened this issue Apr 9, 2021 · 0 comments

Comments

@lxinr
Copy link
Owner

lxinr commented Apr 9, 2021

  1. Webpack的配置中添加 HotModuleReplacementPlugin 插件
  2. entry 注入热更新代码,用于保证服务端与客户端之间能够通信
webpack-hot-middleware/client?path=http://127.0.0.1:9000/__webpack_hmr&noInfo=false&reload=true&quiet=false 
  1. 客户端首次打开后,服务端与客户端通过WebScoket建立通信(webpack-hot-middleware使用的是window.EventSource
  2. 调用webpack的api对文件系统进行watch,其本质就是nodefs模块的watch,当有文件修改时,webpack就会对文件进行重新编译打包,然后保存到内存中,用到的库是memory-fs
  3. devServer通知浏览器端文件发生了改变,并调用 webpack api 监听 compiledone事件,当compile完成后将编译打包后的新模块hash值发送到浏览器端
  4. 浏览器端接收到服务端消息做出响应,一般会先接收一个内容为hash的响应来告知是哪个hash的模块,待接收到ok时执行reload操作
  5. webpack接收到最新hash值验证并请求模块代码
  6. HotModuleReplacement.runtime对模块进行热更新,具体包括模块匹配,模块替换等操作
  7. 最后调用module.hot.accept()完成热更新

image

参考:
Advanced-Frontend/Daily-Interview-Question#118

https://zhuanlan.zhihu.com/p/30669007

https://zhuanlan.zhihu.com/p/30623057

@lxinr lxinr changed the title 2021/04/09 - webpack热更新原理 2021/04/09 - webpack热更新原理 (待完成) Apr 9, 2021
@lxinr lxinr changed the title 2021/04/09 - webpack热更新原理 (待完成) 2021/04/09 - webpack热更新流程 Apr 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant