Skip to content

Commit db84946

Browse files
authored
feat: auto-restart SvelteKit when Svelte config changed (#237)
1 parent 6b0983c commit db84946

File tree

8 files changed

+40
-23
lines changed

8 files changed

+40
-23
lines changed

.changeset/forty-olives-look.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': minor
3+
---
4+
5+
auto-restart SvelteKit when Svelte config changed

packages/e2e-tests/kit-node/__tests__/kit.spec.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
isBuild,
88
readFileContent,
99
sleep,
10-
untilUpdated
10+
untilUpdated,
11+
waitForNavigation
1112
} from '../../testUtils';
1213

1314
import fetch from 'node-fetch';
@@ -232,14 +233,14 @@ describe('kit-node', () => {
232233
});
233234
});
234235
describe('config file update', () => {
235-
it('should show an overlay', async () => {
236-
await editFile('svelte.config.js', (config) => config + '\n');
237-
const errorOverlay = await page.waitForSelector('vite-error-overlay');
238-
expect(errorOverlay).toBeTruthy();
239-
const message = await errorOverlay.$$eval('.message-body', (m) => {
240-
return m[0].innerHTML;
241-
});
242-
expect(message).toContain('Svelte config change detected');
236+
it('should auto refresh', async () => {
237+
const button = await getEl('button');
238+
await button.click();
239+
expect(await getText('button')).toBe('Clicks: 1');
240+
editFile('svelte.config.js', (config) => config + '\n');
241+
await waitForNavigation({ waitUntil: 'networkidle' });
242+
// clicks should reset, means the browser refreshed
243+
expect(await getText('button')).toBe('Clicks: 0');
243244
});
244245
});
245246
});

packages/e2e-tests/kit-node/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
},
1010
"devDependencies": {
1111
"@sveltejs/adapter-node": "^1.0.0-next.56",
12-
"@sveltejs/kit": "^1.0.0-next.203",
12+
"@sveltejs/kit": "^1.0.0-next.204",
1313
"e2e-test-dep-svelte-api-only": "workspace:*",
1414
"svelte": "^3.44.3",
1515
"svelte-i18n": "^3.3.13"

packages/e2e-tests/testUtils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,17 @@ export async function editViteConfig(replacer: (str: string) => string) {
203203
await page.goto(viteTestUrl, { waitUntil: 'networkidle' });
204204
await sleep(50);
205205
}
206+
207+
export async function waitForNavigation(opts: Parameters<typeof page.waitForNavigation>[0]) {
208+
const timeout = opts.timeout || 30000; // default playwright timeout is 30000
209+
let timeoutHandle: NodeJS.Timeout;
210+
const timeoutPromise = new Promise((resolve, reject) => {
211+
timeoutHandle = setTimeout(
212+
() => reject(new Error(`navigation timed out after ${timeout}ms`)),
213+
timeout - 50 // have slightly shorter timeout so error is shown before playwright timeout
214+
);
215+
});
216+
await Promise.race([page.waitForNavigation(opts), timeoutPromise]).finally(() => {
217+
clearTimeout(timeoutHandle);
218+
});
219+
}

packages/playground/kit-demo-app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
},
1010
"devDependencies": {
1111
"@sveltejs/adapter-node": "^1.0.0-next.56",
12-
"@sveltejs/kit": "^1.0.0-next.203",
12+
"@sveltejs/kit": "^1.0.0-next.204",
1313
"svelte": "^3.44.3"
1414
},
1515
"type": "module",

packages/vite-plugin-svelte/src/utils/options.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,7 @@ export async function preResolveOptions(
8282
// extras
8383
root: viteConfigWithResolvedRoot.root!,
8484
isBuild: viteEnv.command === 'build',
85-
isServe: viteEnv.command === 'serve',
86-
// @ts-expect-error we don't declare kit property of svelte config but read it once here to identify kit projects
87-
isSvelteKit: !!svelteConfig?.kit
85+
isServe: viteEnv.command === 'serve'
8886
};
8987
// configFile of svelteConfig contains the absolute path it was loaded from,
9088
// prefer it over the possibly relative inline path
@@ -492,7 +490,6 @@ export interface PreResolvedOptions extends Options {
492490
root: string;
493491
isBuild: boolean;
494492
isServe: boolean;
495-
isSvelteKit: boolean;
496493
}
497494

498495
export interface ResolvedOptions extends PreResolvedOptions {

packages/vite-plugin-svelte/src/utils/watch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ export function setupWatchers(
4242
};
4343

4444
const triggerViteRestart = (filename: string) => {
45-
if (serverConfig.middlewareMode || options.isSvelteKit) {
46-
// in middlewareMode or for sveltekit we can't restart the server automatically
45+
if (serverConfig.middlewareMode) {
46+
// in middlewareMode we can't restart the server automatically
4747
// show the user an overlay instead
4848
const message =
4949
'Svelte config change detected, restart your dev process to apply the changes.';

pnpm-lock.yaml

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)