websocket4.0+typescript 實(shí)現(xiàn)熱更新的方法
最近搞了一個(gè)webpack4+typescript的開(kāi)發(fā)環(huán)境,折騰了很久現(xiàn)在記錄一下。。。。
本身環(huán)境比較好搞,但是熱更新是個(gè)麻煩事兒
本環(huán)境是基于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就沒(méi)有效果
在熱更新的時(shí)候會(huì)出現(xiàn)很多hot-update.js和hot-update.json的細(xì)碎文件
使用hotUpdateChunkFilename和hotUpdateMainFilename指定他們只生成一個(gè)文件,目前沒(méi)有找到不生成這兩個(gè)文件的辦法,如果哪位大佬知道的話請(qǐng)告知。
plugins: [
new HtmlWebpackPlugin({
title: '模塊熱替換',
template: './public/index.html'
}),
new webpack.HotModuleReplacementPlugin(),
// 啟動(dòng)輸出清理
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'
最坑的地方來(lái)了,我一開(kāi)始是加上可hot和hotOnly字段的,但是不管是兩個(gè)都加還是單獨(dú)使用任何一個(gè),HRM都沒(méi)有效果。。。。
最后發(fā)現(xiàn)兩個(gè)都不實(shí)用就可以。。。。。媽耶,我還是從官網(wǎng)看的這個(gè)配置。
到現(xiàn)在也不是很了解是怎么回事。。
最后,貼一下完整的配置吧
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(),
// 啟動(dòng)輸出清理
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"
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
微信小程序開(kāi)發(fā)數(shù)據(jù)緩存基礎(chǔ)知識(shí)辨析及運(yùn)用實(shí)例詳解
這篇文章主要介紹了微信小程序開(kāi)發(fā)數(shù)據(jù)緩存基礎(chǔ)知識(shí)辨析及運(yùn)用實(shí)例詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
10個(gè)在JavaScript開(kāi)發(fā)中常遇到的BUG
給大家詳細(xì)著整理了在JavaScript開(kāi)發(fā)中大家經(jīng)常遇到的BUG和問(wèn)題,需要的朋友參考學(xué)習(xí)下吧。2017-12-12
Javascript 顏色漸變效果的實(shí)現(xiàn)代碼
在搭建博主博客的時(shí)候,尋思著做一些效果,看到菜單,就想是不是可以做一下顏色的漸變,增加一點(diǎn)動(dòng)態(tài)的感覺(jué)。有個(gè)jquery的插件,效果相當(dāng)不錯(cuò),不過(guò)博主還是打算自立更生寫一下,看看能不能實(shí)現(xiàn)2013-10-10
javascript實(shí)現(xiàn)的鼠標(biāo)鏈接提示效果生成器代碼
javascript實(shí)現(xiàn)的鼠標(biāo)鏈接提示效果生成器代碼...2007-06-06
簡(jiǎn)單實(shí)現(xiàn)bootstrap導(dǎo)航效果
這篇文章主要為大家詳細(xì)介紹了如何簡(jiǎn)單實(shí)現(xiàn)bootstrap導(dǎo)航效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
Javascript removeChild()刪除節(jié)點(diǎn)及刪除子節(jié)點(diǎn)的方法
這篇文章主要介紹了Javascript removeChild()刪除節(jié)點(diǎn)及刪除子節(jié)點(diǎn)的方法的相關(guān)資料,需要的朋友可以參考下2015-12-12
noscript 標(biāo)簽 一個(gè)被忽視的重要標(biāo)簽
這篇文章主要介紹了noscript 標(biāo)簽 一個(gè)被忽視的重要標(biāo)簽,需要的朋友可以參考下2023-03-03
javascript當(dāng)中的代碼嗅探擴(kuò)展原生對(duì)象和原型(prototype)
如果不是有特殊需要而去擴(kuò)展原生對(duì)象和原型(prototype)的做法是不好的,除非這樣做是值得的,例如,向一些舊的瀏覽器中添加一些ECMAScript5中的方法2013-01-01
js復(fù)制文本到粘貼板(Clipboard.writeText())
這篇文章主要介紹了js復(fù)制文本到粘貼板(Clipboard.writeText()),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07

