diff --git a/src/loaders/markdown/index.ts b/src/loaders/markdown/index.ts index 108e2dd98..da9cd4a04 100644 --- a/src/loaders/markdown/index.ts +++ b/src/loaders/markdown/index.ts @@ -424,11 +424,26 @@ function emit(this: any, opts: IMdLoaderOptions, ret: IMdTransformerResult) { } } +const fileHashCache = new Map(); + +function getCachedFileHash(file: string) { + const stat = fs.statSync(file, { bigint: true }); + const signature = `${stat.mtimeNs}:${stat.size}`; + const cached = fileHashCache.get(file); + + if (cached?.signature === signature) { + return cached.hash; + } + + const hash = getContentHash(fs.readFileSync(file, 'utf-8')); + fileHashCache.set(file, { signature, hash }); + + return hash; +} + function getDepsCacheKey(deps: (typeof depsMapping)['0'] = []) { return JSON.stringify( - deps.map( - (file) => `${file}:${getContentHash(fs.readFileSync(file, 'utf-8'))}`, - ), + deps.map((file) => `${file}:${getCachedFileHash(file)}`), ); }