Skip to content

Commit

Permalink
refactor: use content hash as cache key for md loader (#1918)
Browse files Browse the repository at this point in the history
  • Loading branch information
PeachScript authored Sep 27, 2023
1 parent 8d52aff commit 4939ce2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
16 changes: 9 additions & 7 deletions src/loaders/markdown/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { isTabRouteFile } from '@/features/tabs';
import type { IThemeLoadResult } from '@/features/theme/loader';
import { getCache } from '@/utils';
import { getCache, getContentHash } from '@/utils';
import fs from 'fs';
import { lodash, Mustache, winPath } from 'umi/plugin-utils';
import { Mustache, lodash, winPath } from 'umi/plugin-utils';
import transform, {
type IMdTransformerOptions,
type IMdTransformerResult,
Expand Down Expand Up @@ -135,9 +135,11 @@ export default DumiMarkdownContent;`;
}
}

function getDepsCacheKey(deps: typeof depsMapping['0'] = []) {
function getDepsCacheKey(deps: (typeof depsMapping)['0'] = []) {
return JSON.stringify(
deps.map((file) => `${file}:${fs.statSync(file).mtimeMs}`),
deps.map(
(file) => `${file}:${getContentHash(fs.readFileSync(file, 'utf-8'))}`,
),
);
}

Expand All @@ -149,13 +151,13 @@ export default function mdLoader(this: any, content: string) {
const cb = this.async();

const cache = getCache('md-loader');
// format: {path:mtime:loaderOpts}
// format: {path:contenthash:loaderOpts}
const baseCacheKey = [
this.resourcePath,
fs.statSync(this.resourcePath).mtimeMs,
getContentHash(content),
JSON.stringify(lodash.omit(opts, ['mode', 'builtins', 'onResolveDemos'])),
].join(':');
// format: {baseCacheKey:{deps:mtime}[]}
// format: {baseCacheKey:{deps:contenthash}[]}
const cacheKey = [
baseCacheKey,
getDepsCacheKey(depsMapping[this.resourcePath]),
Expand Down
10 changes: 9 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createHash } from 'crypto';
import Cache from 'file-system-cache';
import fs from 'fs';
import yaml from 'js-yaml';
Expand Down Expand Up @@ -82,7 +83,7 @@ export function parseCodeFrontmatter(raw: string) {
*/
const caches: Record<string, ReturnType<typeof Cache>> = {};
const CACHE_PATH = 'node_modules/.cache/dumi';
export function getCache(ns: string): typeof caches['0'] {
export function getCache(ns: string): (typeof caches)['0'] {
// return fake cache if cache disabled
if (process.env.DUMI_CACHE === 'none') {
return { set() {}, get() {}, setSync() {}, getSync() {} } as any;
Expand Down Expand Up @@ -156,3 +157,10 @@ export function getProjectRoot(cwd: string) {

return winPath(cwd);
}

/**
* generate hash for string
*/
export function getContentHash(content: string, length = 8) {
return createHash('md5').update(content).digest('hex').slice(0, length);
}

0 comments on commit 4939ce2

Please sign in to comment.