From 5bf11853203d6eeabbb8f80550136244a93459a1 Mon Sep 17 00:00:00 2001 From: Matthias Osswald Date: Fri, 22 Mar 2024 15:24:55 +0100 Subject: [PATCH] perf: Load SAPUI5 types only when needed --- src/detectors/typeChecker/host.ts | 99 +++++++++++++++++++++++++++++- src/detectors/typeChecker/index.ts | 4 ++ 2 files changed, 100 insertions(+), 3 deletions(-) diff --git a/src/detectors/typeChecker/host.ts b/src/detectors/typeChecker/host.ts index 8a9dd4d1e..77f461a40 100644 --- a/src/detectors/typeChecker/host.ts +++ b/src/detectors/typeChecker/host.ts @@ -5,6 +5,71 @@ import fs from "node:fs/promises"; import {createRequire} from "node:module"; const require = createRequire(import.meta.url); +const SAPUI5_TYPES_FILES = [ + "sap.apf", + "sap.chart", + "sap.ui.codeeditor", + "sap.collaboration", + "sap.zen.crosstab", + "sap.zen.dsh", + "sap.zen.commons", + "sap.sac.df", + "sap.ui.commons", + "sap.ui.comp", + "sap.ui.core", + "sap.ui.dt", + "sap.ui.export", + "sap.f", + "sap.ui.fl", + "sap.gantt", + "sap.ui.generic.app", + "sap.ui.generic.template", + "sap.uiext.inbox", + "sap.insights", + "sap.ui.integration", + "sap.ui.layout", + "sap.makit", + "sap.ui.mdc", + "sap.m", + "sap.me", + "sap.ndc", + "sap.ovp", + "sap.ui.richtexteditor", + "sap.ui.rta", + "sap.esh.search.ui", + "sap.fe.core", + "sap.fe.macros", + "sap.fe.navigation", + "sap.fe.placeholder", + "sap.fe.templates", + "sap.fe.test", + "sap.fe.tools", + "sap.feedback.ui", + "sap.rules.ui", + "sap.suite.ui.generic.template", + "sap.ui.vk", + "sap.ui.vtm", + "sap.webanalytics.core", + "sap.ui.suite", + "sap.suite.ui.commons", + "sap.suite.ui.microchart", + "sap.ui.support", + "sap.ui.table", + "sap.ui.testrecorder", + "sap.tnt", + "sap.ca.ui", + "sap.ui.unified", + "sap.ushell", + "sap.ushell_abap", + "sap.ui.ux3", + "sap.uxap", + "sap.ui.vbm", + "sap.viz", + "sap.ui.webc.common", + "sap.ui.webc.fiori", + "sap.ui.webc.main", +]; + interface PackageJson { dependencies: Record; } @@ -50,8 +115,11 @@ export async function createVirtualCompilerHost( )); options.typeRoots = ["/types"]; - // Request compiler to use all types we found in the dependencies of "@sapui5/types" - options.types = typePackageDirs; + options.types = [ + // Request compiler to only use sap.ui.core types by default - other types will be loaded on demand + ...typePackageDirs.filter((dir) => dir !== "/types/@sapui5/types/"), + "/types/@sapui5/types/types/sap.ui.core.d.ts", + ]; // Create regex matching all path mapping keys const pathMappingRegex = new RegExp( @@ -125,6 +193,9 @@ export async function createVirtualCompilerHost( if (fsPath) { return ts.sys.directoryExists(fsPath); } + if (directory.startsWith("/types/@ui5/linter/dynamic-types/")) { + return true; + } } return false; }, @@ -139,6 +210,9 @@ export async function createVirtualCompilerHost( if (fsPath) { return ts.sys.fileExists(fsPath); } + if (fileName.startsWith("/types/@ui5/linter/dynamic-types/") && fileName.endsWith(".d.ts")) { + return true; + } } return false; }, @@ -170,7 +244,26 @@ export async function createVirtualCompilerHost( if (sourceFileCache.has(fileName)) { return sourceFileCache.get(fileName); } - const sourceText = getFile(fileName); + + let sourceText: string | undefined = undefined; + + if (fileName.startsWith("/types/@ui5/linter/dynamic-types/") && fileName.endsWith(".d.ts")) { + const moduleName = fileName.match(/\/types\/@ui5\/linter\/dynamic-types\/(.*)\.d\.ts/)?.[1]; + if (moduleName) { + const libraryNameCheck = moduleName?.replace(/\//g, "."); + const libraryName = SAPUI5_TYPES_FILES.find(($) => libraryNameCheck.startsWith($)); + if (libraryName) { + sourceText = `/// `; + } else { + // Can happen e.g. for sap/ui/base/Event.d.ts, but sap.ui.core.d.ts is loaded by default + return; + } + } + } + + if (!sourceText) { + sourceText = getFile(fileName); + } if (sourceText === undefined) { throw new Error(`File not found: ${fileName}`); } diff --git a/src/detectors/typeChecker/index.ts b/src/detectors/typeChecker/index.ts index 1f8279776..4f6de0e53 100644 --- a/src/detectors/typeChecker/index.ts +++ b/src/detectors/typeChecker/index.ts @@ -59,6 +59,7 @@ export class TsProjectDetector extends ProjectBasedDetector { this.#projectBasePath = `/resources/${namespace}/`; this.compilerOptions.paths = { [`${namespace}/*`]: [`${this.#projectBasePath}*`], + "sap/*": ["/types/@ui5/linter/dynamic-types/sap/*"], }; } @@ -263,6 +264,9 @@ export class TsFileDetector extends FileBasedDetector { const options: ts.CompilerOptions = { ...DEFAULT_OPTIONS, rootDir: this.rootDir, + paths: { + "sap/*": ["/types/@ui5/linter/dynamic-types/sap/*"], + }, }; const resources = new Map();