Skip to content

Commit 63a886d

Browse files
authored
fix(start-plugin-core): skip unchanged server function resolver rebuilds in Rsbuild dev (#8750)
* fix(start-plugin-core): skip unchanged server function resolver rebuilds in Rsbuild dev - Skip the resolver rebuild at the end of make when the resolver has not been written since this compiler last rebuilt it. Rebuilding an unchanged resolver made Rspack rebuild the server chunk graph after every edit. - Child compilations still rebuild every time and do not change the parent compiler's state. - The RSC resolver hook skips unchanged writes in dev, like the non-RSC hook. * docs: drop the COMPILER_ARCHITECTURE.md change
1 parent d9b07e8 commit 63a886d

5 files changed

Lines changed: 265 additions & 33 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/start-plugin-core': patch
3+
---
4+
5+
Skip the server function resolver rebuild in Rsbuild dev when the resolver has not changed, so most edits no longer rebuild the whole server chunk graph.

‎packages/start-plugin-core/src/rsbuild/plugin.ts‎

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
} from './virtual-modules'
2828
import { createServerSetup } from './server-middleware'
2929
import { registerClientBuildCapture } from './normalized-client-build'
30+
import { tapServerFnResolverRebuild } from './server-fn-resolver-rebuild'
3031
import { registerRouterPlugins } from './start-router-plugin'
3132
import { postBuildWithRsbuild } from './post-build'
3233
import { enableSwcReactServerComponents } from './swc-rsc'
@@ -343,22 +344,24 @@ export function tanStackStartRsbuild(
343344
return
344345
}
345346

