Skip to content

Commit

Permalink
feat: allow set fs cache dir by env (#734)
Browse files Browse the repository at this point in the history
* feat: allow set fs cache dir by env

* style: format

* refactor: handle all usages for cache path
  • Loading branch information
PeachScript authored Dec 15, 2023
1 parent b1b2ec3 commit 4e35218
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
5 changes: 2 additions & 3 deletions src/builder/bundle/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { webpack } from '@umijs/bundler-webpack';
import { chalk, importLazy, lodash } from '@umijs/utils';
import path from 'path';
import { CACHE_PATH } from '../../constants';
import { logger } from '../../utils';
import { getCachePath, logger } from '../../utils';
import type { BundleConfigProvider } from '../config';
import {
getBabelPresetReactOpts,
Expand Down Expand Up @@ -84,7 +83,7 @@ async function bundle(opts: IBundleOpts): Promise<void | IBundleWatcher> {

// set cache parent directory, will join it with `bundler-webpack`
// ref: https://github.com/umijs/umi/blob/8dad8c5af0197cd62db11f4b4c85d6bc1db57db1/packages/bundler-webpack/src/build.ts#L32
cacheDirectoryPath: CACHE_PATH,
cacheDirectoryPath: getCachePath(),
},
entry: {
[path.parse(config.output.filename).name]: path.join(
Expand Down
5 changes: 2 additions & 3 deletions src/builder/bundless/dts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { chalk, fsExtra, winPath } from '@umijs/utils';
import fs from 'fs';
import path from 'path';
import tsPathsTransformer from 'typescript-transform-paths';
import { CACHE_PATH } from '../../../constants';
import { getCache, logger } from '../../../utils';
import { getCache, getCachePath, logger } from '../../../utils';
import { getContentHash } from '../../utils';

/**
Expand Down Expand Up @@ -50,7 +49,7 @@ export default async function getDeclarations(
) {
const cache = getCache('bundless-dts');
const enableCache = process.env.FATHER_CACHE !== 'none';
const tscCacheDir = path.join(opts.cwd, CACHE_PATH, 'tsc');
const tscCacheDir = path.join(opts.cwd, getCachePath(), 'tsc');
if (enableCache) {
// make tsc cache dir
fsExtra.ensureDirSync(tscCacheDir);
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const WATCH_DEBOUNCE_STEP = 300;
export const DEV_COMMAND = 'dev';
export const BUILD_COMMANDS = ['build', 'prebundle'];
export const DEBUG_BUNDLESS_NAME = 'father:bundless';
export const CACHE_PATH = 'node_modules/.cache/father';
export const DEFAULT_CACHE_PATH = 'node_modules/.cache/father';
export const DEFAULT_BUNDLESS_IGNORES = [
'**/.*',
'**/.*/**',
Expand Down
11 changes: 9 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ import { chalk, pkgUp, logger as umiLogger } from '@umijs/utils';
import Cache from 'file-system-cache';
import { builtinModules } from 'module';
import path, { isAbsolute } from 'path';
import { CACHE_PATH } from './constants';
import { DEFAULT_CACHE_PATH } from './constants';
import { IApi } from './types';

const caches: Record<string, ReturnType<typeof Cache>> = {};

/**
* get cache path
*/
export function getCachePath() {
return process.env.FATHER_CACHE_DIR || DEFAULT_CACHE_PATH;
}

/**
* get file-system cache for specific namespace
*/
Expand All @@ -16,7 +23,7 @@ export function getCache(ns: string): (typeof caches)['0'] {
const deferrer = () => Promise.resolve();
return { set: deferrer, get: deferrer, setSync() {}, getSync() {} } as any;
}
return (caches[ns] ??= Cache({ basePath: path.join(CACHE_PATH, ns) }));
return (caches[ns] ??= Cache({ basePath: path.join(getCachePath(), ns) }));
}

/**
Expand Down

0 comments on commit 4e35218

Please sign in to comment.