Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use new server.restart api and update vite peer to ^2.7.0 #223

Merged
merged 2 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/smooth-rings-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/vite-plugin-svelte': major
---

update vite peerDependency to ^2.7.0 and refactor server restart on change of svelte.config.js
2 changes: 1 addition & 1 deletion packages/vite-plugin-svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"peerDependencies": {
"diff-match-patch": "^1.0.5",
"svelte": "^3.44.0",
"vite": "^2.6.0"
"vite": "^2.7.0"
},
"peerDependenciesMeta": {
"diff-match-patch": {
Expand Down
17 changes: 5 additions & 12 deletions packages/vite-plugin-svelte/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,9 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
}
},

async resolveId(importee, importer, opts, ...args) {
// get _ssr this way to suppress typescript warning
const _ssr = (args as any)[0] as boolean | undefined;

// @ts-expect-error anticipate vite deprecating forth parameter and rely on `opts.ssr` instead`
// see https://github.com/vitejs/vite/discussions/5109
const ssr: boolean = _ssr === true || opts.ssr;
const svelteRequest = requestParser(importee, !!ssr);
async resolveId(importee, importer, opts) {
const ssr = !!opts?.ssr;
const svelteRequest = requestParser(importee, ssr);
if (svelteRequest?.query.svelte) {
if (svelteRequest.query.type === 'style') {
// return cssId with root prefix so postcss pipeline of vite finds the directory correctly
Expand Down Expand Up @@ -153,10 +148,8 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
},

async transform(code, id, opts) {
// @ts-expect-error anticipate vite changing third parameter as options object
// see https://github.com/vitejs/vite/discussions/5109
const ssr: boolean = opts === true || opts?.ssr;
const svelteRequest = requestParser(id, !!ssr);
const ssr = !!opts?.ssr;
const svelteRequest = requestParser(id, ssr);
if (!svelteRequest) {
return;
}
Expand Down
6 changes: 5 additions & 1 deletion packages/vite-plugin-svelte/src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ function mergeOptions(
viteConfig: UserConfig,
viteEnv: ConfigEnv
): ResolvedOptions {
// @ts-ignore
const merged = {
...defaultOptions,
...svelteConfig,
Expand All @@ -137,7 +138,9 @@ function mergeOptions(
root: viteConfig.root!,
isProduction: viteEnv.mode === 'production',
isBuild: viteEnv.command === 'build',
isServe: viteEnv.command === 'serve'
isServe: viteEnv.command === 'serve',
// @ts-expect-error we don't declare kit property of svelte config but read it once here to identify kit projects
isSvelteKit: !!svelteConfig?.kit
};
// configFile of svelteConfig contains the absolute path it was loaded from,
// prefer it over the possibly relative inline path
Expand Down Expand Up @@ -485,6 +488,7 @@ export interface ResolvedOptions extends Options {
isBuild: boolean;
isServe: boolean;
server?: ViteDevServer;
isSvelteKit?: boolean;
}

export type {
Expand Down
14 changes: 7 additions & 7 deletions packages/vite-plugin-svelte/src/utils/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function setupWatchers(
return;
}
const { watcher, ws } = server;
const { configFile: viteConfigFile, root, server: serverConfig } = server.config;
const { root, server: serverConfig } = server.config;

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

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

Expand Down