From 884e4ce7a055443524cf0095590cb86b2702ef70 Mon Sep 17 00:00:00 2001 From: Brent Erickson Date: Tue, 29 Dec 2020 15:23:54 -0800 Subject: [PATCH 1/3] Add a cache to file path mapping This greatly reduces the time needed to resolve FilePathKey objects on watch builds. --- src/instances.ts | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/instances.ts b/src/instances.ts index d8c29dc60..1e0ad2cea 100644 --- a/src/instances.ts +++ b/src/instances.ts @@ -87,6 +87,8 @@ function createFilePathKeyMapper( compiler: typeof typescript, loaderOptions: LoaderOptions ) { + // Cache file path key - a map lookup is much faster than filesystem/regex operations & the result will never change + const filePathMapperCache = new Map(); // FileName lowercasing copied from typescript const fileNameLowerCaseRegExp = /[^\u0130\u0131\u00DFa-z0-9\\/:\-_\. ]+/g; return useCaseSensitiveFileNames(compiler, loaderOptions) @@ -94,16 +96,26 @@ function createFilePathKeyMapper( : toFileNameLowerCase; function pathResolve(x: string) { - return path.resolve(x) as FilePathKey; + let cachedPath = filePathMapperCache.get(x); + if (!cachedPath) { + cachedPath = path.resolve(x) as FilePathKey; + filePathMapperCache.set(x, cachedPath); + } + return cachedPath; } function toFileNameLowerCase(x: string) { - const filePathKey = pathResolve(x); - return fileNameLowerCaseRegExp.test(filePathKey) - ? (filePathKey.replace(fileNameLowerCaseRegExp, ch => - ch.toLowerCase() - ) as FilePathKey) - : filePathKey; + let cachedPath = filePathMapperCache.get(x); + if (!cachedPath) { + const filePathKey = pathResolve(x); + cachedPath = fileNameLowerCaseRegExp.test(filePathKey) + ? (filePathKey.replace(fileNameLowerCaseRegExp, ch => + ch.toLowerCase() + ) as FilePathKey) + : filePathKey; + filePathMapperCache.set(x, cachedPath); + } + return cachedPath; } } From a6aa53f3813cf6b00abd6392a886145eebbfedbd Mon Sep 17 00:00:00 2001 From: Brent Erickson Date: Tue, 29 Dec 2020 22:10:15 -0800 Subject: [PATCH 2/3] Address PR comments --- src/instances.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/instances.ts b/src/instances.ts index 1e0ad2cea..ce2800382 100644 --- a/src/instances.ts +++ b/src/instances.ts @@ -95,25 +95,25 @@ function createFilePathKeyMapper( ? pathResolve : toFileNameLowerCase; - function pathResolve(x: string) { - let cachedPath = filePathMapperCache.get(x); + function pathResolve(filePath: string) { + let cachedPath = filePathMapperCache.get(filePath); if (!cachedPath) { - cachedPath = path.resolve(x) as FilePathKey; - filePathMapperCache.set(x, cachedPath); + cachedPath = path.resolve(filePath) as FilePathKey; + filePathMapperCache.set(filePath, cachedPath); } return cachedPath; } - function toFileNameLowerCase(x: string) { - let cachedPath = filePathMapperCache.get(x); + function toFileNameLowerCase(filePath: string) { + let cachedPath = filePathMapperCache.get(filePath); if (!cachedPath) { - const filePathKey = pathResolve(x); + const filePathKey = pathResolve(filePath); cachedPath = fileNameLowerCaseRegExp.test(filePathKey) ? (filePathKey.replace(fileNameLowerCaseRegExp, ch => ch.toLowerCase() ) as FilePathKey) : filePathKey; - filePathMapperCache.set(x, cachedPath); + filePathMapperCache.set(filePath, cachedPath); } return cachedPath; } From b89898247f2d550c4e24d650c2ee8a8373964ba1 Mon Sep 17 00:00:00 2001 From: Brent Erickson Date: Tue, 29 Dec 2020 22:10:35 -0800 Subject: [PATCH 3/3] Bump package.json and CHANGELOG.md --- CHANGELOG.md | 2 ++ package.json | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 69a8a2993..092417ab3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,6 @@ # Changelog +## v8.0.13 +* [Speed up builds by adding an in-memory cache to file path lookups](https://github.com/TypeStrong/ts-loader/pull/1228) - thanks @berickson1 ## v8.0.12 * [Instead of checking date, check time thats more accurate to see if something has changed](https://github.com/TypeStrong/ts-loader/pull/1217) - thanks @sheetalkamat diff --git a/package.json b/package.json index 48ff15977..88d44fbd5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ts-loader", - "version": "8.0.12", + "version": "8.0.13", "description": "TypeScript loader for webpack", "main": "index.js", "types": "dist",