-
-
Notifications
You must be signed in to change notification settings - Fork 409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add HMR #25
Merged
Merged
Add HMR #25
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
edc0482
ADD HMR
Jonghakseo e90032c
add missing needToReload = false
Jonghakseo 726e873
ENHANCE: Refactor reload code
Jonghakseo a155c51
ADD: hmr virtual module
Jonghakseo a07a09a
FIX: unsued import
Jonghakseo 64c1061
FIX: production mode
Jonghakseo ece516a
DELETE: client script
Jonghakseo 4d82a9d
REFACTOR: getInjectionCode
Jonghakseo a1affda
REMOVE: unused alias
Jonghakseo 43dd40d
remove checkDevMode
Jonghakseo 98a4573
REFACTOR: remove duplicated code
Jonghakseo 303a2c8
EDIT: add git ignore compiled files
Jonghakseo 6b55eba
EDIT: reload-on-update function args type
Jonghakseo e5fcecc
ADD: interpreter and update pending message
Jonghakseo bedfc16
ADD: watchPath
Jonghakseo a5371de
EDIT: use Interpreter and set pending event
Jonghakseo 4619d24
EDIT: set watchPath
Jonghakseo a6f10f7
Merge branch 'main' into add-hmr
Jonghakseo 880332d
REMOVE: console.log
Jonghakseo f46cc8c
REFACTOR: add comment and re-naming
Jonghakseo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,5 @@ | |
|
||
#generated manifest | ||
public/manifest.json | ||
utils/reload/*.js | ||
utils/reload/injections/*.js |
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 |
---|---|---|
@@ -1 +1,5 @@ | ||
import reloadOnUpdate from "virtual:reload-on-update-in-background-script"; | ||
|
||
reloadOnUpdate("pages/background"); | ||
|
||
console.log("background loaded"); |
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
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
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,50 @@ | ||
import * as path from "path"; | ||
import { PluginOption } from "vite"; | ||
import { readFileSync } from "fs"; | ||
|
||
const isDev = process.env.__DEV__ === "true"; | ||
|
||
const DUMMY_CODE = `export default function(){};`; | ||
|
||
function getInjectionCode(fileName: string): string { | ||
return readFileSync( | ||
path.resolve(__dirname, "..", "reload", "injections", fileName), | ||
{ encoding: "utf8" } | ||
); | ||
} | ||
|
||
type Config = { | ||
background?: boolean; | ||
view?: boolean; | ||
}; | ||
|
||
export default function addHmr(config?: Config): PluginOption { | ||
const { background = false, view = true } = config || {}; | ||
const idInBackgroundScript = "virtual:reload-on-update-in-background-script"; | ||
const idInView = "virtual:reload-on-update-in-view"; | ||
|
||
const scriptHmrCode = isDev ? getInjectionCode("script.js") : DUMMY_CODE; | ||
const viewHmrCode = isDev ? getInjectionCode("view.js") : DUMMY_CODE; | ||
|
||
return { | ||
name: "add-hmr", | ||
resolveId(id) { | ||
if (id === idInBackgroundScript || id === idInView) { | ||
return getResolvedId(id); | ||
} | ||
}, | ||
load(id) { | ||
if (id === getResolvedId(idInBackgroundScript)) { | ||
return background ? scriptHmrCode : DUMMY_CODE; | ||
} | ||
|
||
if (id === getResolvedId(idInView)) { | ||
return view ? viewHmrCode : DUMMY_CODE; | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
function getResolvedId(id: string) { | ||
return "\0" + id; | ||
} |
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,5 @@ | ||
export const LOCAL_RELOAD_SOCKET_PORT = 8081; | ||
export const LOCAL_RELOAD_SOCKET_URL = `ws://localhost:${LOCAL_RELOAD_SOCKET_PORT}`; | ||
export const UPDATE_PENDING_MESSAGE = "wait_update"; | ||
export const UPDATE_REQUEST_MESSAGE = "do_update"; | ||
export const UPDATE_COMPLETE_MESSAGE = "done_update"; |
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,57 @@ | ||
import { | ||
LOCAL_RELOAD_SOCKET_URL, | ||
UPDATE_COMPLETE_MESSAGE, | ||
UPDATE_PENDING_MESSAGE, | ||
UPDATE_REQUEST_MESSAGE, | ||
} from "./constant"; | ||
import { Interpreter } from "./interpreter"; | ||
|
||
let needToUpdate = false; | ||
|
||
export default function initReloadClient({ | ||
watchPath, | ||
onUpdate, | ||
}: { | ||
watchPath: string; | ||
onUpdate: () => void; | ||
}): WebSocket { | ||
const socket = new WebSocket(LOCAL_RELOAD_SOCKET_URL); | ||
|
||
function sendUpdateCompleteMessage() { | ||
socket.send(Interpreter.Send({ type: UPDATE_COMPLETE_MESSAGE })); | ||
} | ||
|
||
socket.addEventListener("message", (event) => { | ||
const message = Interpreter.Receive(String(event.data)); | ||
|
||
switch (message.type) { | ||
case UPDATE_REQUEST_MESSAGE: { | ||
if (needToUpdate) { | ||
sendUpdateCompleteMessage(); | ||
needToUpdate = false; | ||
onUpdate(); | ||
} | ||
return; | ||
} | ||
case UPDATE_PENDING_MESSAGE: { | ||
needToUpdate = checkUpdateIsNeeded({ | ||
watchPath, | ||
updatedPath: message.path, | ||
}); | ||
return; | ||
} | ||
} | ||
}); | ||
|
||
return socket; | ||
} | ||
|
||
function checkUpdateIsNeeded({ | ||
watchPath, | ||
updatedPath, | ||
}: { | ||
updatedPath: string; | ||
watchPath: string; | ||
}): boolean { | ||
return updatedPath.includes(watchPath); | ||
} |
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,70 @@ | ||
import { WebSocketServer, WebSocket } from "ws"; | ||
import chokidar from "chokidar"; | ||
import { clearTimeout } from "timers"; | ||
import { | ||
LOCAL_RELOAD_SOCKET_PORT, | ||
UPDATE_COMPLETE_MESSAGE, | ||
UPDATE_PENDING_MESSAGE, | ||
UPDATE_REQUEST_MESSAGE, | ||
} from "./constant"; | ||
import { Interpreter } from "./interpreter"; | ||
|
||
const clientsThatNeedToUpdate: Set<WebSocket> = new Set(); | ||
|
||
function initReloadServer() { | ||
const wss = new WebSocketServer({ port: LOCAL_RELOAD_SOCKET_PORT }); | ||
|
||
wss.on("connection", (ws) => { | ||
clientsThatNeedToUpdate.add(ws); | ||
|
||
ws.addEventListener("close", () => clientsThatNeedToUpdate.delete(ws)); | ||
ws.addEventListener("message", (event) => { | ||
const message = Interpreter.Receive(String(event.data)); | ||
if (message.type === UPDATE_COMPLETE_MESSAGE) { | ||
ws.close(); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
/** CHECK:: src file was updated **/ | ||
chokidar.watch("src").on("all", (event, path) => { | ||
debounce(sendPendingUpdateMessageToAllSockets, 200)(path); | ||
}); | ||
|
||
function sendPendingUpdateMessageToAllSockets(path: string) { | ||
const _sendPendingUpdateMessage = (ws: WebSocket) => | ||
sendPendingUpdateMessage(ws, path); | ||
|
||
clientsThatNeedToUpdate.forEach(_sendPendingUpdateMessage); | ||
} | ||
|
||
function sendPendingUpdateMessage(ws: WebSocket, path: string) { | ||
ws.send(Interpreter.Send({ type: UPDATE_PENDING_MESSAGE, path })); | ||
} | ||
|
||
/** CHECK:: build was completed **/ | ||
chokidar.watch("dist").on("all", () => { | ||
debounce(sendUpdateMessageToAllSockets, 200); | ||
}); | ||
|
||
function sendUpdateMessageToAllSockets() { | ||
clientsThatNeedToUpdate.forEach(sendUpdateMessage); | ||
} | ||
|
||
function sendUpdateMessage(ws: WebSocket) { | ||
ws.send(Interpreter.Send({ type: UPDATE_REQUEST_MESSAGE })); | ||
} | ||
|
||
function debounce<A extends unknown[]>( | ||
callback: (...args: A) => void, | ||
delay: number | ||
) { | ||
let timer: NodeJS.Timeout; | ||
return function (...args: A) { | ||
clearTimeout(timer); | ||
timer = setTimeout(() => callback(...args), delay); | ||
}; | ||
} | ||
|
||
initReloadServer(); |
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,12 @@ | ||
import initReloadClient from "../initReloadClient"; | ||
|
||
export default function addHmrIntoScript(watchPath: string) { | ||
initReloadClient({ | ||
watchPath, | ||
onUpdate: () => { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
chrome.runtime.reload(); | ||
}, | ||
}); | ||
} |
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,29 @@ | ||
import initReloadClient from "../initReloadClient"; | ||
|
||
export default function addHmrIntoView(watchPath: string) { | ||
let pendingReload = false; | ||
|
||
initReloadClient({ | ||
watchPath, | ||
onUpdate: () => { | ||
// disable reload when tab is hidden | ||
if (document.hidden) { | ||
pendingReload = true; | ||
return; | ||
} | ||
reload(); | ||
}, | ||
}); | ||
|
||
// reload | ||
function reload(): void { | ||
pendingReload = false; | ||
window.location.reload(); | ||
} | ||
|
||
// reload when tab is visible | ||
function reloadWhenTabIsVisible(): void { | ||
!document.hidden && pendingReload && reload(); | ||
} | ||
document.addEventListener("visibilitychange", reloadWhenTabIsVisible); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curios what is it about