Skip to content

Commit 4021352

Browse files
authored
refactor: use new server.restart api and update vite peer to ^2.7.0 (#223)
* refactor: update peerDependency on vite to ^2.7.0 and use new server.restart api instead of fake change event * fix: disable automatic restart for kit, it doesn't work
1 parent 2f5b373 commit 4021352

File tree

5 files changed

+23
-21
lines changed

5 files changed

+23
-21
lines changed

.changeset/smooth-rings-run.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': major
3+
---
4+
5+
update vite peerDependency to ^2.7.0 and refactor server restart on change of svelte.config.js

packages/vite-plugin-svelte/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"peerDependencies": {
5656
"diff-match-patch": "^1.0.5",
5757
"svelte": "^3.44.0",
58-
"vite": "^2.6.0"
58+
"vite": "^2.7.0"
5959
},
6060
"peerDependenciesMeta": {
6161
"diff-match-patch": {

packages/vite-plugin-svelte/src/index.ts

+5-12
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,9 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
9595
}
9696
},
9797

98-
async resolveId(importee, importer, opts, ...args) {
99-
// get _ssr this way to suppress typescript warning
100-
const _ssr = (args as any)[0] as boolean | undefined;
101-
102-
// @ts-expect-error anticipate vite deprecating forth parameter and rely on `opts.ssr` instead`
103-
// see https://github.com/vitejs/vite/discussions/5109
104-
const ssr: boolean = _ssr === true || opts.ssr;
105-
const svelteRequest = requestParser(importee, !!ssr);
98+
async resolveId(importee, importer, opts) {
99+
const ssr = !!opts?.ssr;
100+
const svelteRequest = requestParser(importee, ssr);
106101
if (svelteRequest?.query.svelte) {
107102
if (svelteRequest.query.type === 'style') {
108103
// return cssId with root prefix so postcss pipeline of vite finds the directory correctly
@@ -153,10 +148,8 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
153148
},
154149

155150
async transform(code, id, opts) {
156-
// @ts-expect-error anticipate vite changing third parameter as options object
157-
// see https://github.com/vitejs/vite/discussions/5109
158-
const ssr: boolean = opts === true || opts?.ssr;
159-
const svelteRequest = requestParser(id, !!ssr);
151+
const ssr = !!opts?.ssr;
152+
const svelteRequest = requestParser(id, ssr);
160153
if (!svelteRequest) {
161154
return;
162155
}

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ function mergeOptions(
121121
viteConfig: UserConfig,
122122
viteEnv: ConfigEnv
123123
): ResolvedOptions {
124+
// @ts-ignore
124125
const merged = {
125126
...defaultOptions,
126127
...svelteConfig,
@@ -137,7 +138,9 @@ function mergeOptions(
137138
root: viteConfig.root!,
138139
isProduction: viteEnv.mode === 'production',
139140
isBuild: viteEnv.command === 'build',
140-
isServe: viteEnv.command === 'serve'
141+
isServe: viteEnv.command === 'serve',
142+
// @ts-expect-error we don't declare kit property of svelte config but read it once here to identify kit projects
143+
isSvelteKit: !!svelteConfig?.kit
141144
};
142145
// configFile of svelteConfig contains the absolute path it was loaded from,
143146
// prefer it over the possibly relative inline path
@@ -485,6 +488,7 @@ export interface ResolvedOptions extends Options {
485488
isBuild: boolean;
486489
isServe: boolean;
487490
server?: ViteDevServer;
491+
isSvelteKit?: boolean;
488492
}
489493

490494
export type {

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function setupWatchers(
1717
return;
1818
}
1919
const { watcher, ws } = server;
20-
const { configFile: viteConfigFile, root, server: serverConfig } = server.config;
20+
const { root, server: serverConfig } = server.config;
2121

2222
const emitChangeEventOnDependants = (filename: string) => {
2323
const dependants = cache.getDependants(filename);
@@ -42,19 +42,19 @@ export function setupWatchers(
4242
};
4343

4444
const triggerViteRestart = (filename: string) => {
45-
// vite restart is triggered by simulating a change to vite config. This requires that vite config exists
46-
// also we do not restart in middleware-mode as it could be risky
47-
if (!!viteConfigFile && !serverConfig.middlewareMode) {
48-
log.info(`svelte config changed: restarting vite server. - file: ${filename}`);
49-
watcher.emit('change', viteConfigFile);
50-
} else {
45+
if (serverConfig.middlewareMode || options.isSvelteKit) {
46+
// in middlewareMode or for sveltekit we can't restart the server automatically
47+
// show the user an overlay instead
5148
const message =
5249
'Svelte config change detected, restart your dev process to apply the changes.';
5350
log.info(message, filename);
5451
ws.send({
5552
type: 'error',
5653
err: { message, stack: '', plugin: 'vite-plugin-svelte', id: filename }
5754
});
55+
} else {
56+
log.info(`svelte config changed: restarting vite server. - file: ${filename}`);
57+
server.restart(!!options.experimental?.prebundleSvelteLibraries);
5858
}
5959
};
6060

0 commit comments

Comments
 (0)