Closed patroza closed 1 year ago
I've fixed the performance issue with 3 caches, but the last one seems to have made the most difference. (initialize caches outside the Parser definition)
1:
const tsPlusTypeCache = {}
for (const resolvedPath of resolvedPaths) {
const json = resolvedPath in tsPlusTypeCache ? tsPlusTypeCache[resolvedPath] : (() => { const text = sys.readFile(resolvedPath); if (text) { const json = JSON.parse(text); tsPlusTypeCache[resolvedPath] = json; return json}})()
if (json) {
2:
const resolvedPathsCache = {}
if (options.configFilePath) {
const resolvedPaths = getResolvedPaths(options)
if (resolvedPaths.length === 0) {
return;
}
function getResolvedPaths(options) {
if (options.configFilePath in resolvedPathsCache) { return resolvedPathsCache[options.configFilePath] }
let resolvedPaths = [];
if (options.tsPlusTypes) {
for (const path of options.tsPlusTypes) {
if (pathIsRelative(path)) {
resolvedPaths.push(resolvePath(options.configFilePath.split("/").slice(0, -1).join("/"), path));
} else {
const resolvedModule = resolveModuleName(path, options.configFilePath, options, sys).resolvedModule;
if (resolvedModule) {
resolvedPaths.push(resolvedModule.resolvedFileName);
break;
}
}
}
}
const packagePath = removeExtension(options.configFilePath.split("node_modules").slice(-1)[0].substring(1), ".d.ts");
if (packagePath) {
let packageName;
if (packagePath.startsWith("@")) {
packageName = packagePath.split(directorySeparator).slice(0, 2).join(directorySeparator);
} else {
packageName = packagePath.split(directorySeparator).slice(0, 1)[0];
}
const resolvedPackageJson = resolvePackageNameToPackageJson(packageName, options.configFilePath, options, sys, void 0);
if (resolvedPackageJson) {
const packageJsonText = sys.readFile(resolvePath(resolvedPackageJson.packageDirectory, "package.json"));
if (packageJsonText) {
const packageJson = JSON.parse(packageJsonText);
if (packageJson.tsPlusTypes) {
for (const path of toArray(packageJson.tsPlusTypes)) {
resolvedPaths.push(resolvePath(resolvedPackageJson.packageDirectory, path));
}
}
}
}
if (packagePath.startsWith("@")) {
packageName = mangleScopedPackageName(packagePath.split(directorySeparator).slice(0, 2).join(directorySeparator));
} else {
packageName = packagePath.split(directorySeparator).slice(0, 1)[0];
}
const { resolvedModule } = resolveModuleName(`@tsplus-types/${packageName}`, options.configFilePath, { ...options, resolveJsonModule: true }, sys);
if (resolvedModule) {
resolvedPaths.push(resolvedModule.resolvedFileName);
}
}
resolvedPathsCache[options.configFilePath] = resolvedPaths
return resolvedPaths
}
3:
const resolvedModuleCache = {}
const resolvedModule = key in resolvedModuleCache ? resolvedModuleCache[key] : (resolvedModuleCache[key] = resolveModuleName(moduleName, options.configFilePath, options, sys).resolvedModule);
@0x706b this seems to work well now, thanks!
Perhaps also related: https://github.com/ts-plus/typescript/issues/248
Using these https://github.com/tim-smart/effect-rpc/tree/main/packages/router/vendor by @tim-smart coming out of https://github.com/tim-smart/tsplus-gen