Skip to content

Commit 9795ffb

Browse files
committed
feat!: rework dist file names
1 parent 2c3e1ba commit 9795ffb

File tree

7 files changed

+58
-103
lines changed

7 files changed

+58
-103
lines changed

docs/kit/rpc.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,12 @@ When using `vite devtools build` to create a static DevTools build, the server c
115115
#### How It Works
116116

117117
1. At build time, `dumpFunctions()` executes your RPC handlers with predefined arguments
118-
2. Results are stored in `.vdt-rpc-dump.json` in the build output
118+
2. Results are stored in `.rpc-dump/index.json` in the build output
119119
3. The static client reads from this JSON file instead of making live RPC calls
120120

121+
Dump shard files are written to `.rpc-dump/*.json`. Function names in shard file keys replace `:` with `~` (for example `my-plugin:get-data` -> `my-plugin~get-data`).
122+
Query record maps are embedded directly in `.rpc-dump/index.json`; no per-function index files are generated.
123+
121124
#### Static Functions (Recommended)
122125

123126
Functions with `type: 'static'` are **automatically dumped** with no arguments:

packages/core/src/node/__tests__/static-dump.test.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import { defineRpcFunction } from '@vitejs/devtools-kit'
2-
import {
3-
DEVTOOLS_RPC_DUMP_QUERY_DIR,
4-
DEVTOOLS_RPC_DUMP_QUERY_FALLBACK_FILENAME,
5-
DEVTOOLS_RPC_DUMP_QUERY_INDEX_FILENAME,
6-
DEVTOOLS_RPC_DUMP_QUERY_RECORDS_DIRNAME,
7-
DEVTOOLS_RPC_DUMP_STATIC_DIR,
8-
} from '@vitejs/devtools-kit/constants'
2+
import { DEVTOOLS_RPC_DUMP_DIRNAME } from '@vitejs/devtools-kit/constants'
93
import { describe, expect, it } from 'vitest'
104
import { collectStaticRpcDump } from '../static-dump'
115

@@ -18,7 +12,7 @@ describe('collectStaticRpcDump', () => {
1812
})
1913

2014
const result = await collectStaticRpcDump([getVersion], {})
21-
const expectedPath = `${DEVTOOLS_RPC_DUMP_STATIC_DIR}/test%3Aget-version.json`
15+
const expectedPath = `${DEVTOOLS_RPC_DUMP_DIRNAME}/test~get-version.static.json`
2216

