Skip to content

Commit

Permalink
Merge pull request #4 from isaurssaurav/issue/#2
Browse files Browse the repository at this point in the history
fix(#2): refresh the page after extension is reloaded
  • Loading branch information
isaurssaurav authored Oct 28, 2023
2 parents 6c6dae9 + 020d4ca commit 09d3f73
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
7 changes: 1 addition & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ export type hotReloadExtensionOptions = {
log?: boolean;
};

let IS_TRANSFORMED = false;

const hotReloadExtension = (options: hotReloadExtensionOptions): Plugin => {
const { log, backgroundPath } = options;
let ws: WebSocket | null = null;
Expand All @@ -28,13 +26,11 @@ const hotReloadExtension = (options: hotReloadExtensionOptions): Plugin => {
return {
name: PLUGIN_NAME,
async transform(code: string, id: string) {
if (!isDev || IS_TRANSFORMED) {
if (!isDev) {
return;
}

if (id.includes(backgroundPath)) {
IS_TRANSFORMED = true;

const buffer = fs.readFileSync(resolve(__dirname, 'scripts/background-reload.js'));
return {
code: code + buffer.toString()
Expand All @@ -50,7 +46,6 @@ const hotReloadExtension = (options: hotReloadExtensionOptions): Plugin => {
chalkLogger.red('Load extension to browser...');
return;
}

setTimeout(() => {
ws?.send(Message.FILE_CHANGE);
if (log) chalkLogger.green('Extension Reloaded...');
Expand Down
7 changes: 6 additions & 1 deletion src/scripts/background-reload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import { HOT_RELOAD_EXTENSION_VITE_PORT, Message } from '../utils';
*/
const socket = new WebSocket(`ws://localhost:${HOT_RELOAD_EXTENSION_VITE_PORT}`);

chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === chrome.runtime.OnInstalledReason.UPDATE) {
chrome.tabs.reload();
}
});

socket.addEventListener('message', (event) => {
if (event.data === Message.FILE_CHANGE) {
chrome.runtime.reload();
chrome.tabs.reload();
}
});

0 comments on commit 09d3f73

Please sign in to comment.