From 0dabda3f452320634fdec031aff7c9802ad283e6 Mon Sep 17 00:00:00 2001 From: Anton Gilgur Date: Sat, 30 Jul 2022 16:18:09 -0400 Subject: [PATCH] refactor(cache): simplify creating / using the `cache` var - as everything is created in the `buildStart` hook now (which has `RollupContext`), we can create the `cache` there too - no need for slightly hacky, lazy creation during `transform` anymore - simplifies it and also standardizes it so it's created the same way as all the other instance vars - fix: reset `cache` after each watch cycle - previously the cache was never reset, meaning that if anything became dirty in a watch cycle, it would never get reset back - in some cases, this would mean that the cache was effectively always dirty during an entire watch mode run, and therefore never used - this would be quite inefficient as the FS usage for the cache would just go to waste - see that test coverage has now increased as a result! --- src/index.ts | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/src/index.ts b/src/index.ts index 0b1ca2b8..2ac5c106 100644 --- a/src/index.ts +++ b/src/index.ts @@ -31,24 +31,17 @@ const typescript: PluginImpl = (options) => let servicesHost: LanguageServiceHost; let service: tsTypes.LanguageService; let documentRegistry: tsTypes.DocumentRegistry; // keep the same DocumentRegistry between watch cycles + let cache: TsCache; let noErrors = true; const declarations: { [name: string]: { type: tsTypes.OutputFile; map?: tsTypes.OutputFile } } = {}; const checkedFiles = new Set(); - let _cache: TsCache; - const cache = (): TsCache => - { - if (!_cache) - _cache = new TsCache(pluginOptions.clean, pluginOptions.objectHashIgnoreUnknownHack, servicesHost, pluginOptions.cacheRoot, parsedConfig.options, rollupOptions, parsedConfig.fileNames, context); - return _cache; - }; - const getDiagnostics = (id: string, snapshot: tsTypes.IScriptSnapshot) => { - return cache().getSyntacticDiagnostics(id, snapshot, () => + return cache.getSyntacticDiagnostics(id, snapshot, () => { return service.getSyntacticDiagnostics(id); - }).concat(cache().getSemanticDiagnostics(id, snapshot, () => + }).concat(cache.getSemanticDiagnostics(id, snapshot, () => { return service.getSemanticDiagnostics(id); })); @@ -85,7 +78,7 @@ const typescript: PluginImpl = (options) => if (!watchMode && !noErrors) context.info(yellow("there were errors or warnings.")); - cache().done(); + cache.done(); } const pluginOptions: IOptions = Object.assign({}, @@ -155,6 +148,8 @@ const typescript: PluginImpl = (options) => service = tsModule.createLanguageService(servicesHost, documentRegistry); servicesHost.setLanguageService(service); + cache = new TsCache(pluginOptions.clean, pluginOptions.objectHashIgnoreUnknownHack, servicesHost, pluginOptions.cacheRoot, parsedConfig.options, rollupOptions, parsedConfig.fileNames, context); + // printing compiler option errors if (pluginOptions.check) { const diagnostics = convertDiagnostic("options", service.getCompilerOptionsDiagnostics()); @@ -189,7 +184,7 @@ const typescript: PluginImpl = (options) => return; if (filter(resolved)) - cache().setDependency(resolved, importer); + cache.setDependency(resolved, importer); if (resolved.endsWith(".d.ts")) return; @@ -216,7 +211,7 @@ const typescript: PluginImpl = (options) => const snapshot = servicesHost.setSnapshot(id, code); // getting compiled file from cache or from ts - const result = cache().getCompiled(id, snapshot, () => + const result = cache.getCompiled(id, snapshot, () => { const output = service.getEmitOutput(id); @@ -290,7 +285,7 @@ const typescript: PluginImpl = (options) => // walkTree once on each cycle when in watch mode if (watchMode) { - cache().walkTree((id) => + cache.walkTree((id) => { if (!filter(id)) return;