Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a cache to file path mapping #1228

Merged
merged 6 commits into from Dec 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions 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
Expand Down
2 changes: 1 addition & 1 deletion 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",
Expand Down
30 changes: 21 additions & 9 deletions src/instances.ts
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