2317
expect(result.manifest['test:get-version']).toEqual({
2418
type: 'static',
@@ -44,26 +38,28 @@ describe('collectStaticRpcDump', () => {
4438
})
4539

4640
const result = await collectStaticRpcDump([getItem], {})
47-
const basePath = `${DEVTOOLS_RPC_DUMP_QUERY_DIR}/test%3Aget-item`
48-
const queryRecordsPath = `${basePath}/${DEVTOOLS_RPC_DUMP_QUERY_RECORDS_DIRNAME}/`
49-
const manifest = result.manifest['test:get-item'] as { type: 'query', index: string }
50-
const index = result.files[manifest.index] as {
41+
const basePath = `${DEVTOOLS_RPC_DUMP_DIRNAME}/test~get-item`
42+
const manifest = result.manifest['test:get-item'] as {
43+
type: 'query'
5144
records: Record<string, string>
5245
fallback?: string
5346
}
5447

5548
expect(manifest).toEqual({
5649
type: 'query',
57-
index: `${basePath}/${DEVTOOLS_RPC_DUMP_QUERY_INDEX_FILENAME}`,
50+
records: expect.any(Object),
51+
fallback: `${basePath}.fallback.json`,
5852
})
59-
expect(Object.keys(index.records)).toHaveLength(2)
60-
expect(index.fallback).toBe(`${basePath}/${DEVTOOLS_RPC_DUMP_QUERY_FALLBACK_FILENAME}`)
53+
expect(Object.keys(manifest.records)).toHaveLength(2)
6154

62-
const recordPaths = Object.values(index.records)
55+
const recordPaths = Object.values(manifest.records)
6356
for (const path of recordPaths) {
64-
expect(path.startsWith(queryRecordsPath)).toBe(true)
57+
expect(path.startsWith(`${basePath}.record.`)).toBe(true)
58+
expect(path.endsWith('.json')).toBe(true)
6559
expect(path in result.files).toBe(true)
6660
}
61+
62+
expect(Object.keys(result.files).some(path => path.endsWith('.index.json'))).toBe(false)
6763
})
6864

6965
it('skips query functions without dump config', async () => {

packages/core/src/node/cli-commands.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
DEVTOOLS_DIRNAME,
99
DEVTOOLS_DOCK_IMPORTS_FILENAME,
1010
DEVTOOLS_MOUNT_PATH,
11-
DEVTOOLS_RPC_DUMP_API_DIRNAME,
11+
DEVTOOLS_RPC_DUMP_DIRNAME,
1212
DEVTOOLS_RPC_DUMP_MANIFEST_FILENAME,
1313
} from '@vitejs/devtools-kit/constants'
1414
import c from 'ansis'
@@ -104,7 +104,7 @@ export async function build(options: BuildOptions) {
104104
await fs.cp(distDir, join(outDir, baseUrl), { recursive: true })
105105
}
106106

107-
await fs.mkdir(resolve(devToolsRoot, DEVTOOLS_RPC_DUMP_API_DIRNAME), { recursive: true })
107+
await fs.mkdir(resolve(devToolsRoot, DEVTOOLS_RPC_DUMP_DIRNAME), { recursive: true })
108108
await fs.writeFile(resolve(devToolsRoot, DEVTOOLS_CONNECTION_META_FILENAME), JSON.stringify({ backend: 'static' }, null, 2), 'utf-8')
109109
await fs.writeFile(resolve(devToolsRoot, DEVTOOLS_DOCK_IMPORTS_FILENAME), renderDockImportsMap(devtools.context.docks.values()), 'utf-8')
110110

packages/core/src/node/static-dump.ts

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import type { RpcDumpRecord, RpcFunctionDefinitionAny } from '@vitejs/devtools-rpc'
22
import {
3-
DEVTOOLS_RPC_DUMP_QUERY_DIR,
4-
DEVTOOLS_RPC_DUMP_QUERY_FALLBACK_FILENAME,
5-
DEVTOOLS_RPC_DUMP_QUERY_INDEX_FILENAME,
6-
DEVTOOLS_RPC_DUMP_QUERY_RECORDS_DIRNAME,
7-
DEVTOOLS_RPC_DUMP_STATIC_DIR,
3+
DEVTOOLS_RPC_DUMP_DIRNAME,
84
} from '@vitejs/devtools-kit/constants'
95
import { dumpFunctions, getRpcHandler } from '@vitejs/devtools-rpc'
106

@@ -15,7 +11,8 @@ export interface StaticRpcDumpManifestStaticEntry {
1511

1612
export interface StaticRpcDumpManifestQueryEntry {
1713
type: 'query'
18-
index: string
14+
records: Record<string, string>
15+
fallback?: string
1916
}
2017

2118
export type StaticRpcDumpManifestValue
@@ -30,24 +27,20 @@ export interface StaticRpcDumpCollection {
3027
files: Record<string, any>
3128
}
3229

33-
function encodeName(name: string): string {
34-
return encodeURIComponent(name)
30+
function makeDumpKey(name: string): string {
31+
return encodeURIComponent(name.replaceAll(':', '~'))
3532
}
3633

3734
function makeStaticPath(name: string): string {
38-
return `${DEVTOOLS_RPC_DUMP_STATIC_DIR}/${encodeName(name)}.json`
39-
}
40-
41-
function makeQueryIndexPath(name: string): string {
42-
return `${DEVTOOLS_RPC_DUMP_QUERY_DIR}/${encodeName(name)}/${DEVTOOLS_RPC_DUMP_QUERY_INDEX_FILENAME}`
35+
return `${DEVTOOLS_RPC_DUMP_DIRNAME}/${makeDumpKey(name)}.static.json`
4336
}
4437

4538
function makeQueryRecordPath(name: string, hash: string): string {
46-
return `${DEVTOOLS_RPC_DUMP_QUERY_DIR}/${encodeName(name)}/${DEVTOOLS_RPC_DUMP_QUERY_RECORDS_DIRNAME}/${hash}.json`
39+
return `${DEVTOOLS_RPC_DUMP_DIRNAME}/${makeDumpKey(name)}.record.${hash}.json`
4740
}
4841

4942
function makeQueryFallbackPath(name: string): string {
50-
return `${DEVTOOLS_RPC_DUMP_QUERY_DIR}/${encodeName(name)}/${DEVTOOLS_RPC_DUMP_QUERY_FALLBACK_FILENAME}`
43+
return `${DEVTOOLS_RPC_DUMP_DIRNAME}/${makeDumpKey(name)}.fallback.json`
5144
}
5245

5346
async function resolveRecord(record: RpcDumpRecord | (() => Promise<RpcDumpRecord>)): Promise<RpcDumpRecord> {
@@ -87,11 +80,8 @@ export async function collectStaticRpcDump(
8780
if (!(definition.name in store.definitions))
8881
continue
8982

90-
const indexPath = makeQueryIndexPath(definition.name)
91-
const index: {
92-
records: Record<string, string>
93-
fallback?: string
94-
} = {
83+
const queryEntry: StaticRpcDumpManifestQueryEntry = {
84+
type: 'query',
9585
records: {},
9686
}
9787

@@ -107,23 +97,19 @@ export async function collectStaticRpcDump(
10797
if (key === 'fallback') {
10898
const path = makeQueryFallbackPath(definition.name)
10999
files[path] = record
110-
index.fallback = path
100+
queryEntry.fallback = path
111101
}
112102
else {
113103
const path = makeQueryRecordPath(definition.name, key)
114104
files[path] = record
115-
index.records[key] = path
105+
queryEntry.records[key] = path
116106
}
117107
}
118108

119-
if (!Object.keys(index.records).length && !index.fallback)
109+
if (!Object.keys(queryEntry.records).length && !queryEntry.fallback)
120110
continue
121111

122-
files[indexPath] = index
123-
manifest[definition.name] = {
124-
type: 'query',
125-
index: indexPath,
126-
}
112+
manifest[definition.name] = queryEntry
127113
}
128114

129115
return {

packages/kit/src/client/static-rpc.test.ts

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
import { hash } from 'ohash'
22
import { describe, expect, it } from 'vitest'
3-
import {
4-
DEVTOOLS_RPC_DUMP_QUERY_DIR,
5-
DEVTOOLS_RPC_DUMP_QUERY_FALLBACK_FILENAME,
6-
DEVTOOLS_RPC_DUMP_QUERY_INDEX_FILENAME,
7-
DEVTOOLS_RPC_DUMP_QUERY_RECORDS_DIRNAME,
8-
DEVTOOLS_RPC_DUMP_STATIC_DIR,
9-
} from '../constants'
3+
import { DEVTOOLS_RPC_DUMP_DIRNAME } from '../constants'
104
import { createStaticRpcCaller } from './static-rpc'
115

12-
const DEMO_STATIC_VERSION_PATH = `${DEVTOOLS_RPC_DUMP_STATIC_DIR}/demo%3Aversion.json`
13-
const DEMO_QUERY_BASE_PATH = `${DEVTOOLS_RPC_DUMP_QUERY_DIR}/demo%3Aget-item`
14-
const DEMO_QUERY_INDEX_PATH = `${DEMO_QUERY_BASE_PATH}/${DEVTOOLS_RPC_DUMP_QUERY_INDEX_FILENAME}`
15-
const DEMO_QUERY_RECORDS_PATH = `${DEMO_QUERY_BASE_PATH}/${DEVTOOLS_RPC_DUMP_QUERY_RECORDS_DIRNAME}`
16-
const DEMO_QUERY_FALLBACK_PATH = `${DEMO_QUERY_BASE_PATH}/${DEVTOOLS_RPC_DUMP_QUERY_FALLBACK_FILENAME}`
6+
const DEMO_STATIC_VERSION_PATH = `${DEVTOOLS_RPC_DUMP_DIRNAME}/demo~version.static.json`
7+
const DEMO_QUERY_BASE_PATH = `${DEVTOOLS_RPC_DUMP_DIRNAME}/demo~get-item`
8+
const DEMO_QUERY_RECORDS_PATH = `${DEMO_QUERY_BASE_PATH}.record`
9+
const DEMO_QUERY_FALLBACK_PATH = `${DEMO_QUERY_BASE_PATH}.fallback.json`
1710

1811
describe('createStaticRpcCaller', () => {
1912
it('loads static rpc shards lazily and caches by file path', async () => {
@@ -41,26 +34,21 @@ describe('createStaticRpcCaller', () => {
4134
{
4235
'demo:get-item': {
4336
type: 'query',
44-
index: DEMO_QUERY_INDEX_PATH,
37+
records: {
38+
[hash(['a'])]: `${DEMO_QUERY_RECORDS_PATH}.ok.json`,
39+
[hash(['boom'])]: `${DEMO_QUERY_RECORDS_PATH}.error.json`,
40+
},
41+
fallback: DEMO_QUERY_FALLBACK_PATH,
4542
},
4643
},
4744
async (path) => {
48-
if (path === DEMO_QUERY_INDEX_PATH) {
49-
return {
50-
records: {
51-
[hash(['a'])]: `${DEMO_QUERY_RECORDS_PATH}/ok.json`,
52-
[hash(['boom'])]: `${DEMO_QUERY_RECORDS_PATH}/error.json`,
53-
},
54-
fallback: DEMO_QUERY_FALLBACK_PATH,
55-
}
56-
}
57-
if (path.endsWith('/ok.json')) {
45+
if (path.endsWith('.ok.json')) {
5846
return {
5947
inputs: ['a'],
6048
output: { id: 'a' },
6149
}
6250
}
63-
if (path.endsWith('/error.json')) {
51+
if (path.endsWith('.error.json')) {
6452
return {
6553
inputs: ['boom'],
6654
error: {
@@ -69,7 +57,7 @@ describe('createStaticRpcCaller', () => {
6957
},
7058
}
7159
}
72-
if (path.endsWith('/fallback.json')) {
60+
if (path.endsWith('.fallback.json')) {
7361
return {
7462
inputs: [],
7563
output: null,

packages/kit/src/client/static-rpc.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ export interface StaticRpcManifestStaticEntry {
77

88
export interface StaticRpcManifestQueryEntry {
99
type: 'query'
10-
index: string
10+
records: Record<string, string>
11+
fallback?: string
1112
}
1213

1314
export type StaticRpcManifestEntry
@@ -26,11 +27,6 @@ export interface StaticRpcRecord {
2627
}
2728
}
2829

29-
export interface StaticRpcQueryIndex {
30-
records: Record<string, string>
31-
fallback?: string
32-
}
33-
3430
function isStaticEntry(value: unknown): value is StaticRpcManifestStaticEntry {
3531
return typeof value === 'object'
3632
&& value !== null
@@ -42,7 +38,8 @@ function isQueryEntry(value: unknown): value is StaticRpcManifestQueryEntry {
4238
return typeof value === 'object'
4339
&& value !== null
4440
&& (value as any).type === 'query'
45-
&& typeof (value as any).index === 'string'
41+
&& typeof (value as any).records === 'object'
42+
&& (value as any).records !== null
4643
}
4744

4845
function isRecord(value: unknown): value is StaticRpcRecord {
@@ -65,7 +62,6 @@ export function createStaticRpcCaller(
6562
fetchJson: (path: string) => Promise<any>,
6663
) {
6764
const staticCache = new Map<string, Promise<any>>()
68-
const queryIndexCache = new Map<string, Promise<StaticRpcQueryIndex>>()
6965
const queryRecordCache = new Map<string, Promise<StaticRpcRecord>>()
7066

7167
async function loadStatic(entry: StaticRpcManifestStaticEntry): Promise<any> {
@@ -79,13 +75,6 @@ export function createStaticRpcCaller(
7975
return data
8076
}
8177

82-
async function loadQueryIndex(entry: StaticRpcManifestQueryEntry): Promise<StaticRpcQueryIndex> {
83-
if (!queryIndexCache.has(entry.index)) {
84-
queryIndexCache.set(entry.index, fetchJson(entry.index))
85-
}
86-
return await queryIndexCache.get(entry.index)!
87-
}
88-
8978
async function loadQueryRecord(path: string): Promise<StaticRpcRecord> {
9079
if (!queryRecordCache.has(path)) {
9180
queryRecordCache.set(path, fetchJson(path))
@@ -109,17 +98,16 @@ export function createStaticRpcCaller(
10998
}
11099

111100
if (isQueryEntry(entry)) {
112-
const index = await loadQueryIndex(entry)
113101
const argsHash = hash(args)
114-
const recordPath = index.records[argsHash]
102+
const recordPath = entry.records[argsHash]
115103

116104
if (recordPath) {
117105
const record = await loadQueryRecord(recordPath)
118106
return resolveRecordOutput(record)
119107
}
120108

121-
if (index.fallback) {
122-
const fallback = await loadQueryRecord(index.fallback)
109+
if (entry.fallback) {
110+
const fallback = await loadQueryRecord(entry.fallback)
123111
return resolveRecordOutput(fallback)
124112
}
125113

packages/kit/src/constants.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,11 @@ export const DEVTOOLS_MOUNT_PATH = '/.devtools/'
55
export const DEVTOOLS_MOUNT_PATH_NO_TRAILING_SLASH = '/.devtools'
66
export const DEVTOOLS_DIRNAME = '.devtools'
77

8-
export const DEVTOOLS_CONNECTION_META_FILENAME = '.vdt-connection.json'
9-
export const DEVTOOLS_RPC_DUMP_MANIFEST_FILENAME = '.vdt-rpc-dump.json'
10-
export const DEVTOOLS_DOCK_IMPORTS_FILENAME = '.devtools-imports.js'
11-
export const DEVTOOLS_DOCK_IMPORTS_VIRTUAL_ID = '/.devtools-imports'
12-
export const DEVTOOLS_RPC_DUMP_API_DIRNAME = 'api'
13-
14-
export const DEVTOOLS_RPC_DUMP_STATIC_DIR = 'api/rpc/static'
15-
export const DEVTOOLS_RPC_DUMP_QUERY_DIR = 'api/rpc/query'
16-
export const DEVTOOLS_RPC_DUMP_QUERY_RECORDS_DIRNAME = 'records'
17-
export const DEVTOOLS_RPC_DUMP_QUERY_INDEX_FILENAME = 'index.json'
18-
export const DEVTOOLS_RPC_DUMP_QUERY_FALLBACK_FILENAME = 'fallback.json'
8+
export const DEVTOOLS_CONNECTION_META_FILENAME = '.connection.json'
9+
export const DEVTOOLS_RPC_DUMP_MANIFEST_FILENAME = '.rpc-dump/index.json'
10+
export const DEVTOOLS_DOCK_IMPORTS_FILENAME = '.client-imports.js'
11+
export const DEVTOOLS_DOCK_IMPORTS_VIRTUAL_ID = '/.devtools/client-imports'
12+
export const DEVTOOLS_RPC_DUMP_DIRNAME = '.rpc-dump'
1913

2014
export const DEFAULT_CATEGORIES_ORDER: Record<string, number> = {
2115
'~viteplus': -1000,

0 commit comments

Comments
 (0)