{"id":19030760,"url":"https://github.com/liueng/webpack-server","last_synced_at":"2026-05-13T21:35:38.896Z","repository":{"id":194223862,"uuid":"248004525","full_name":"LIUeng/webpack-server","owner":"LIUeng","description":"about a hot reload example","archived":false,"fork":false,"pushed_at":"2020-04-22T08:36:59.000Z","size":17,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-21T20:17:42.660Z","etag":null,"topics":["hot-reload","webpack","webpack-dev-server"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LIUeng.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2020-03-17T15:24:51.000Z","updated_at":"2020-04-22T08:37:02.000Z","dependencies_parsed_at":"2023-09-12T11:55:00.165Z","dependency_job_id":null,"html_url":"https://github.com/LIUeng/webpack-server","commit_stats":null,"previous_names":["liueng/webpack-server"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/LIUeng/webpack-server","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LIUeng%2Fwebpack-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LIUeng%2Fwebpack-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LIUeng%2Fwebpack-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LIUeng%2Fwebpack-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LIUeng","download_url":"https://codeload.github.com/LIUeng/webpack-server/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LIUeng%2Fwebpack-server/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33001276,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-13T13:14:54.681Z","status":"ssl_error","status_checked_at":"2026-05-13T13:14:51.610Z","response_time":115,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hot-reload","webpack","webpack-dev-server"],"created_at":"2024-11-08T21:19:25.777Z","updated_at":"2026-05-13T21:35:38.878Z","avatar_url":"https://github.com/LIUeng.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003e如何实现一个简易热更新\u003c/h1\u003e\n\n使用 Webpack + Nodejs(Express) 实现热更新\n\n[项目地址](https://github.com/LIUeng/webpack-server)\n\n## 实现思考？\n\n保存代码，webpack 监听编译代码，编译完成，通知浏览器更新页面（抛出想法，解决问题）\n\n### 思考一\n\n***项目利用 Nodejs(Http) + Express 启动一个端口服务作为服务端***\n\n#### `解答`\n\n```js\n\n// 项目目录 servers/Server.js\n// 启动端口\nlet app = new express();\nlet server = http.createServer(app);\nserver.listen(port, hostname, (err) =\u003e {})\n\n```\n\n### 思考二\n\nwebpack 监听文件变化以及编译\n\n#### `解答`\n\n```js\n\n// 项目目录 servers/Server.js\n// 这里 express 借助 webpack-dev-middware 中间件\nlet app = new express();\nlet middleware = webpackDevMiddleware(compiler, {\n    logLevel: 'error', // 编译出错时打印日志 log\n});\napp.use(middleware);\n\n```\n\n* webpack-dev-middware 中间件作用\n\n- 启动 webpack compiler watch [监听入口文件的变化 实时编译]\n    \n    ```js\n    let compiler = webpack(config);\n    compiler.watch({}, (err, stats) =\u003e {});\n    ```\n\n- 作为 express 的中间件\n\n    - webpack-dev-middleware 利用 memory-fs 进行文件的记忆性生成 [实际上已经生成,没看到而已,可以配置writeToDisk] main.js\n\n    - 访问 http://localhost:xxxx 时获取资源文件 [webpack=\u003eoutput=\u003efilename] main.js\n\n- 配合 html-webpack-plugin 插件\n\n    - /public/index.html 模板文件 -\u003e 生成访问的html文件\n\n    ```html\n    \u003cbody\u003e\n        \u003cdiv id=\"app\"\u003e\u003c/div\u003e\n        \u003cscript src=\"main.js\"\u003e\u003c/script\u003e\n    \u003c/body\u003e\n    ```\n\n### 思考三\n\n实现双方通信 websocket [浏览器端][服务端]\n\n#### `解答`\n\n[参考 sockjs-client](https://github.com/sockjs/sockjs-client)\n\n[参考 sockjs-node](https://github.com/sockjs/sockjs-node)\n\n- 浏览器端(利用 sockjs-client 创建 websocket)\n\n    ```js\n\n    // 参考目录 clients/index.js\n    // url: http://localhost:xxxx\n    let clients = new SockJsClient(url);\n\n    // events: onopen onclose onmessage\n\n    ```\n\n- 服务端(利用 sockjs-node 创建 socket 服务)\n\n    ```js\n\n    // 参考目录 servers/Server.js\n    // createSocketServer\n    let socketServer = new sockjs();\n    socketServer.createServer({});\n    socketServer.installHandlers(app, {\n        prefix: '/xxxx', // 这里是你要创建的 socket 服务地址\n    });\n\n    // events: send onconnection onclose\n\n    ```\n\n- 如何通信并刷新浏览器\n\n    利用 webpack hooks 监听文件编译完成\n\n    - ⚔ 服务端\n\n    ```js\n    compiler.hooks.done.tap('dev-server', function() {\n        // 这里通过服务端 socket server 发送一组信号 通知浏览器端 onmessage\n    })\n    ```\n\n    - ⚔ 浏览器端\n\n        - 这里需要把浏览器端需要创建的 websocket 代码引入到 index.html 中\n\n        - 所以这里我们把 clients/index.js 和 项目入口 ./src/index.js 共同加入到 webpack 的 entry 中\n\n        - 一起打包到 webpack output 文件中\n\n        - 参考文件 webpack.config.js\n\n    - ⚔ 刷新页面\n\n        - socekt server 发送信号 浏览器已经创建了 websocket 建立连接\n    \n        - 收到信号 浏览器利用自身的 `window` 对象进行刷新 window.location.reload()\n\n    - ⚔ 结束\n\n        - 完成一次通信\n\n\n## 环境准备\n\n安装所需要的依赖包\n\n- webpack\n- html-webpack-plugin\n- sockjs\n- sockjs-client\n- express\n\n## 目录搭建\n\n- clients 客户端\n- public 模板html\n- servers 服务端\n- src 页面目录\n- scripts 启动项目脚本目录\n\n## 项目启动\n\n```bash\nnpm run start\n\nor\n\nyarn start\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliueng%2Fwebpack-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fliueng%2Fwebpack-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliueng%2Fwebpack-server/lists"}