Skip to content

Commit e160564

Browse files
Add a cache to file path mapping (#1228)
* Add a cache to file path mapping This greatly reduces the time needed to resolve FilePathKey objects on watch builds. * Address PR comments * Bump package.json and CHANGELOG.md Co-authored-by: John Reilly <johnny_reilly@hotmail.com>
1 parent 14fa3f8 commit e160564

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Changelog
2+
## v8.0.13
3+
* [Speed up builds by adding an in-memory cache to file path lookups](https://github.com/TypeStrong/ts-loader/pull/1228) - thanks @berickson1
24

35
## v8.0.12
46
* [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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ts-loader",
3-
"version": "8.0.12",
3+
"version": "8.0.13",
44
"description": "TypeScript loader for webpack",
55
"main": "index.js",
66
"types": "dist",

src/instances.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,35 @@ function createFilePathKeyMapper(
8787
compiler: typeof typescript,
8888
loaderOptions: LoaderOptions
8989
) {
90+
// Cache file path key - a map lookup is much faster than filesystem/regex operations & the result will never change
91+
const filePathMapperCache = new Map<string, FilePathKey>();
9092
// FileName lowercasing copied from typescript
9193
const fileNameLowerCaseRegExp = /[^\u0130\u0131\u00DFa-z0-9\\/:\-_\. ]+/g;
9294
return useCaseSensitiveFileNames(compiler, loaderOptions)
9395
? pathResolve
9496
: toFileNameLowerCase;
9597

96-
function pathResolve(x: string) {
97-
return path.resolve(x) as FilePathKey;
98+
function pathResolve(filePath: string) {
99+
let cachedPath = filePathMapperCache.get(filePath);
100+
if (!cachedPath) {
101+
cachedPath = path.resolve(filePath) as FilePathKey;
102+
filePathMapperCache.set(filePath, cachedPath);
103+
}
104+
return cachedPath;
98105
}
99106

100-
function toFileNameLowerCase(x: string) {
101-
const filePathKey = pathResolve(x);
102-
return fileNameLowerCaseRegExp.test(filePathKey)
103-
? (filePathKey.replace(fileNameLowerCaseRegExp, ch =>
104-
ch.toLowerCase()
105-
) as FilePathKey)
106-
: filePathKey;
107+
function toFileNameLowerCase(filePath: string) {
108+
let cachedPath = filePathMapperCache.get(filePath);
109+
if (!cachedPath) {
110+
const filePathKey = pathResolve(filePath);
111+
cachedPath = fileNameLowerCaseRegExp.test(filePathKey)
112+
? (filePathKey.replace(fileNameLowerCaseRegExp, ch =>
113+
ch.toLowerCase()
114+
) as FilePathKey)
115+
: filePathKey;
116+
filePathMapperCache.set(filePath, cachedPath);
117+
}
118+
return cachedPath;
107119
}
108120
}
109121

0 commit comments

Comments
 (0)