diff --git a/.changeset/router-webpack-listener-shutdown.md b/.changeset/router-webpack-listener-shutdown.md new file mode 100644 index 0000000000..a10567caf6 --- /dev/null +++ b/.changeset/router-webpack-listener-shutdown.md @@ -0,0 +1,5 @@ +--- +'@tanstack/router-plugin': patch +--- + +webpack and Rspack: the router plugin's route listener ends when the compiler shuts down (`compiler.close()`), not only when it stops watching. A one-shot build with the generator and code splitter as separate plugins no longer keeps its compiler state alive for the rest of the process. diff --git a/packages/router-plugin/src/hosts/webpack.ts b/packages/router-plugin/src/hosts/webpack.ts index 2638549491..dbb8e1f2e9 100644 --- a/packages/router-plugin/src/hosts/webpack.ts +++ b/packages/router-plugin/src/hosts/webpack.ts @@ -69,6 +69,7 @@ export interface CompilerLike extends WatchingCompilerLike { ) => void } watchClose: { tap: (name: string, fn: () => void) => void } + shutdown: { tapPromise: (name: string, fn: () => Promise) => void } } } @@ -279,6 +280,14 @@ export function createRouterCompilerWebpackPlugin( await session.invalidate(normalizePath(file), kind) } }) + // the session lives as long as the compiler: until it shuts down + // (`compiler.close()`, after a one-shot `run()` too) or stops watching + // (a dev middleware closes its watching, not the compiler); the + // context outlives both (the separate plugins share the module's) + compiler.hooks.shutdown.tapPromise(PLUGIN_NAME, () => { + unsubscribe() + return Promise.resolve() + }) compiler.hooks.watchClose.tap(PLUGIN_NAME, () => { unsubscribe() })