diff --git a/packages/kit/src/node/vite-host.test.ts b/packages/kit/src/node/vite-host.test.ts new file mode 100644 index 000000000..f5710182c --- /dev/null +++ b/packages/kit/src/node/vite-host.test.ts @@ -0,0 +1,35 @@ +import type { ResolvedConfig } from 'vite' +import { homedir } from 'node:os' +import { join } from 'node:path' +import { describe, expect, it } from 'vitest' +import { createViteDevToolsHost } from './vite-host' + +/** Only `root` is read off the Vite config by the host's storage layout. */ +function storageDir(scope: 'workspace' | 'project' | 'global', workspaceRoot?: string): string { + return createViteDevToolsHost({ + viteConfig: { root: '/repo/apps/site' } as unknown as ResolvedConfig, + workspaceRoot, + }).getStorageDir(scope) +} + +describe('storage scopes', () => { + it('keeps the workspace scope committable', () => { + expect(storageDir('workspace', '/repo')).toBe(join('/repo', '.vite/devtools')) + }) + + it('places the per-checkout scope under node_modules', () => { + expect(storageDir('project', '/repo')).toBe(join('/repo', 'node_modules/.vite/devtools')) + }) + + it('places the per-user scope in the home directory', () => { + expect(storageDir('global', '/repo')).toBe(join(homedir(), '.vite/devtools')) + }) + + it('keeps per-checkout data out of the per-user directory', () => { + expect(storageDir('project', '/repo')).not.toBe(storageDir('global', '/repo')) + }) + + it('falls back to the Vite root for the workspace root', () => { + expect(storageDir('workspace')).toBe(join('/repo/apps/site', '.vite/devtools')) + }) +}) diff --git a/packages/kit/src/node/vite-host.ts b/packages/kit/src/node/vite-host.ts index 9b2fa2ae1..0647c7414 100644 --- a/packages/kit/src/node/vite-host.ts +++ b/packages/kit/src/node/vite-host.ts @@ -112,10 +112,21 @@ export function createViteDevToolsHost(options: CreateViteDevToolsHostOptions): const reachable = host === '0.0.0.0' || host === '::' || !host ? 'localhost' : host return `${https ? 'https' : 'http'}://${reachable}:${port}` }, + /** + * Mirror devframe's storage layout: `workspace` is committable and + * team-shared, `project` rotates with the checkout's `node_modules`, and + * `global` is per-user. Each scope needs its own directory, or a + * per-checkout file lands where every other project reads it. + * + * The workspace dir deliberately avoids `.vite-devtools`, which is the + * CLI's default build `outDir` and gets wiped by every build. + */ getStorageDir(scope) { - return scope === 'workspace' - ? join(workspaceRoot, 'node_modules/.vite/devtools') - : join(homedir(), '.vite/devtools') + if (scope === 'workspace') + return join(workspaceRoot, '.vite/devtools') + if (scope === 'project') + return join(workspaceRoot, 'node_modules/.vite/devtools') + return join(homedir(), '.vite/devtools') }, } }