-
-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feat] Support content.css HMR (#799)
Co-authored-by: Jonghakseo <[email protected]>
- Loading branch information
1 parent
ca9a822
commit a1ddaa3
Showing
5 changed files
with
80 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* eslint-disable */ | ||
(function () { | ||
'use strict'; | ||
// This is the custom ID for HMR (chrome-extension/vite.config.mts) | ||
const __HMR_ID = 'chrome-extension-hmr'; | ||
|
||
const LOCAL_RELOAD_SOCKET_PORT = 8081; | ||
const LOCAL_RELOAD_SOCKET_URL = `ws://localhost:${LOCAL_RELOAD_SOCKET_PORT}`; | ||
|
||
const DO_UPDATE = 'do_update'; | ||
const DONE_UPDATE = 'done_update'; | ||
|
||
class MessageInterpreter { | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
constructor() {} | ||
|
||
static send(message) { | ||
return JSON.stringify(message); | ||
} | ||
|
||
static receive(serializedMessage) { | ||
return JSON.parse(serializedMessage); | ||
} | ||
} | ||
|
||
function initClient({ id, onUpdate }) { | ||
const ws = new WebSocket(LOCAL_RELOAD_SOCKET_URL); | ||
|
||
ws.onopen = () => { | ||
ws.addEventListener('message', event => { | ||
const message = MessageInterpreter.receive(String(event.data)); | ||
|
||
if (message.type === DO_UPDATE && message.id === id) { | ||
onUpdate(); | ||
ws.send(MessageInterpreter.send({ type: DONE_UPDATE })); | ||
return; | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
function addRefresh() { | ||
let pendingReload = false; | ||
|
||
initClient({ | ||
id: __HMR_ID, | ||
onUpdate: () => { | ||
// disable reload when tab is hidden | ||
if (document.hidden) { | ||
pendingReload = true; | ||
return; | ||
} | ||
reload(); | ||
}, | ||
}); | ||
|
||
// reload | ||
function reload() { | ||
pendingReload = false; | ||
window.location.reload(); | ||
} | ||
|
||
// reload when tab is visible | ||
function reloadWhenTabIsVisible() { | ||
!document.hidden && pendingReload && reload(); | ||
} | ||
|
||
document.addEventListener('visibilitychange', reloadWhenTabIsVisible); | ||
} | ||
|
||
addRefresh(); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,5 @@ export type PluginConfig = { | |
onStart?: () => void; | ||
reload?: boolean; | ||
refresh?: boolean; | ||
id?: string; | ||
}; |