Skip to content

Commit 211622b

Browse files
committed
refactor: cleanup vitest-transform schema support
1 parent fa03267 commit 211622b

File tree

6 files changed

+61
-12
lines changed

6 files changed

+61
-12
lines changed

packages/extension/src/extension.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ class VitestExtension {
5757
this.testController.resolveHandler = item => this.resolveTestFile(item)
5858
this.loadingTestItem = this.testController.createTestItem('_resolving', 'Resolving Vitest...')
5959
this.loadingTestItem.sortText = '.0' // show it first
60-
this.testTree = new TestTree(this.testController, this.loadingTestItem)
61-
this.tagsManager = new TagsManager(this.testTree)
62-
this.debugManager = new DebugManager()
6360
this.schemaProvider = new SchemaProvider(
6461
async (apiId, project, environment, file) => {
6562
const api = this.api?.folderAPIs.find(a => a.id === apiId)
6663
return api?.getTransformedModule(project, environment, file) ?? null
6764
},
6865
)
66+
this.testTree = new TestTree(this.testController, this.loadingTestItem, this.schemaProvider)
67+
this.tagsManager = new TagsManager(this.testTree)
68+
this.debugManager = new DebugManager()
6969
}
7070

7171
private _defineTestProfilePromise: Promise<void> | undefined
@@ -371,7 +371,7 @@ class VitestExtension {
371371
label += environment
372372
return {
373373
label,
374-
uriParts: [api.id, project.name, environment],
374+
uriParts: [api.id, project.name, environment.name, environment.transformTimestamp],
375375
}
376376
})
377377
})
@@ -385,9 +385,9 @@ class VitestExtension {
385385
return
386386
}
387387
try {
388-
const [apiId, projectName, environment] = pick.uriParts
388+
const [apiId, projectName, environment, t] = pick.uriParts
389389
const uri = vscode.Uri.parse(
390-
`vitest-transform://${currentUri.fsPath}.js?apiId=${apiId}&project=${projectName}&environment=${environment}`,
390+
`vitest-transform://${currentUri.fsPath}.js?apiId=${apiId}&project=${projectName}&environment=${environment}&t=${t}`,
391391
)
392392
const doc = await vscode.workspace.openTextDocument(uri)
393393
await vscode.window.showTextDocument(doc, { preview: false })

packages/extension/src/schemaProvider.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,28 @@ import * as vscode from 'vscode'
33
export class SchemaProvider implements vscode.TextDocumentContentProvider, vscode.Disposable {
44
private disposables: vscode.Disposable[] = []
55

6+
private _onDidChangeEvents = new vscode.EventEmitter<vscode.Uri>()
7+
68
constructor(
79
private getTransformedModule: (apiId: string, project: string, environment: string, file: string) => Promise<string | null>,
810
) {
911
this.disposables.push(vscode.workspace.registerTextDocumentContentProvider('vitest-transform', this))
12+
this.disposables.push(this._onDidChangeEvents)
13+
}
14+
15+
public onDidChange = this._onDidChangeEvents.event
16+
17+
public emitChange(uri: vscode.Uri) {
18+
const cachedFsPaths = this._cachedFsPaths.get(uri.fsPath)
19+
if (cachedFsPaths) {
20+
cachedFsPaths.forEach((uri) => {
21+
this._onDidChangeEvents.fire(uri)
22+
})
23+
}
1024
}
1125

26+
private _cachedFsPaths = new Map<string, Set<vscode.Uri>>()
27+
1228
async provideTextDocumentContent(uri: vscode.Uri): Promise<string> {
1329
const searchParams = new URLSearchParams(uri.query)
1430
const apiId = searchParams.get('apiId')
@@ -18,6 +34,24 @@ export class SchemaProvider implements vscode.TextDocumentContentProvider, vscod
1834
throw new Error(`Cannot parse the schema: ${uri.toString()}`)
1935
}
2036
const fsPath = uri.fsPath.slice(0, -3) // remove .js
37+
38+
let _cachedUris = this._cachedFsPaths.get(fsPath)
39+
if (!_cachedUris) {
40+
_cachedUris = new Set()
41+
}
42+
else {
43+
// remove older files from the same environment
44+
_cachedUris.forEach((uri) => {
45+
const query = uri.query.replace(/&t=\d+/, '')
46+
const currentQuery = uri.query.replace(/&t=\d+/, '')
47+
if (query === currentQuery) {
48+
_cachedUris?.delete(uri)
49+
}
50+
})
51+
}
52+
_cachedUris.add(uri)
53+
this._cachedFsPaths.set(fsPath, _cachedUris)
54+
2155
const content = await this.getTransformedModule(apiId, project, environment, fsPath)
2256
if (content == null) {
2357
throw new Error(`The file ${fsPath} was not processed by Vite yet.`)
@@ -26,6 +60,7 @@ export class SchemaProvider implements vscode.TextDocumentContentProvider, vscod
2660
}
2761

2862
dispose() {
63+
this._cachedFsPaths.clear()
2964
this.disposables.forEach(d => d.dispose())
3065
}
3166
}

packages/extension/src/testTree.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { RunnerTask, RunnerTestFile } from 'vitest'
22
import type { ExtensionTestFileSpecification } from 'vitest-vscode-shared'
33
import type { VitestFolderAPI } from './api'
4+
import type { SchemaProvider } from './schemaProvider'
45
import type { TestFileMetadata } from './testTreeData'
56
import { realpathSync } from 'node:fs'
67
import { resolve } from 'node:path'
@@ -30,6 +31,7 @@ export class TestTree extends vscode.Disposable {
3031
constructor(
3132
private readonly controller: vscode.TestController,
3233
private readonly loaderItem: vscode.TestItem,
34+
schemaProvider: SchemaProvider,
3335
) {
3436
super(() => {
3537
this.folderItems.clear()
@@ -38,7 +40,7 @@ export class TestTree extends vscode.Disposable {
3840
this.testItemsByFile.clear()
3941
this.watcher.dispose()
4042
})
41-
this.watcher = new ExtensionWatcher(this)
43+
this.watcher = new ExtensionWatcher(this, schemaProvider)
4244
}
4345

4446
public getFileTestItems(fsPath: string) {

packages/extension/src/watcher.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { VitestFolderAPI } from './api'
2+
import type { SchemaProvider } from './schemaProvider'
23
import type { TestTree } from './testTree'
34
import { relative } from 'node:path'
45
import { normalize } from 'pathe'
@@ -10,7 +11,10 @@ export class ExtensionWatcher extends vscode.Disposable {
1011
private watcherByFolder = new Map<vscode.WorkspaceFolder, vscode.FileSystemWatcher>()
1112
private apisByFolder = new WeakMap<vscode.WorkspaceFolder, VitestFolderAPI[]>()
1213

13-
constructor(private readonly testTree: TestTree) {
14+
constructor(
15+
private readonly testTree: TestTree,
16+
private readonly schemaProvider: SchemaProvider,
17+
) {
1418
super(() => {
1519
this.reset()
1620
log.verbose?.('[VSCODE] Watcher disposed')
@@ -42,9 +46,11 @@ export class ExtensionWatcher extends vscode.Disposable {
4246
watcher.onDidDelete((uri) => {
4347
log.verbose?.('[VSCODE] File deleted:', this.relative(api, uri))
4448
this.testTree.removeFile(normalize(uri.fsPath))
49+
this.schemaProvider.emitChange(uri)
4550
})
4651

4752
watcher.onDidChange(async (uri) => {
53+
this.schemaProvider.emitChange(uri)
4854
const path = normalize(uri.fsPath)
4955
if (await this.shouldIgnoreFile(api, path, uri)) {
5056
return

packages/shared/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ export interface ExtensionUserConsoleLog extends UserConsoleLog {
3939

4040
export interface ExtensionEnvironment {
4141
name: string
42-
environments: string[]
42+
environments: {
43+
name: string
44+
transformTimestamp: number
45+
}[]
4346
}
4447

4548
export interface ExtensionWorkerTransport {

packages/worker/src/worker.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,20 @@ export class ExtensionWorker implements ExtensionWorkerTransport {
106106

107107
getModuleEnvironments(moduleId: string): ExtensionEnvironment[] {
108108
return this.vitest.projects.map((project) => {
109-
const environments = new Set<string>()
109+
const environments = new Map<string, { timestamp: number }>()
110110
for (const name in project.vite.environments) {
111111
const environment = project.vite.environments[name]
112112
const nodes = [...environment.moduleGraph.getModulesByFile(moduleId) || []]
113113
if (nodes.some(n => n.transformResult)) {
114-
environments.add(name)
114+
environments.set(name, { timestamp: nodes[0].lastInvalidationTimestamp })
115115
}
116116
}
117117
return {
118118
name: project.name,
119-
environments: Array.from(environments),
119+
environments: Array.from(environments).map(([name, { timestamp }]) => ({
120+
name,
121+
transformTimestamp: timestamp,
122+
})),
120123
}
121124
})
122125
}

0 commit comments

Comments
 (0)