forked from vitejs/vite-plugin-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.test.ts
More file actions
234 lines (202 loc) · 6.77 KB
/
Copy pathplugin.test.ts
File metadata and controls
234 lines (202 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { describe, expect, test } from 'vitest'
import { vitePluginRscMinimal, type PluginApi } from './plugin'
describe('RscPluginManager compatibility version', () => {
test('throws during build before finalization', () => {
const manager = createManager()
expect(() => manager.getCompatibilityManifest()).toThrow(
/compatibility manifest is not ready/,
)
expect(() => manager.finalizeCompatibilityManifest()).toThrow(
/requires the final assets manifest/,
)
})
test('serializes normalized references and final build fingerprints', () => {
const manager = createFinalizedManager({
root: '/workspace/app',
base: '/base/',
})
expect(manager.getCompatibilityManifest()).toMatchObject({
version: 1,
compatibilityVersion: expect.stringMatching(/^[a-f0-9]{64}$/),
base: '/base/',
assetsManifestHash: expect.stringMatching(/^[a-f0-9]{64}$/),
bundles: {
client: expect.stringMatching(/^[a-f0-9]{64}$/),
rsc: expect.stringMatching(/^[a-f0-9]{64}$/),
},
clientReferences: [
{
id: 'src/button.tsx',
referenceKey: 'button',
renderedExports: ['Button'],
},
],
serverReferences: [
{
id: 'src/actions.ts',
referenceKey: 'actions',
exportNames: ['save'],
},
],
})
expect(manager.getCompatibilityVersion()).toBe(
manager.getCompatibilityManifest().compatibilityVersion,
)
})
test('ignores client exports that are not rendered', () => {
const manager = createFinalizedManager()
const before = manager.getCompatibilityVersion()
manager.clientReferenceMetaMap[
'/workspace/app/src/button.tsx'
]!.exportNames = ['Button', 'Unused']
manager.finalizeCompatibilityManifest()
expect(manager.getCompatibilityVersion()).toBe(before)
})
test('changes when the rendered client export ABI changes', () => {
const manager = createFinalizedManager()
const before = manager.getCompatibilityVersion()
manager.clientReferenceMetaMap[
'/workspace/app/src/button.tsx'
]!.renderedExports = ['Button', 'ButtonIcon']
manager.finalizeCompatibilityManifest()
expect(manager.getCompatibilityVersion()).not.toBe(before)
})
test('changes when the server reference ABI changes', () => {
const manager = createFinalizedManager()
const before = manager.getCompatibilityVersion()
manager.serverReferenceMetaMap[
'/workspace/app/src/actions.ts'
]!.exportNames = ['delete', 'save']
manager.finalizeCompatibilityManifest()
expect(manager.getCompatibilityVersion()).not.toBe(before)
})
test('changes when the client assets manifest changes', () => {
const manager = createFinalizedManager()
const before = manager.getCompatibilityVersion()
manager.buildAssetsManifest = {
...manager.buildAssetsManifest!,
clientReferenceDeps: {
button: {
js: ['/assets/button.new.js'],
css: [],
},
},
}
manager.finalizeCompatibilityManifest()
expect(manager.getCompatibilityVersion()).not.toBe(before)
})
test('changes when final client bundle content changes', () => {
const manager = createFinalizedManager()
const before = manager.getCompatibilityVersion()
manager.bundles.client = createBundle({
'assets/button.js': 'export const Button = "new"',
})
manager.finalizeCompatibilityManifest()
expect(manager.getCompatibilityVersion()).not.toBe(before)
})
test('changes when final rsc bundle content changes', () => {
const manager = createFinalizedManager()
const before = manager.getCompatibilityVersion()
manager.bundles.rsc = createBundle({
'index.js': 'export const root = "new-rsc"',
})
manager.finalizeCompatibilityManifest()
expect(manager.getCompatibilityVersion()).not.toBe(before)
})
test('changes when server action encryption key identity changes', () => {
const manager = createFinalizedManager()
manager.serverActionEncryptionKeyHash = 'key-a'
manager.finalizeCompatibilityManifest()
const before = manager.getCompatibilityVersion()
manager.serverActionEncryptionKeyHash = 'key-b'
manager.finalizeCompatibilityManifest()
expect(manager.getCompatibilityVersion()).not.toBe(before)
})
test('is stable across different absolute roots', () => {
const first = createFinalizedManager({ root: '/first/root' })
first.clientReferenceMetaMap = {
'/first/root/src/button.tsx': {
importId: '/first/root/src/button.tsx',
referenceKey: 'button',
exportNames: ['Button'],
renderedExports: ['Button'],
},
}
first.finalizeCompatibilityManifest()
const second = createFinalizedManager({ root: '/second/root' })
second.clientReferenceMetaMap = {
'/second/root/src/button.tsx': {
importId: '/second/root/src/button.tsx',
referenceKey: 'button',
exportNames: ['Button'],
renderedExports: ['Button'],
},
}
second.finalizeCompatibilityManifest()
expect(second.getCompatibilityVersion()).toBe(
first.getCompatibilityVersion(),
)
})
})
type ManagerOptions = {
base?: string
root?: string
}
function createFinalizedManager(options: ManagerOptions = {}) {
const manager = createManager(options)
manager.clientReferenceMetaMap = {
[`${options.root ?? '/workspace/app'}/src/button.tsx`]: {
importId: `${options.root ?? '/workspace/app'}/src/button.tsx`,
referenceKey: 'button',
exportNames: ['Button'],
renderedExports: ['Button'],
},
}
manager.serverReferenceMetaMap = {
[`${options.root ?? '/workspace/app'}/src/actions.ts`]: {
importId: `${options.root ?? '/workspace/app'}/src/actions.ts`,
referenceKey: 'actions',
exportNames: ['save'],
},
}
manager.buildAssetsManifest = {
bootstrapScriptContent: 'import("/assets/index.js")',
clientReferenceDeps: {
button: {
js: ['/assets/button.js'],
css: [],
},
},
}
manager.bundles = {
client: createBundle({
'assets/button.js': 'export const Button = "old"',
}),
rsc: createBundle({
'index.js': 'export const root = "rsc"',
}),
}
manager.finalizeCompatibilityManifest()
return manager
}
function createManager({
base = '/',
root = '/workspace/app',
}: ManagerOptions = {}) {
const [plugin] = vitePluginRscMinimal()
const manager = (plugin as { api: PluginApi }).api.manager
manager.config = { base, command: 'build', root } as any
return manager
}
function createBundle(chunks: Record<string, string>) {
return Object.fromEntries(
Object.entries(chunks).map(([fileName, code]) => [
fileName,
{
type: 'chunk',
fileName,
code,
},
]),
) as any
}