diff --git a/packages/language-server/src/lib/documents/Document.ts b/packages/language-server/src/lib/documents/Document.ts index e7728fa72..0c64674b8 100644 --- a/packages/language-server/src/lib/documents/Document.ts +++ b/packages/language-server/src/lib/documents/Document.ts @@ -17,6 +17,11 @@ export class Document extends WritableDocument { configPromise: Promise; config?: SvelteConfig; html!: HTMLDocument; + /** + * Compute and cache directly because of performance reasons + * and it will be called anyway. + */ + private path = urlToPath(this.url); constructor(public url: string, public content: string) { super(); @@ -77,7 +82,7 @@ export class Document extends WritableDocument { * Returns the file path if the url scheme is file */ getFilePath(): string | null { - return urlToPath(this.url); + return this.path; } /** diff --git a/packages/language-server/src/plugins/typescript/module-loader.ts b/packages/language-server/src/plugins/typescript/module-loader.ts index b023318b1..1fcac077a 100644 --- a/packages/language-server/src/plugins/typescript/module-loader.ts +++ b/packages/language-server/src/plugins/typescript/module-loader.ts @@ -90,9 +90,14 @@ export function createSvelteModuleLoader( fileExists: svelteSys.fileExists, readFile: svelteSys.readFile, readDirectory: svelteSys.readDirectory, - deleteFromModuleCache: (path: string) => moduleCache.delete(path), - deleteUnresolvedResolutionsFromCache: (path: string) => - moduleCache.deleteUnresolvedResolutionsFromCache(path), + deleteFromModuleCache: (path: string) => { + svelteSys.deleteFromCache(path); + moduleCache.delete(path); + }, + deleteUnresolvedResolutionsFromCache: (path: string) => { + svelteSys.deleteFromCache(path); + moduleCache.deleteUnresolvedResolutionsFromCache(path); + }, resolveModuleNames }; diff --git a/packages/language-server/src/plugins/typescript/svelte-sys.ts b/packages/language-server/src/plugins/typescript/svelte-sys.ts index 6393c0c30..64a3c840d 100644 --- a/packages/language-server/src/plugins/typescript/svelte-sys.ts +++ b/packages/language-server/src/plugins/typescript/svelte-sys.ts @@ -6,10 +6,15 @@ import { ensureRealSvelteFilePath, isVirtualSvelteFilePath, toRealSvelteFilePath * This should only be accessed by TS svelte module resolution. */ export function createSvelteSys(getSnapshot: (fileName: string) => DocumentSnapshot) { - const svelteSys: ts.System = { + const fileExistsCache = new Map(); + + const svelteSys: ts.System & { deleteFromCache: (path: string) => void } = { ...ts.sys, fileExists(path: string) { - return ts.sys.fileExists(ensureRealSvelteFilePath(path)); + path = ensureRealSvelteFilePath(path); + const exists = fileExistsCache.get(path) ?? ts.sys.fileExists(path); + fileExistsCache.set(path, exists); + return exists; }, readFile(path: string) { const snapshot = getSnapshot(path); @@ -19,6 +24,13 @@ export function createSvelteSys(getSnapshot: (fileName: string) => DocumentSnaps const extensionsWithSvelte = (extensions ?? []).concat('.svelte'); return ts.sys.readDirectory(path, extensionsWithSvelte, exclude, include, depth); + }, + deleteFile(path) { + fileExistsCache.delete(ensureRealSvelteFilePath(path)); + return ts.sys.deleteFile?.(path); + }, + deleteFromCache(path) { + fileExistsCache.delete(ensureRealSvelteFilePath(path)); } };