From 9ab5fbddb4ca14ce71812adc770dc69c3c7563a3 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 18 Dec 2024 16:05:47 -0800 Subject: [PATCH] Restore regex fast path --- src/compiler/path.ts | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/compiler/path.ts b/src/compiler/path.ts index 3f86b8260bf53..f46b70e9283af 100644 --- a/src/compiler/path.ts +++ b/src/compiler/path.ts @@ -630,6 +630,10 @@ export function getNormalizedAbsolutePath(path: string, currentDirectory: string path = combinePaths(currentDirectory, path); rootLength = getRootLength(path); } + const simple = simpleNormalizePath(path); + if (simple !== undefined) { + return simple; + } const root = path.substring(0, rootLength); const normalizedRoot = root && normalizeSlashes(root); // `normalized` is only initialized once `path` is determined to be non-normalized @@ -720,10 +724,31 @@ export function getNormalizedAbsolutePath(path: string, currentDirectory: string /** @internal */ export function normalizePath(path: string): string { - const normalized = getNormalizedAbsolutePath(path, ""); + let normalized = simpleNormalizePath(path); + if (normalized !== undefined) { + return normalized; + } + normalized = getNormalizedAbsolutePath(path, ""); return normalized && hasTrailingDirectorySeparator(path) ? ensureTrailingDirectorySeparator(normalized) : normalized; } +function simpleNormalizePath(path: string): string | undefined { + path = normalizeSlashes(path); + // Most paths don't require normalization + if (!relativePathSegmentRegExp.test(path)) { + return path; + } + // Some paths only require cleanup of `/./` or leading `./` + const simplified = path.replace(/\/\.\//g, "/").replace(/^\.\//, ""); + if (simplified !== path) { + path = simplified; + if (!relativePathSegmentRegExp.test(path)) { + return path; + } + } + return undefined; +} + function getPathWithoutRoot(pathComponents: readonly string[]) { if (pathComponents.length === 0) return ""; return pathComponents.slice(1).join(directorySeparator);