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
Changes from 3 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
26 changes: 19 additions & 7 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;
let cachedPath = filePathMapperCache.get(x);
if (!cachedPath) {
cachedPath = path.resolve(x) as FilePathKey;
filePathMapperCache.set(x, cachedPath);
johnnyreilly marked this conversation as resolved.
Show resolved Hide resolved
}
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);
johnnyreilly marked this conversation as resolved.
Show resolved Hide resolved
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;
}
}

Expand Down