Skip to content

Commit c6be0eb

Browse files
authored
refactor(cache): simplify creating / using the cache var (#415)
- 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!
1 parent 1e71d50 commit c6be0eb

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

src/index.ts

+9-14
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,17 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
3131
let servicesHost: LanguageServiceHost;
3232
let service: tsTypes.LanguageService;
3333
let documentRegistry: tsTypes.DocumentRegistry; // keep the same DocumentRegistry between watch cycles
34+
let cache: TsCache;
3435
let noErrors = true;
3536
const declarations: { [name: string]: { type: tsTypes.OutputFile; map?: tsTypes.OutputFile } } = {};
3637
const checkedFiles = new Set<string>();
3738

38-
let _cache: TsCache;
39-
const cache = (): TsCache =>
40-
{
41-
if (!_cache)
42-
_cache = new TsCache(pluginOptions.clean, pluginOptions.objectHashIgnoreUnknownHack, servicesHost, pluginOptions.cacheRoot, parsedConfig.options, rollupOptions, parsedConfig.fileNames, context);
43-
return _cache;
44-
};
45-
4639
const getDiagnostics = (id: string, snapshot: tsTypes.IScriptSnapshot) =>
4740
{
48-
return cache().getSyntacticDiagnostics(id, snapshot, () =>
41+
return cache.getSyntacticDiagnostics(id, snapshot, () =>
4942
{
5043
return service.getSyntacticDiagnostics(id);
51-
}).concat(cache().getSemanticDiagnostics(id, snapshot, () =>
44+
}).concat(cache.getSemanticDiagnostics(id, snapshot, () =>
5245
{
5346
return service.getSemanticDiagnostics(id);
5447
}));
@@ -85,7 +78,7 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
8578
if (!watchMode && !noErrors)
8679
context.info(yellow("there were errors or warnings."));
8780

88-
cache().done();
81+
cache.done();
8982
}
9083

9184
const pluginOptions: IOptions = Object.assign({},
@@ -155,6 +148,8 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
155148
service = tsModule.createLanguageService(servicesHost, documentRegistry);
156149
servicesHost.setLanguageService(service);
157150

151+
cache = new TsCache(pluginOptions.clean, pluginOptions.objectHashIgnoreUnknownHack, servicesHost, pluginOptions.cacheRoot, parsedConfig.options, rollupOptions, parsedConfig.fileNames, context);
152+
158153
// printing compiler option errors
159154
if (pluginOptions.check) {
160155
const diagnostics = convertDiagnostic("options", service.getCompilerOptionsDiagnostics());
@@ -189,7 +184,7 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
189184
return;
190185

191186
if (filter(resolved))
192-
cache().setDependency(resolved, importer);
187+
cache.setDependency(resolved, importer);
193188

194189
if (resolved.endsWith(".d.ts"))
195190
return;
@@ -216,7 +211,7 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
216211
const snapshot = servicesHost.setSnapshot(id, code);
217212

218213
// getting compiled file from cache or from ts
219-
const result = cache().getCompiled(id, snapshot, () =>
214+
const result = cache.getCompiled(id, snapshot, () =>
220215
{
221216
const output = service.getEmitOutput(id);
222217

@@ -290,7 +285,7 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
290285
// walkTree once on each cycle when in watch mode
291286
if (watchMode)
292287
{
293-
cache().walkTree((id) =>
288+
cache.walkTree((id) =>
294289
{
295290
if (!filter(id))
296291
return;

0 commit comments

Comments
 (0)