Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/vitest-dev/vscode. VS Code extension for Vitest vitest.dev/vscode
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576import * as vscode from 'vscode'
export class TransformSchemaProvider implements vscode.TextDocumentContentProvider, vscode.Disposable{ private disposables: vscode.Disposable[] = []
private _onDidChangeEvents = new vscode.EventEmitter<vscode.Uri>()
constructor( private getTransformedModule: ( apiId: string, project: string, environment: string, file: string, ) => Promise<string | null>, ) { this.disposables.push( vscode.workspace.registerTextDocumentContentProvider('vitest-transform', this), ) this.disposables.push(this._onDidChangeEvents) }
// This is called by vscode to clear the internal cache public onDidChange = this._onDidChangeEvents.event
public emitChange(uri: vscode.Uri) { const cachedFsPaths = this._cachedFsPaths.get(uri.fsPath) if (cachedFsPaths) { cachedFsPaths.forEach((uri) => { this._onDidChangeEvents.fire(uri) }) } }
private _cachedFsPaths = new Map<string, Set<vscode.Uri>>()
async provideTextDocumentContent(uri: vscode.Uri): Promise<string> { const searchParams = new URLSearchParams(uri.query) const apiId = searchParams.get('apiId') const project = searchParams.get('project') const environment = searchParams.get('environment') if (apiId == null || project == null || environment == null) { throw new Error(`Cannot parse the schema: ${uri.toString()}`) } const fsPath = uri.fsPath.slice(0, -3) // remove .js
let _cachedUris = this._cachedFsPaths.get(fsPath) if (!_cachedUris) { _cachedUris = new Set() } else { // remove older files from the same environment _cachedUris.forEach((uri) => { const query = uri.query.replace(/&t=\d+/, '') const currentQuery = uri.query.replace(/&t=\d+/, '') if (query === currentQuery) { _cachedUris?.delete(uri) } }) } _cachedUris.add(uri) this._cachedFsPaths.set(fsPath, _cachedUris)
const content = await this.getTransformedModule(apiId, project, environment, fsPath) if (content == null) { throw new Error(`The file ${fsPath} was not processed by Vite yet.`) } return content }
dispose() { this._cachedFsPaths.clear() this.disposables.forEach((d) => d.dispose()) }}