Skip to content
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

[Fix] Support content.css HMR #799

Merged
merged 3 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions chrome-extension/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const manifest = deepmerge(
},
{
matches: ['http://*/*', 'https://*/*', '<all_urls>'],
js: ['refresh.js'], // for public's HMR(refresh) support
css: ['content.css'], // public folder
},
],
Expand Down
72 changes: 72 additions & 0 deletions chrome-extension/public/refresh.js
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();
})();
2 changes: 1 addition & 1 deletion chrome-extension/vite.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default defineConfig({
}) as PluginOption,
watchPublicPlugin(),
makeManifestPlugin({ outDir }),
isDev && watchRebuildPlugin({ reload: true }),
isDev && watchRebuildPlugin({ reload: true, id: 'chrome-extension-hmr' }),
],
publicDir: resolve(rootDir, 'public'),
build: {
Expand Down
10 changes: 5 additions & 5 deletions packages/hmr/lib/plugins/watch-rebuild-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ const refreshCode = fs.readFileSync(path.resolve(injectionsPath, 'refresh.js'),
const reloadCode = fs.readFileSync(path.resolve(injectionsPath, 'reload.js'), 'utf-8');

export function watchRebuildPlugin(config: PluginConfig): PluginOption {
const { refresh, reload, id: _id, onStart } = config;
const hmrCode = (refresh ? refreshCode : '') + (reload ? reloadCode : '');

let ws: WebSocket | null = null;

const id = Math.random().toString(36);
const id = _id ?? Math.random().toString(36);
let reconnectTries = 0;

const { refresh, reload } = config;
const hmrCode = (refresh ? refreshCode : '') + (reload ? reloadCode : '');

function initializeWebSocket() {
ws = new WebSocket(LOCAL_RELOAD_SOCKET_URL);

Expand All @@ -46,7 +46,7 @@ export function watchRebuildPlugin(config: PluginConfig): PluginOption {
return {
name: 'watch-rebuild',
writeBundle() {
config.onStart?.();
onStart?.();
if (!ws) {
initializeWebSocket();
return;
Expand Down
1 change: 1 addition & 0 deletions packages/hmr/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ export type PluginConfig = {
onStart?: () => void;
reload?: boolean;
refresh?: boolean;
id?: string;
};
Loading