|
| 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