diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index dc446319..6a80cfcb 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -73,6 +73,6 @@ A useful resource as you dive deeper are the [unit tests](__tests__/). They're g - _NOTE_: These are fairly short and unfortunately leave a lot to be desired... especially when you consider that this plugin is actually one of the simpler integrations out there. 1. At this point, you may be ready to read the more complicated bits of [`index`](src/index.ts) in detail and see how it interacts with the other modules. - The integration tests [TBD] could be useful to review at this point as well. -1. Once you're pretty familiar with `index`, you can dive into some of the cache code in [`tscache`](src/tscache.ts), [`nocache`](src/nocache.ts), and [`rollingcache`](src/rollingcache.ts). +1. Once you're pretty familiar with `index`, you can dive into some of the cache code in [`tscache`](src/tscache.ts) and [`rollingcache`](src/rollingcache.ts). 1. And finally, you can see some of the Rollup logging nuances in [`context`](src/context.ts) and [`rollupcontext`](src/rollupcontext.ts), and then the TS logging nuances in [`print-diagnostics`](src/print-diagnostics.ts), and [`diagnostics-format-host`](src/diagnostics-format-host.ts) - While these are necessary to the implementation, they are fairly ancillary to understanding and working with the codebase. diff --git a/__tests__/nocache.spec.ts b/__tests__/nocache.spec.ts deleted file mode 100644 index ee8ca11f..00000000 --- a/__tests__/nocache.spec.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { test, expect } from "@jest/globals"; - -import { NoCache } from "../src/nocache"; - -test("NoCache", () => { - const noCache = new NoCache(); - - expect(noCache.exists("")).toBeFalsy(); - expect(noCache.path("x")).toEqual("x"); - expect(noCache.match([])).toBeFalsy(); - expect(noCache.read("x")).toEqual(undefined); - expect(noCache.write("", {})).toBeFalsy(); - expect(noCache.touch("")).toBeFalsy(); - expect(noCache.roll()).toBeFalsy(); -}); diff --git a/src/nocache.ts b/src/nocache.ts deleted file mode 100644 index 8c1e15eb..00000000 --- a/src/nocache.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { ICache } from "./icache"; - -export class NoCache implements ICache -{ - public exists(_name: string): boolean - { - return false; - } - - public path(name: string): string - { - return name; - } - - public match(_names: string[]): boolean - { - return false; - } - - public read(_name: string): DataType | null | undefined - { - return undefined; - } - - public write(_name: string, _data: DataType): void - { - return; - } - - public touch(_name: string) - { - return; - } - - public roll() - { - return; - } -} diff --git a/src/tscache.ts b/src/tscache.ts index a9724d49..bbe600ac 100644 --- a/src/tscache.ts +++ b/src/tscache.ts @@ -10,7 +10,6 @@ import { RollingCache } from "./rollingcache"; import { ICache } from "./icache"; import { tsModule } from "./tsproxy"; import { formatHost } from "./diagnostics-format-host"; -import { NoCache } from "./nocache"; export interface ICode { @@ -103,9 +102,9 @@ export class TsCache private cacheVersion = "9"; private cachePrefix = "rpt2_"; private dependencyTree: Graph; - private ambientTypes: ITypeSnapshot[]; + private ambientTypes!: ITypeSnapshot[]; private ambientTypesDirty = false; - private cacheDir: string | undefined; + private cacheDir!: string; private codeCache!: ICache; private typesCache!: ICache; private semanticDiagnosticsCache!: ICache; @@ -114,25 +113,28 @@ export class TsCache constructor(private noCache: boolean, hashIgnoreUnknown: boolean, private host: tsTypes.LanguageServiceHost, private cacheRoot: string, private options: tsTypes.CompilerOptions, private rollupConfig: any, rootFilenames: string[], private context: IContext) { - this.hashOptions.ignoreUnknown = hashIgnoreUnknown; - if (!noCache) + this.dependencyTree = new Graph({ directed: true }); + this.dependencyTree.setDefaultNodeLabel((_node: string) => ({ dirty: false })); + + if (noCache) { - this.cacheDir = `${this.cacheRoot}/${this.cachePrefix}${objHash( - { - version: this.cacheVersion, - rootFilenames, - options: this.options, - rollupConfig: this.rollupConfig, - tsVersion: tsModule.version, - }, - this.hashOptions, - )}`; - } else { this.clean(); + return; } - this.dependencyTree = new Graph({ directed: true }); - this.dependencyTree.setDefaultNodeLabel((_node: string) => ({ dirty: false })); + this.hashOptions.ignoreUnknown = hashIgnoreUnknown; + this.cacheDir = `${this.cacheRoot}/${this.cachePrefix}${objHash( + { + version: this.cacheVersion, + rootFilenames, + options: this.options, + rollupConfig: this.rollupConfig, + tsVersion: tsModule.version, + }, + this.hashOptions, + )}`; + + this.init(); const automaticTypes = tsModule.getAutomaticTypeDirectiveNames(options, tsModule.sys) .map((entry) => tsModule.resolveTypeReferenceDirective(entry, undefined, options, tsModule.sys)) @@ -143,8 +145,6 @@ export class TsCache .concat(automaticTypes) .map((id) => ({ id, snapshot: this.host.getScriptSnapshot(id) })); - this.init(); - this.checkAmbientTypes(); } @@ -200,6 +200,9 @@ export class TsCache public done() { + if (this.noCache) + return; + this.context.info(blue("rolling caches")); this.codeCache.roll(); this.semanticDiagnosticsCache.roll(); @@ -254,12 +257,6 @@ export class TsCache private checkAmbientTypes(): void { - if (this.noCache) - { - this.ambientTypesDirty = true; - return; - } - this.context.debug(blue("Ambient types:")); const typeHashes = this.ambientTypes.filter((snapshot) => snapshot.snapshot !== undefined) .map((snapshot) => @@ -312,24 +309,10 @@ export class TsCache private init() { - if (this.noCache) - { - this.codeCache = new NoCache(); - this.typesCache = new NoCache(); - this.syntacticDiagnosticsCache = new NoCache(); - this.semanticDiagnosticsCache = new NoCache(); - } - else - { - // this is an invariant, it should never happen: cacheDir should only not exist when noCache - if (this.cacheDir === undefined) - throw new Error(`this.cacheDir undefined`); - - this.codeCache = new RollingCache(`${this.cacheDir}/code`, true); - this.typesCache = new RollingCache(`${this.cacheDir}/types`, true); - this.syntacticDiagnosticsCache = new RollingCache(`${this.cacheDir}/syntacticDiagnostics`, true); - this.semanticDiagnosticsCache = new RollingCache(`${this.cacheDir}/semanticDiagnostics`, true); - } + this.codeCache = new RollingCache(`${this.cacheDir}/code`, true); + this.typesCache = new RollingCache(`${this.cacheDir}/types`, true); + this.syntacticDiagnosticsCache = new RollingCache(`${this.cacheDir}/syntacticDiagnostics`, true); + this.semanticDiagnosticsCache = new RollingCache(`${this.cacheDir}/semanticDiagnostics`, true); } private markAsDirty(id: string): void