Skip to content

Commit

Permalink
refactor(cache): simplify creating / using the cache var
Browse files Browse the repository at this point in the history
- 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!
  • Loading branch information
agilgur5 committed Aug 25, 2022
1 parent d79a559 commit 2fcae7c
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,17 @@ const typescript: PluginImpl<RPT2Options> = (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<string>();

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);
}));
Expand Down Expand Up @@ -85,7 +78,7 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
if (!watchMode && !noErrors)
context.info(yellow("there were errors or warnings."));

cache().done();
cache.done();
}

const pluginOptions: IOptions = Object.assign({},
Expand Down Expand Up @@ -155,6 +148,8 @@ const typescript: PluginImpl<RPT2Options> = (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());
Expand Down Expand Up @@ -189,7 +184,7 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
return;

if (filter(resolved))
cache().setDependency(resolved, importer);
cache.setDependency(resolved, importer);

if (resolved.endsWith(".d.ts"))
return;
Expand All @@ -216,7 +211,7 @@ const typescript: PluginImpl<RPT2Options> = (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);

Expand Down Expand Up @@ -290,7 +285,7 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
// walkTree once on each cycle when in watch mode
if (watchMode)
{
cache().walkTree((id) =>
cache.walkTree((id) =>
{
if (!filter(id))
return;
Expand Down

0 comments on commit 2fcae7c

Please sign in to comment.