经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » TypeScript » 查看文章
websocket4.0+typescript 实现热更新的方法
来源:jb51  时间:2019/8/14 10:22:31  对本文有异议

最近搞了一个webpack4+typescript的开发环境,折腾了很久现在记录一下。。。。

本身环境比较好搞,但是热更新是个麻烦事儿

本环境是基于webpack-dev-server搭建的

  1. output: {
  2. publicPath: '/dist',
  3. path: path.resolve(__dirname, 'dist'),
  4. filename: 'ljax.bundle.js',
  5. hotUpdateChunkFilename: 'hot/hot-update.js',
  6. hotUpdateMainFilename: 'hot/hot-update.json'
  7. },

publicPath是必须的字段,不添加HRM就没有效果

在热更新的时候会出现很多hot-update.js和hot-update.json的细碎文件

使用hotUpdateChunkFilename和hotUpdateMainFilename指定他们只生成一个文件,目前没有找到不生成这两个文件的办法,如果哪位大佬知道的话请告知。

  1. plugins: [
  2. new HtmlWebpackPlugin({
  3. title: '模块热替换',
  4. template: './public/index.html'
  5. }),
  6. new webpack.HotModuleReplacementPlugin(),
  7. // 启动输出清理
  8. new FriendlyErrorsWebpackPlugin({
  9. compilationSuccessInfo: {
  10. messages: [`You application is running here ${HTTPS ? 'https' : 'http'}://${HOST}:${PORT}`],
  11. // notes: ['Some additional notes to be displayed upon successful compilation'],
  12. clearConsole: true
  13. },
  14. })
  15. ],

HotModuleReplacementPlugin是热更新必不可少的插件

  1. contentBase: __dirname,
  2. quiet: true,
  3. compress: true,
  4. port: PORT,
  5. host: HOST,
  6. https: HTTPS,
  7. // hot: true,
  8. // hotOnly: true,
  9. // inline: true,
  10. open: true,
  11. overlay: true,
  12. openPage: './dist/index.html'

最坑的地方来了,我一开始是加上可hot和hotOnly字段的,但是不管是两个都加还是单独使用任何一个,HRM都没有效果。。。。

最后发现两个都不实用就可以。。。。。妈耶,我还是从官网看的这个配置。

到现在也不是很了解是怎么回事。。

最后,贴一下完整的配置吧

webpack.config.js

  1. const path = require('path')
  2. const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin')
  3. const webpack = require('webpack')
  4. const HtmlWebpackPlugin = require('html-webpack-plugin')
  5.  
  6. const HOST = 'localhost'
  7. const PORT = 8080
  8. const HTTPS = false
  9.  
  10. module.exports = {
  11. mode: 'development',
  12.  
  13. context: __dirname,
  14.  
  15. entry: {
  16. app: './src/index.ts'
  17. },
  18.  
  19. output: {
  20. publicPath: '/dist',
  21. path: path.resolve(__dirname, 'dist'),
  22. filename: 'ljax.bundle.js',
  23. hotUpdateChunkFilename: 'hot/hot-update.js',
  24. hotUpdateMainFilename: 'hot/hot-update.json'
  25. },
  26.  
  27. module: {
  28. rules: [
  29. { test: /\.ts/, use: 'ts-loader', exclude: /node_modules/ }
  30. ]
  31. },
  32.  
  33. resolve: {
  34. extensions: ['.ts', '.js']
  35. },
  36.  
  37. plugins: [
  38. new HtmlWebpackPlugin({
  39. title: '模块热替换',
  40. template: './public/index.html'
  41. }),
  42. new webpack.HotModuleReplacementPlugin(),
  43. // 启动输出清理
  44. new FriendlyErrorsWebpackPlugin({
  45. compilationSuccessInfo: {
  46. messages: [`You application is running here ${HTTPS ? 'https' : 'http'}://${HOST}:${PORT}`],
  47. // notes: ['Some additional notes to be displayed upon successful compilation'],
  48. clearConsole: true
  49. },
  50. })
  51. ],
  52.  
  53. devServer: {
  54. contentBase: __dirname,
  55. quiet: true,
  56. compress: true,
  57. port: PORT,
  58. host: HOST,
  59. https: HTTPS,
  60. // hot: true,
  61. // hotOnly: true,
  62. // inline: true,
  63. open: true,
  64. overlay: true,
  65. openPage: './dist/index.html'
  66. }
  67. }
  68.  

package.json

  1. {
  2. "name": "ljax",
  3. "version": "1.0.0",
  4. "description": "",
  5. "main": "index.js",
  6. "scripts": {
  7. "test": "echo \"Error: no test specified\" && exit 1",
  8. "watch": "webpack -w",
  9. "dev-server": "webpack-dev-server",
  10. "serve": "start yarn dev-server && yarn watch"
  11. },
  12. "author": "",
  13. "license": "ISC",
  14. "dependencies": {
  15. "friendly-errors-webpack-plugin": "^1.7.0",
  16. "html-webpack-plugin": "^3.2.0",
  17. "ts-loader": "^6.0.4",
  18. "typescript": "^3.5.3",
  19. "webpack": "^4.39.1",
  20. "webpack-dev-server": "^3.7.2"
  21. },
  22. "devDependencies": {
  23. "webpack-cli": "^3.3.6"
  24. }
  25. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号