Skip to content

Commit

Permalink
refactor: use hash instead mtime for cache key (#718)
Browse files Browse the repository at this point in the history
* refactor: use hash instead mtime for cache key

* style: correct variable names for bundle
  • Loading branch information
PeachScript committed Sep 19, 2023
1 parent af0dbd5 commit d318d4f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/builder/bundle/index.ts
Expand Up @@ -24,16 +24,16 @@ export interface IBundleWatcher {
close: () => void;
}

interface IBundlessOpts {
interface IBundleOpts {
cwd: string;
configProvider: BundleConfigProvider;
buildDependencies?: string[];
watch?: boolean;
}

function bundless(opts: Omit<IBundlessOpts, 'watch'>): Promise<void>;
function bundless(opts: IBundlessOpts): Promise<IBundleWatcher>;
async function bundless(opts: IBundlessOpts): Promise<void | IBundleWatcher> {
function bundle(opts: Omit<IBundleOpts, 'watch'>): Promise<void>;
function bundle(opts: IBundleOpts): Promise<IBundleWatcher>;
async function bundle(opts: IBundleOpts): Promise<void | IBundleWatcher> {
const enableCache = process.env.FATHER_CACHE !== 'none';
const closeHandlers: webpack.Watching['close'][] = [];

Expand Down Expand Up @@ -192,4 +192,4 @@ async function bundless(opts: IBundlessOpts): Promise<void | IBundleWatcher> {
}
}

export default bundless;
export default bundle;
5 changes: 3 additions & 2 deletions src/builder/bundless/loaders/index.ts
Expand Up @@ -4,6 +4,7 @@ import { runLoaders } from 'loader-runner';
import type { IApi } from '../../../types';
import { getCache } from '../../../utils';
import type { IBundlessConfig } from '../../config';
import { getContentHash } from '../../utils';
import { getTsconfig } from '../dts';
import type { IBundlessLoader, ILoaderOutput } from './types';

Expand Down Expand Up @@ -51,10 +52,10 @@ export default async (
},
) => {
const cache = getCache('bundless-loader');
// format: {path:mtime:config:pkgDeps}
// format: {path:contenthash:config:pkgDeps}
const cacheKey = [
fileAbsPath,
fs.statSync(fileAbsPath).mtimeMs,
getContentHash(fs.readFileSync(fileAbsPath, 'utf-8')),
JSON.stringify(opts.config),
// use for babel opts generator in src/builder/utils.ts
JSON.stringify(
Expand Down
8 changes: 6 additions & 2 deletions src/doctor/parser.ts
Expand Up @@ -4,6 +4,7 @@ import {
} from '@umijs/bundler-utils/compiled/esbuild';
import fs from 'fs';
import path from 'path';
import { getContentHash } from '../builder/utils';
import { getCache } from '../utils';

export type IDoctorSourceParseResult = {
Expand All @@ -14,8 +15,11 @@ export default async (
fileAbsPath: string,
): Promise<IDoctorSourceParseResult> => {
const cache = getCache('doctor-parser');
// format: {path:mtime}
const cacheKey = [fileAbsPath, fs.statSync(fileAbsPath).mtimeMs].join(':');
// format: {path:contenthash}
const cacheKey = [
fileAbsPath,
getContentHash(fs.readFileSync(fileAbsPath, 'utf-8')),
].join(':');
const cacheRet = cache.getSync(cacheKey, '');
const ret: IDoctorSourceParseResult = { imports: [] };

Expand Down

0 comments on commit d318d4f

Please sign in to comment.