dominikg/tsconfck

Improve `parse` performance

sapphi-red opened this issue ยท 3 comments

Describe the feature
parse function is the performance bottleneck of vite's transformWithEsbuild function which is heavily used.

Suggested solution (copied from the original issue)
I have two ideas to solve this.

First one is to keep negative cache for find function like below. This will reduce calling fs.stat which is called inside tsconfigDir function.

const negativeCache = new Set()
export async function find(filename: string) {
	let dir = path.dirname(path.resolve(filename));
	while (dir) {
                if (!negativeCache.has(dir)) {
		    const tsconfig = await tsconfigInDir(dir);
		    if (tsconfig) {
			    return tsconfig;
		    }
		    negativeCache.add(dir);
                } else {
			const parent = path.dirname(dir);
			if (parent === dir) {
				break;
			} else {
				dir = parent;
			}
		}
	}
	throw new Error(`no tsconfig file found for ${filename}`);
}

Second one is to collect all tsconfig.json inside project folder before transpiling.

const tsconfigPaths = new Set(getTsconfigPaths())
export async function find(filename: string) {
	let dir = path.dirname(path.resolve(filename));
	while (dir) {
                if (tsconfigPaths.has(dir)) {
		    const tsconfig = await tsconfigInDir(dir);
		    if (tsconfig) {
			    return tsconfig;
		    }
                } else {
			const parent = path.dirname(dir);
			if (parent === dir) {
				break;
			} else {
				dir = parent;
			}
		}
	}
	throw new Error(`no tsconfig file found for ${filename}`);
}

Additional context
For more context with vite, see vitejs/vite#7023.

Thanks for bringing this up. One possible issue i see with this is that it would deviate from how typescript itself handles finding config files.
This can be an issue in current vite projects too, where it finds a tsconfig.json outside of the workspace root because there is no tsconfig.json inside.

"root" and "tsconfigPaths" to the tsconfck options could help. when root is set, tsconfigPaths could also be automatically generated by a recursive find in root. With documentation added that this deviates from typescript behavior and throws an error if no tsconfig was found in root.

I think "tsconfigPaths" is better than "root" option, because tsconfig.json file may be created after collecting tsconfigPaths.
Or to add a way to clear tsconfigPaths.

released in 1.2.0