Skip to content

Commit 53300c4

Browse files
authored
[fix] restart on config change (#8087)
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.
1 parent 96751b0 commit 53300c4

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
restart vite dev-server on svelte config change

packages/kit/src/exports/vite/dev/index.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,16 @@ export async function dev(vite, vite_config, svelte_config) {
221221
}, 100);
222222
};
223223

224+
// flag to skip watchers if server is already restarting
225+
let restarting = false;
226+
224227
// Debounce add/unlink events because in case of folder deletion or moves
225228
// they fire in rapid succession, causing needless invocations.
226229
watch('add', () => debounce(update_manifest));
227230
watch('unlink', () => debounce(update_manifest));
228231
watch('change', (file) => {
229232
// Don't run for a single file if the whole manifest is about to get updated
230-
if (timeout) return;
233+
if (timeout || restarting) return;
231234

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

250+
// changing the svelte config requires restarting the dev server
251+
// the config is only read on start and passed on to vite-plugin-svelte
252+
// which needs up-to-date values to operate correctly
253+
vite.watcher.on('change', (file) => {
254+
if (path.basename(file) === 'svelte.config.js') {
255+
console.log(`svelte config changed, restarting vite dev-server. changed file: ${file}`);
256+
restarting = true;
257+
vite.restart();
258+
}
259+
});
260+
247261
const assets = svelte_config.kit.paths.assets ? SVELTE_KIT_ASSETS : svelte_config.kit.paths.base;
248262
const asset_server = sirv(svelte_config.kit.files.assets, {
249263
dev: true,

0 commit comments

Comments
 (0)