Skip to content

Commit

Permalink
Close Websocket on Vite server restart (#715)
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-lee authored Feb 20, 2025
1 parent 31b5248 commit 9cee3d4
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 21 deletions.
20 changes: 0 additions & 20 deletions packages/zudoku/src/vite/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,6 @@ const getDocsConfigFiles = (
return docsArray.map((doc) => path.posix.join(baseDir, doc.files));
};

// We extend the dependencies with the files from configured APIs
// so that the server restarts when these files change.
const registerApiFileImportDependencies = (
config: LoadedConfig,
rootDir: string,
) => {
if (!config.apis) return;

const apis = Array.isArray(config.apis) ? config.apis : [config.apis];

const files = apis
.filter((c) => c.type === "file")
.flatMap((c) => (Array.isArray(c.input) ? c.input : [c.input]))
.map((c) => path.posix.join(rootDir, c));

config.__meta.registerDependency(...files);
};

function loadEnv(configEnv: ConfigEnv, rootDir: string) {
const envPrefix = [
...(ZuploEnv.isZuplo ? ["ZUPLO_PUBLIC_"] : []),
Expand Down Expand Up @@ -107,8 +89,6 @@ export async function loadZudokuConfig(
}
const loadedConfig = await tryLoadZudokuConfig(rootDir, envVars);

registerApiFileImportDependencies(loadedConfig, rootDir);

logger.info(
colors.yellow(`loaded config file `) +
colors.dim(loadedConfig.__meta.path),
Expand Down
3 changes: 2 additions & 1 deletion packages/zudoku/src/vite/plugin-config-reload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const createConfigReloadPlugin = (

const plugin: Plugin = {
name: "zudoku-config-reload",
configureServer: ({ watcher, restart }) => {
configureServer: ({ watcher, restart, ws }) => {
if (!onConfigChange) return;

watcher.on("change", async (file) => {
Expand All @@ -26,6 +26,7 @@ export const createConfigReloadPlugin = (
// Assume `.tsx` files are handled by HMR (skip if the config file itself changed)
if (file !== newConfig.__meta.path && file.endsWith(".tsx")) return;

await ws.close();
await restart();
printDiagnosticsToConsole(
`[${new Date().toLocaleTimeString()}]: Config ${path.basename(currentConfig.__meta.path)} changed. Restarted server.`,
Expand Down

0 comments on commit 9cee3d4

Please sign in to comment.