feat: support hot reload preload script
This commit is contained in:
1 parent
d50f500bbd
commit
91384d9f37
4 files changed
+102
-10
No files matched your search
@@ -13,3 +13,37 @@ export function domReady(condition: DocumentReadyState[] = ['complete', 'interac
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/** Inject ws related code */
|
||||
export function injectWsCode(options: {
|
||||
host: string
|
||||
port: string | number
|
||||
}) {
|
||||
const oScript = document.createElement('script')
|
||||
oScript.id = 'ws-preload-hot-reload'
|
||||
|
||||
oScript.innerHTML = `
|
||||
${__ws_hot_reload_for_preload.toString()}
|
||||
${__ws_hot_reload_for_preload.name}(${JSON.stringify(options)})
|
||||
`
|
||||
|
||||
document.body.appendChild(oScript)
|
||||
}
|
||||
|
||||
function __ws_hot_reload_for_preload(options: { host: string; port: string | number }) {
|
||||
const ws = new WebSocket(`ws://${options.host}:${options.port}`)
|
||||
ws.onmessage = function (ev) {
|
||||
try {
|
||||
console.log('[preload] ws.onmessage:', ev.data)
|
||||
|
||||
const data = JSON.parse(ev.data) // { "cmd": "string", data: "string|number" }
|
||||
|
||||
if (data.cmd === 'reload') {
|
||||
setTimeout(() => window.location.reload(), 999)
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn(`ws.onmessage should be accept "JSON.string" formatted string.`)
|
||||
console.error(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user