Skip to content

Commit 0d27505

Browse files
committed
feat(rsc): expose framework utility helpers
(cherry picked from commit 262d7b4)
1 parent 8b82c9c commit 0d27505

4 files changed

Lines changed: 65 additions & 0 deletions

File tree

packages/plugin-rsc/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"./package.json": "./package.json",
2626
"./types": "./types/index.d.ts",
2727
".": "./dist/index.js",
28+
"./utils": "./dist/utils.js",
2829
"./transforms": "./dist/transforms/index.js",
2930
"./*": "./dist/*.js"
3031
},
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import type { DevEnvironment } from 'vite'
2+
import { describe, expect, it } from 'vitest'
3+
import { hashString, normalizeViteImportAnalysisUrl } from './utils'
4+
5+
function createEnvironment(options?: {
6+
consumer?: 'client' | 'server'
7+
timestamp?: number
8+
}) {
9+
return {
10+
config: {
11+
root: '/root',
12+
consumer: options?.consumer ?? 'server',
13+
},
14+
moduleGraph: {
15+
getModuleById: () =>
16+
options?.timestamp
17+
? { lastHMRTimestamp: options.timestamp }
18+
: undefined,
19+
},
20+
} as unknown as DevEnvironment
21+
}
22+
23+
describe(hashString, () => {
24+
it('returns a stable short sha256 hash', () => {
25+
expect(hashString('test')).toBe('9f86d081884c')
26+
expect(hashString('test')).toBe(hashString('test'))
27+
expect(hashString('other')).not.toBe(hashString('test'))
28+
})
29+
})
30+
31+
describe(normalizeViteImportAnalysisUrl, () => {
32+
it('normalizes root files and virtual ids', () => {
33+
const environment = createEnvironment()
34+
expect(
35+
normalizeViteImportAnalysisUrl(environment, '/root/src/action.ts'),
36+
).toBe('/src/action.ts')
37+
expect(normalizeViteImportAnalysisUrl(environment, 'virtual:action')).toBe(
38+
'/@id/virtual:action',
39+
)
40+
})
41+
42+
it('injects HMR timestamps for client consumers or when requested', () => {
43+
const id = '/root/src/action.ts'
44+
expect(
45+
normalizeViteImportAnalysisUrl(
46+
createEnvironment({ consumer: 'client', timestamp: 123 }),
47+
id,
48+
),
49+
).toBe('/src/action.ts?t=123')
50+
expect(
51+
normalizeViteImportAnalysisUrl(
52+
createEnvironment({ timestamp: 456 }),
53+
id,
54+
{ injectHMRTimestamp: true },
55+
),
56+
).toBe('/src/action.ts?t=456')
57+
expect(
58+
normalizeViteImportAnalysisUrl(createEnvironment({ timestamp: 789 }), id),
59+
).toBe('/src/action.ts')
60+
})
61+
})

packages/plugin-rsc/src/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { hashString } from './plugins/utils'
2+
export { normalizeViteImportAnalysisUrl } from './plugins/vite-utils'

packages/plugin-rsc/tsdown.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export default defineConfig({
1919
'src/react/rsc.ts',
2020
'src/react/rsc/server.ts',
2121
'src/react/rsc/client.ts',
22+
'src/utils.ts',
2223
'src/transforms/index.ts',
2324
'src/plugins/cjs.ts',
2425
'src/utils/rpc.ts',

0 commit comments

Comments
 (0)