diff --git a/src/index.ts b/src/index.ts index 48d2c72ee..f9500ce5a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -81,7 +81,7 @@ class MemoryCache { fileContents = new Map() fileVersions = new Map() - constructor (rootFileNames: string[] = []) { + constructor (public rootFileNames: string[] = []) { for (const fileName of rootFileNames) this.fileVersions.set(fileName, 1) } } @@ -293,7 +293,7 @@ export function register (opts: Options = {}): Register { // Create the compiler host for type checking. const serviceHost: _ts.LanguageServiceHost = { - getScriptFileNames: () => Array.from(memoryCache.fileVersions.keys()), + getScriptFileNames: () => memoryCache.rootFileNames, getScriptVersion: (fileName: string) => { const version = memoryCache.fileVersions.get(fileName) return version === undefined ? '' : version.toString() @@ -332,6 +332,9 @@ export function register (opts: Options = {}): Register { const updateMemoryCache = (contents: string, fileName: string) => { const fileVersion = memoryCache.fileVersions.get(fileName) || 0 + // Add to `rootFiles` when discovered for the first time. + if (fileVersion === 0) memoryCache.rootFileNames.push(fileName) + // Avoid incrementing cache when nothing has changed. if (memoryCache.fileContents.get(fileName) === contents) return