Skip to content

Commit

Permalink
Add a cache to file path mapping (#1228)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
berickson1 and johnnyreilly authored Dec 31, 2020
1 parent 14fa3f8 commit e160564
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
30 changes: 21 additions & 9 deletions src/instances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, FilePathKey>();
// 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;
}
}

Expand Down

0 comments on commit e160564

Please sign in to comment.