|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { vitePluginRscMinimal } from './plugin' |
| 3 | + |
| 4 | +describe('server reference manifest', () => { |
| 5 | + it('preserves and deduplicates existing export names', async () => { |
| 6 | + const plugins = vitePluginRscMinimal({ enableActionEncryption: false }) |
| 7 | + const minimalPlugin = plugins.find( |
| 8 | + (plugin) => plugin.name === 'rsc:minimal', |
| 9 | + )! |
| 10 | + const manager = (minimalPlugin.api as any).manager |
| 11 | + manager.config = { |
| 12 | + command: 'build', |
| 13 | + root: '/root', |
| 14 | + } |
| 15 | + |
| 16 | + const id = '/root/actions.ts' |
| 17 | + manager.serverReferenceMetaMap[id] = { |
| 18 | + importId: id, |
| 19 | + referenceKey: 'existing', |
| 20 | + exportNames: ['cached', 'action', 'cached'], |
| 21 | + } |
| 22 | + |
| 23 | + const useServerPlugin = plugins.find( |
| 24 | + (plugin) => plugin.name === 'rsc:use-server', |
| 25 | + )! |
| 26 | + const transformHandler = (useServerPlugin.transform as any).handler |
| 27 | + await transformHandler.call( |
| 28 | + { |
| 29 | + environment: { name: 'rsc', mode: 'build' }, |
| 30 | + error(error: unknown) { |
| 31 | + throw error |
| 32 | + }, |
| 33 | + }, |
| 34 | + `"use server"; export async function action() {}`, |
| 35 | + id, |
| 36 | + ) |
| 37 | + |
| 38 | + expect(manager.serverReferenceMetaMap[id].exportNames).toEqual([ |
| 39 | + 'cached', |
| 40 | + 'action', |
| 41 | + ]) |
| 42 | + |
| 43 | + const manifestPlugin = plugins.find( |
| 44 | + (plugin) => plugin.name === 'rsc:virtual-vite-rsc/server-references', |
| 45 | + )! |
| 46 | + const loadHandler = (manifestPlugin.load as any).handler |
| 47 | + const manifest = await loadHandler.call( |
| 48 | + { environment: { mode: 'build' } }, |
| 49 | + '\0virtual:vite-rsc/server-references', |
| 50 | + {}, |
| 51 | + ) |
| 52 | + |
| 53 | + expect(manifest.code.match(/\bcached\b/g)).toHaveLength(2) |
| 54 | + expect(manifest.code.match(/\baction\b/g)).toHaveLength(2) |
| 55 | + }) |
| 56 | +}) |
0 commit comments