347+
const environmentName = utils.environment.name
346348
config.plugins.push({
347349
apply(compiler: RspackCompiler) {
348-
compiler.hooks.finishMake.tapPromise(
349-
{
350-
name: 'TanStackStartServerFnResolverRebuild',
351-
stage: -10,
352-
},
353-
async (compilation: RspackCompilationExtended) => {
350+
tapServerFnResolverRebuild(compiler, {
351+
name: 'TanStackStartServerFnResolverRebuild',
352+
updateResolver: () => {
354353
virtualModuleState.updateServerFnResolver()
355-
356-
await rebuildModulesContaining(
354+
},
355+
getResolverWriteCount: () =>
356+
virtualModuleState.getServerFnResolverWriteCount(
357+
environmentName,
358+
),
359+
rebuildResolver: (compilation) =>
360+
rebuildModulesContaining(
357361
compilation,
358362
virtualModuleState.serverFnResolverPath,
359-
)
360-
},
361-
)
363+
),
364+
})
362365
},
363366
})
364367
})
@@ -537,24 +540,23 @@ export function tanStackStartRsbuild(
537540

538541
config.plugins.push({
539542
apply(compiler: RspackCompiler) {
540-
compiler.hooks.finishMake.tapPromise(
541-
{
542-
name: 'TanStackStartRscServerFnResolverRebuild',
543-
stage: -10,
544-
},
545-
async (compilation: RspackCompilationExtended) => {
546-
const resolverContent =
547-
virtualModuleState.generateCurrentResolverContent(true)
543+
tapServerFnResolverRebuild(compiler, {
544+
name: 'TanStackStartRscServerFnResolverRebuild',
545+
updateResolver: () => {
548546
virtualModuleState.tryUpdateServerFnResolver(
549-
resolverContent,
547+
virtualModuleState.generateCurrentResolverContent(true),
550548
)
551-
552-
await rebuildModulesContaining(
549+
},
550+
getResolverWriteCount: () =>
551+
virtualModuleState.getServerFnResolverWriteCount(
552+
RSBUILD_ENVIRONMENT_NAMES.server,
553+
),
554+
rebuildResolver: (compilation) =>
555+
rebuildModulesContaining(
553556
compilation,
554557
virtualModuleState.serverFnResolverPath,
555-
)
556-
},
557-
)
558+
),
559+
})
558560
},
559561
})
560562

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import type { Rspack } from '@rsbuild/core'
2+
3+
export interface ServerFnResolverRebuildOptions {
4+
/** Name of the finishMake tap. */
5+
name: string
6+
/** Writes the current resolver content to its virtual module. */
7+
updateResolver: () => void
8+
/** Returns how many times this environment's resolver has been written. */
9+
getResolverWriteCount: () => number
10+
/** Rebuilds the resolver module in the given compilation. */
11+
rebuildResolver: (compilation: Rspack.Compilation) => Promise<void>
12+
}
13+
14+
/**
15+
* Rebuilds the server function resolver module at finishMake stage -10,
16+
* after stage -20 has refreshed the server function registry.
17+
*
18+
* Skips the rebuild when the resolver has not been written since this
19+
* compiler last rebuilt it. In development the resolver is written only when
20+
* its content changes. Rebuilding an unchanged resolver can make Rspack
21+
* rebuild the whole chunk graph.
22+
*/
23+
export function tapServerFnResolverRebuild(
24+
compiler: Rspack.Compiler,
25+
options: ServerFnResolverRebuildOptions,
26+
): void {
27+
// Write count of the resolver content this compiler last rebuilt from.
28+
let rebuiltAtWriteCount: number | undefined
29+
30+
compiler.hooks.finishMake.tapPromise(
31+
{ name: options.name, stage: -10 },
32+
async (compilation) => {
33+
options.updateResolver()
34+
35+
// Child compilers copy finishMake taps from their parent. Child
36+
// compilations always rebuild and do not change this compiler's state.
37+
if (compilation.compiler !== compiler) {
38+
await options.rebuildResolver(compilation)
39+
return
40+
}
41+
42+
// Read the count before the rebuild reads the resolver, so a write
43+
// that lands during the rebuild causes another rebuild next time.
44+
const writeCount = options.getResolverWriteCount()
45+
if (writeCount === rebuiltAtWriteCount) {
46+
return
47+
}
48+
49+
await options.rebuildResolver(compilation)
50+
rebuiltAtWriteCount = writeCount
51+
},
52+
)
53+
}

‎packages/start-plugin-core/src/rsbuild/virtual-modules.ts‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ export interface VirtualModuleState {
4545
updateManifest: (clientBuild: NormalizedClientBuild) => void
4646
/** Call to update server fn resolver content after compilation discovers fns */
4747
updateServerFnResolver: () => void
48-
/** Try to write explicit resolver content now; queues if env not ready */
48+
/** Write explicit server resolver content; queues if env not ready */
4949
tryUpdateServerFnResolver: (content: string) => void
50+
/** Number of times the server fn resolver was written for an environment */
51+
getServerFnResolverWriteCount: (environmentName: string) => number
5052
/** Get the virtual path for a given module ID */
5153
getVirtualPath: (moduleId: string) => string
5254
/** Generate resolver module content from current serverFnsById state.
@@ -276,6 +278,7 @@ export function registerVirtualModules(
276278
let clientBuild: NormalizedClientBuild | undefined
277279
const lastResolverContentByEnvironment: Record<string, string | undefined> =
278280
{}
281+
const resolverWriteCountByEnvironment: Record<string, number> = {}
279282
const hasSeparateProviderEnvironment =
280283
!opts.rscEnabled &&
281284
opts.providerEnvName !== RSBUILD_ENVIRONMENT_NAMES.server
@@ -307,6 +310,8 @@ export function registerVirtualModules(
307310
content !== lastResolverContentByEnvironment[environmentName]
308311
) {
309312
lastResolverContentByEnvironment[environmentName] = content
313+
resolverWriteCountByEnvironment[environmentName] =
314+
(resolverWriteCountByEnvironment[environmentName] ?? 0) + 1
310315
tryWriteModule(environmentName, paths.serverFnResolver, content)
311316
}
312317
}
@@ -591,13 +596,11 @@ export function createFromReadableStream() { throw new Error('RSC SSR decode is
591596
},
592597

593598
tryUpdateServerFnResolver(content: string) {
594-
lastResolverContentByEnvironment[RSBUILD_ENVIRONMENT_NAMES.server] =
595-
content
596-
tryWriteModule(
597-
RSBUILD_ENVIRONMENT_NAMES.server,
598-
paths.serverFnResolver,
599-
content,
600-
)
599+
writeResolverContent(RSBUILD_ENVIRONMENT_NAMES.server, content)
600+
},
601+
602+
getServerFnResolverWriteCount(environmentName: string): number {
603+
return resolverWriteCountByEnvironment[environmentName] ?? 0
601604
},
602605

603606
getVirtualPath(moduleId: string): string {
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
import { describe, expect, test } from 'vitest'
2+
import { RSBUILD_ENVIRONMENT_NAMES } from '../../src/rsbuild/planning'
3+
import { tapServerFnResolverRebuild } from '../../src/rsbuild/server-fn-resolver-rebuild'
4+
import { registerVirtualModules } from '../../src/rsbuild/virtual-modules'
5+
import type { ServerFn } from '../../src/start-compiler/types'
6+
7+
type FinishMakeTap = (compilation: { compiler: object }) => Promise<void>
8+
9+
function serverFn(name: string): ServerFn {
10+
return {
11+
functionName: name,
12+
functionId: name,
13+
extractedFilename: `/app/src/${name}.ts?tss-serverfn-split`,
14+
filename: `/app/src/${name}.ts`,
15+
}
16+
}
17+
18+
function setup(opts: { action?: 'dev' | 'build'; rsc?: boolean } = {}) {
19+
const serverFnsById: Record<string, ServerFn> = {}
20+
const virtualModules = registerVirtualModules(
21+
{
22+
context: { action: opts.action ?? 'dev' },
23+
modifyRspackConfig: () => {},
24+
} as any,
25+
{
26+
root: '/app',
27+
getConfig: () => {
28+
throw new Error('getConfig is not used by these tests')
29+
},
30+
serverFnsById,
31+
providerEnvName: RSBUILD_ENVIRONMENT_NAMES.server,
32+
ssrIsProvider: true,
33+
serializationAdapters: undefined,
34+
getDevClientEntryUrl: () => '/client.js',
35+
rscEnabled: opts.rsc,
36+
scriptFormat: 'module',
37+
},
38+
)
39+
40+
const taps: Array<FinishMakeTap> = []
41+
const compiler = {
42+
hooks: {
43+
finishMake: {
44+
tapPromise: (_options: unknown, tap: FinishMakeTap) => {
45+
taps.push(tap)
46+
},
47+
},
48+
},
49+
}
50+
51+
// Wired the same way as the non-RSC and RSC hooks in plugin.ts.
52+
let rebuilds = 0
53+
let duringNextRebuild: (() => void) | undefined
54+
tapServerFnResolverRebuild(compiler as any, {
55+
name: 'test',
56+
updateResolver: opts.rsc
57+
? () => {
58+
virtualModules.tryUpdateServerFnResolver(
59+
virtualModules.generateCurrentResolverContent(true),
60+
)
61+
}
62+
: () => {
63+
virtualModules.updateServerFnResolver()
64+
},
65+
getResolverWriteCount: () =>
66+
virtualModules.getServerFnResolverWriteCount(
67+
RSBUILD_ENVIRONMENT_NAMES.server,
68+
),
69+
rebuildResolver: () => {
70+
rebuilds++
71+
const during = duringNextRebuild
72+
duringNextRebuild = undefined
73+
during?.()
74+
return Promise.resolve()
75+
},
76+
})
77+
78+
const runFinishMake = async (compilationCompiler: object) => {
79+
for (const tap of taps) {
80+
await tap({ compiler: compilationCompiler })
81+
}
82+
}
83+
84+
return {
85+
serverFnsById,
86+
virtualModules,
87+
rebuilds: () => rebuilds,
88+
duringNextRebuild: (callback: () => void) => {
89+
duringNextRebuild = callback
90+
},
91+
finishMake: () => runFinishMake(compiler),
92+
// Rspack copies finishMake taps into child compilers.
93+
finishMakeInChild: () => runFinishMake({}),
94+
}
95+
}
96+
97+
describe('server function resolver rebuild', () => {
98+
test.each([
99+
{ resolver: 'resolver', rsc: false },
100+
{ resolver: 'RSC resolver', rsc: true },
101+
])(
102+
'rebuilds the $resolver only when its content changes',
103+
async ({ rsc }) => {
104+
const t = setup({ rsc })
105+
106+
await t.finishMake()
107+
expect(t.rebuilds()).toBe(1)
108+
109+
await t.finishMake()
110+
expect(t.rebuilds()).toBe(1)
111+
112+
t.serverFnsById.a = serverFn('a')
113+
await t.finishMake()
114+
expect(t.rebuilds()).toBe(2)
115+
116+
await t.finishMake()
117+
expect(t.rebuilds()).toBe(2)
118+
119+
delete t.serverFnsById.a
120+
await t.finishMake()
121+
expect(t.rebuilds()).toBe(3)
122+
},
123+
)
124+
125+
test('rebuilds after another write to the resolver, even if the content is restored', async () => {
126+
const t = setup()
127+
await t.finishMake()
128+
129+
// For example, a child compilation that wrote content from another registry.
130+
t.virtualModules.tryUpdateServerFnResolver('export {}')
131+
await t.finishMake()
132+
expect(t.rebuilds()).toBe(2)
133+
})
134+
135+
test('rebuilds after a write that lands while the resolver is being rebuilt', async () => {
136+
const t = setup()
137+
// For example, another compiler's finishMake updating the shared registry.
138+
t.duringNextRebuild(() => {
139+
t.serverFnsById.a = serverFn('a')
140+
t.virtualModules.updateServerFnResolver()
141+
})
142+
143+
await t.finishMake()
144+
await t.finishMake()
145+
expect(t.rebuilds()).toBe(2)
146+
})
147+
148+
test('child compilations always rebuild and do not count for the parent', async () => {
149+
const t = setup()
150+
151+
await t.finishMakeInChild()
152+
await t.finishMake()
153+
expect(t.rebuilds()).toBe(2)
154+
155+
await t.finishMakeInChild()
156+
expect(t.rebuilds()).toBe(3)
157+
158+
await t.finishMake()
159+
expect(t.rebuilds()).toBe(3)
160+
})
161+
162+
test('rebuilds in every compilation of a production build', async () => {
163+
const t = setup({ action: 'build' })
164+
165+
await t.finishMake()
166+
await t.finishMake()
167+
expect(t.rebuilds()).toBe(2)
168+
})
169+
})

0 commit comments

Comments
 (0)