From eb5408e156731417f27bea43dd0fa6ce576190c4 Mon Sep 17 00:00:00 2001 From: Andrew Casey Date: Wed, 19 May 2021 10:53:07 -0700 Subject: [PATCH 1/3] Kick out of normalizePath if there's nothing to do ...using `relativePathSegmentRegExp`. Bonus: use a regex to handle "/./" to avoid splitting and joining in a common case. When building the compiler, for example, it looks like ~95% of arguments to `normalizePath` do not require any normalization. --- src/compiler/path.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/compiler/path.ts b/src/compiler/path.ts index 27b5330b2cedc..acd8669aad622 100644 --- a/src/compiler/path.ts +++ b/src/compiler/path.ts @@ -552,6 +552,10 @@ namespace ts { export function normalizePath(path: string): string { path = normalizeSlashes(path); + path = path.replace(/\/\.\//g, "/"); + if (!relativePathSegmentRegExp.test(path)) { + return path; + } const normalized = getPathFromPathComponents(reducePathComponents(getPathComponents(path))); return normalized && hasTrailingDirectorySeparator(path) ? ensureTrailingDirectorySeparator(normalized) : normalized; } From 0c1b81cc6670ce541d2d4ce9d8995a260212886c Mon Sep 17 00:00:00 2001 From: Andrew Casey Date: Wed, 19 May 2021 15:31:34 -0700 Subject: [PATCH 2/3] Check normalization before and after . cleanup --- src/compiler/path.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/compiler/path.ts b/src/compiler/path.ts index acd8669aad622..a065b8f036856 100644 --- a/src/compiler/path.ts +++ b/src/compiler/path.ts @@ -552,10 +552,19 @@ namespace ts { export function normalizePath(path: string): string { path = normalizeSlashes(path); - path = path.replace(/\/\.\//g, "/"); + // Most paths don't require normalization if (!relativePathSegmentRegExp.test(path)) { return path; } + // Some paths only require cleanup of `/./` + const simplified = path.replace(/\/\.\//g, "/"); + if (simplified !== path) { + path = simplified; + if (!relativePathSegmentRegExp.test(path)) { + return path; + } + } + // Other paths require full normalization const normalized = getPathFromPathComponents(reducePathComponents(getPathComponents(path))); return normalized && hasTrailingDirectorySeparator(path) ? ensureTrailingDirectorySeparator(normalized) : normalized; } From a098b541230fab63a70795a141afd429f34e6865 Mon Sep 17 00:00:00 2001 From: Andrew Casey Date: Fri, 21 May 2021 10:41:15 -0700 Subject: [PATCH 3/3] Also cleanup leading ./ --- src/compiler/path.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compiler/path.ts b/src/compiler/path.ts index a065b8f036856..d09108d34c7f3 100644 --- a/src/compiler/path.ts +++ b/src/compiler/path.ts @@ -556,8 +556,8 @@ namespace ts { if (!relativePathSegmentRegExp.test(path)) { return path; } - // Some paths only require cleanup of `/./` - const simplified = path.replace(/\/\.\//g, "/"); + // Some paths only require cleanup of `/./` or leading `./` + const simplified = path.replace(/\/\.\//g, "/").replace(/^\.\//, ""); if (simplified !== path) { path = simplified; if (!relativePathSegmentRegExp.test(path)) {