dominikg / tsconfck

A utility to find and parse tsconfig files without depending on typescript
Other
290 stars 14 forks source link

refactor(cache): introduce standalone cache for find/parse #112

Closed dominikg closed 1 year ago

dominikg commented 1 year ago

used in findNative/parseNative as well.

Main motivation behind this is that the previous way to use findAll + tsconfigPaths could create a lot of extra work in huge projects, where ts files were contained in a small subtree.

Without having to traverse all of root and lazily caching all directories that have been traversed, this costs a little bit more memory, but less up front work.

Example for tsconfig in /a/b/c/tsconfig.json

const cache = new TSConfckCache(); 
const eConfig = await find('/a/b/c/d/e/foo.ts',{cache}); //  traverses e,d,c  to find the config and adds all to the cache separately
const dConfig = await find('/a/b/c/d/foo.ts',{cache}); // no traversal needed, cache hit
const fConfig = await find('/a/b/c/d/e/f/foo.ts',{cache});  // only traverse from f to e, then finds cache hit

(findNative caching is a little bit less effective as cache hits during traversal are impossible)

In a very large project with deep code trees and many directories, this can consume extra memory because all intermediate directories are added to the cache separately.

dominikg commented 1 year ago

Improved code to eagerly add Promises to cache instead of only setting results. This ensures that fs calls are minimized even with parallel async calls to find and parse. This can occur in vite when many files are passing through transformWithESbuild eg during devserver start.