您好,登錄后才能下訂單哦!
這篇文章給大家介紹怎么在websocket4.0中使用typescript 實現熱更新,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
本環境是基于webpack-dev-server搭建的
output: { publicPath: '/dist', path: path.resolve(__dirname, 'dist'), filename: 'ljax.bundle.js', hotUpdateChunkFilename: 'hot/hot-update.js', hotUpdateMainFilename: 'hot/hot-update.json' },
publicPath是必須的字段,不添加HRM就沒有效果
在熱更新的時候會出現很多hot-update.js和hot-update.json的細碎文件
使用hotUpdateChunkFilename和hotUpdateMainFilename指定他們只生成一個文件,目前沒有找到不生成這兩個文件的辦法,如果哪位大佬知道的話請告知。
plugins: [ new HtmlWebpackPlugin({ title: '模塊熱替換', template: './public/index.html' }), new webpack.HotModuleReplacementPlugin(), // 啟動輸出清理 new FriendlyErrorsWebpackPlugin({ compilationSuccessInfo: { messages: [`You application is running here ${HTTPS ? 'https' : 'http'}://${HOST}:${PORT}`], // notes: ['Some additional notes to be displayed upon successful compilation'], clearConsole: true }, }) ],
HotModuleReplacementPlugin是熱更新必不可少的插件
contentBase: __dirname, quiet: true, compress: true, port: PORT, host: HOST, https: HTTPS, // hot: true, // hotOnly: true, // inline: true, open: true, overlay: true, openPage: './dist/index.html'
最坑的地方來了,我一開始是加上可hot和hotOnly字段的,但是不管是兩個都加還是單獨使用任何一個,HRM都沒有效果。。。。
最后發現兩個都不實用就可以。。。。。媽耶,我還是從官網看的這個配置。
到現在也不是很了解是怎么回事。。
最后,貼一下完整的配置吧
webpack.config.js
const path = require('path') const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin') const webpack = require('webpack') const HtmlWebpackPlugin = require('html-webpack-plugin') const HOST = 'localhost' const PORT = 8080 const HTTPS = false module.exports = { mode: 'development', context: __dirname, entry: { app: './src/index.ts' }, output: { publicPath: '/dist', path: path.resolve(__dirname, 'dist'), filename: 'ljax.bundle.js', hotUpdateChunkFilename: 'hot/hot-update.js', hotUpdateMainFilename: 'hot/hot-update.json' }, module: { rules: [ { test: /\.ts/, use: 'ts-loader', exclude: /node_modules/ } ] }, resolve: { extensions: ['.ts', '.js'] }, plugins: [ new HtmlWebpackPlugin({ title: '模塊熱替換', template: './public/index.html' }), new webpack.HotModuleReplacementPlugin(), // 啟動輸出清理 new FriendlyErrorsWebpackPlugin({ compilationSuccessInfo: { messages: [`You application is running here ${HTTPS ? 'https' : 'http'}://${HOST}:${PORT}`], // notes: ['Some additional notes to be displayed upon successful compilation'], clearConsole: true }, }) ], devServer: { contentBase: __dirname, quiet: true, compress: true, port: PORT, host: HOST, https: HTTPS, // hot: true, // hotOnly: true, // inline: true, open: true, overlay: true, openPage: './dist/index.html' } }
package.json
{ "name": "ljax", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "watch": "webpack -w", "dev-server": "webpack-dev-server", "serve": "start yarn dev-server && yarn watch" }, "author": "", "license": "ISC", "dependencies": { "friendly-errors-webpack-plugin": "^1.7.0", "html-webpack-plugin": "^3.2.0", "ts-loader": "^6.0.4", "typescript": "^3.5.3", "webpack": "^4.39.1", "webpack-dev-server": "^3.7.2" }, "devDependencies": { "webpack-cli": "^3.3.6" } }
關于怎么在websocket4.0中使用typescript 實現熱更新就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。