Skip to content

Commit

Permalink
Fix typegen watcher leak on dev restart (#12331)
Browse files Browse the repository at this point in the history
  • Loading branch information
markdalgleish authored Nov 21, 2024
1 parent 10a1eff commit d0ac7ba
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/nice-cobras-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@react-router/dev": patch
---

Ensure typegen file watcher is cleaned up when Vite dev server restarts
10 changes: 9 additions & 1 deletion packages/react-router-dev/typegen/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ export async function run(rootDirectory: string) {
await writeAll(ctx);
}

export type Watcher = {
close: () => Promise<void>;
};

export async function watch(
rootDirectory: string,
{ logger }: { logger?: vite.Logger } = {}
) {
): Promise<Watcher> {
const ctx = await createContext({ rootDirectory, watch: true });
await writeAll(ctx);
logger?.info(pc.green("generated types"), { timestamp: true, clear: true });
Expand All @@ -38,6 +42,10 @@ export async function watch(
});
}
});

return {
close: async () => await ctx.configLoader.close(),
};
}

async function createContext({
Expand Down
6 changes: 5 additions & 1 deletion packages/react-router-dev/vite/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
let cssModulesManifest: Record<string, string> = {};
let viteChildCompiler: Vite.ViteDevServer | null = null;
let reactRouterConfigLoader: ConfigLoader;
let typegenWatcherPromise: Promise<Typegen.Watcher> | undefined;
let logger: Vite.Logger;
let firstLoad = true;

Expand Down Expand Up @@ -748,7 +749,7 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
viteUserConfig.root ?? process.env.REACT_ROUTER_ROOT ?? process.cwd();

if (viteCommand === "serve") {
Typegen.watch(rootDirectory, {
typegenWatcherPromise = Typegen.watch(rootDirectory, {
// ignore `info` logs from typegen since they are redundant when Vite plugin logs are active
logger: vite.createLogger("warn", { prefix: "[react-router]" }),
});
Expand Down Expand Up @@ -1246,6 +1247,9 @@ export const reactRouterVitePlugin: ReactRouterVitePlugin = () => {
async buildEnd() {
await viteChildCompiler?.close();
await reactRouterConfigLoader.close();

let typegenWatcher = await typegenWatcherPromise;
await typegenWatcher?.close();
},
},
{
Expand Down

0 comments on commit d0ac7ba

Please sign in to comment.