Skip to content

Commit

Permalink
[fix] restart on config change (#8087)
Browse files Browse the repository at this point in the history
recently sveltekit changed to passing the svelte config inline to vite-plugin-svelte. With this way, vite-plugin-svelte does not watch svelte.config.js for changes to restart the vite devserver, as configFile: false indicates it should not use it.

this PR sets up a watcher in kits dev plugin that does the same. Restarting the server is absolutely required in this case.
  • Loading branch information
dominikg authored Dec 12, 2022
1 parent 96751b0 commit 53300c4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-numbers-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

restart vite dev-server on svelte config change
18 changes: 16 additions & 2 deletions packages/kit/src/exports/vite/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,16 @@ export async function dev(vite, vite_config, svelte_config) {
}, 100);
};

// flag to skip watchers if server is already restarting
let restarting = false;

// Debounce add/unlink events because in case of folder deletion or moves
// they fire in rapid succession, causing needless invocations.
watch('add', () => debounce(update_manifest));
watch('unlink', () => debounce(update_manifest));
watch('change', (file) => {
// Don't run for a single file if the whole manifest is about to get updated
if (timeout) return;
if (timeout || restarting) return;

sync.update(svelte_config, manifest_data, file);
});
Expand All @@ -238,12 +241,23 @@ export async function dev(vite, vite_config, svelte_config) {
// send the vite client a full-reload event without path being set
if (appTemplate !== 'index.html') {
vite.watcher.on('change', (file) => {
if (file === appTemplate) {
if (file === appTemplate && !restarting) {
vite.ws.send({ type: 'full-reload' });
}
});
}

// changing the svelte config requires restarting the dev server
// the config is only read on start and passed on to vite-plugin-svelte
// which needs up-to-date values to operate correctly
vite.watcher.on('change', (file) => {
if (path.basename(file) === 'svelte.config.js') {
console.log(`svelte config changed, restarting vite dev-server. changed file: ${file}`);
restarting = true;
vite.restart();
}
});

const assets = svelte_config.kit.paths.assets ? SVELTE_KIT_ASSETS : svelte_config.kit.paths.base;
const asset_server = sirv(svelte_config.kit.files.assets, {
dev: true,
Expand Down

0 comments on commit 53300c4

Please sign in to comment.