Skip to content

Fix typegen watcher leak on dev restart #12331

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

Merged
merged 1 commit into from
Nov 21, 2024
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/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