Skip to content

Commit

Permalink
Debounce hot module reloading for package rebuilds
Browse files Browse the repository at this point in the history
- this prevents reloads going nuts when changing e.g. gitbutler/ui
  • Loading branch information
mtsgrd committed Feb 6, 2025
1 parent 2d28818 commit f38eecc
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion apps/desktop/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { sentrySvelteKit } from '@sentry/sveltekit';
import { sveltekit } from '@sveltejs/kit/vite';
import { svelteTesting } from '@testing-library/svelte/vite';
import { defineConfig } from 'vitest/config';
import { defineConfig, type Plugin } from 'vitest/config';

export default defineConfig({
plugins: [
debounceReload(),
sentrySvelteKit({
adapter: 'other',
autoInstrument: {
Expand Down Expand Up @@ -64,3 +65,29 @@ export default defineConfig({
setupFiles: ['./vitest-setup.js']
}
});

/**
* A module to debounce reloading when making changes to packages rather than
* the desktop app.
*/
function debounceReload(): Plugin {
let timeout: NodeJS.Timeout | undefined;

return {
name: 'debounce-reload',
/**
* There is a `handleHotUpdate` callback that has the same docs, and
* gets called as expected, but that fails to prevent the reload.
*/
hotUpdate({ server, file }) {
if (file.includes('gitbutler/packages')) {
if (timeout) clearTimeout(timeout);
timeout = setTimeout(() => {
server.hot.send({ type: 'full-reload' });
timeout = undefined;
}, 5000);
return []; // Prevent immediate reload.
}
}
};
}

0 comments on commit f38eecc

Please sign in to comment.