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", diff --git a/src/instances.ts b/src/instances.ts index d8c29dc60..ce2800382 100644 --- a/src/instances.ts +++ b/src/instances.ts @@ -87,23 +87,35 @@ 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) ? pathResolve : toFileNameLowerCase; - function pathResolve(x: string) { - return path.resolve(x) as FilePathKey; + function pathResolve(filePath: string) { + let cachedPath = filePathMapperCache.get(filePath); + if (!cachedPath) { + cachedPath = path.resolve(filePath) as FilePathKey; + filePathMapperCache.set(filePath, cachedPath); + } + return cachedPath; } - function toFileNameLowerCase(x: string) { - const filePathKey = pathResolve(x); - return fileNameLowerCaseRegExp.test(filePathKey) - ? (filePathKey.replace(fileNameLowerCaseRegExp, ch => - ch.toLowerCase() - ) as FilePathKey) - : filePathKey; + function toFileNameLowerCase(filePath: string) { + let cachedPath = filePathMapperCache.get(filePath); + if (!cachedPath) { + const filePathKey = pathResolve(filePath); + cachedPath = fileNameLowerCaseRegExp.test(filePathKey) + ? (filePathKey.replace(fileNameLowerCaseRegExp, ch => + ch.toLowerCase() + ) as FilePathKey) + : filePathKey; + filePathMapperCache.set(filePath, cachedPath); + } + return cachedPath; } }