Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cache): simplify creating / using the cache var #415

Merged
merged 1 commit into from
Aug 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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