diff --git a/src/builder/bundle/index.ts b/src/builder/bundle/index.ts index 350542a8..30a9e0ee 100644 --- a/src/builder/bundle/index.ts +++ b/src/builder/bundle/index.ts @@ -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, @@ -84,7 +83,7 @@ async function bundle(opts: IBundleOpts): Promise { // 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( diff --git a/src/builder/bundless/dts/index.ts b/src/builder/bundless/dts/index.ts index 84220664..5c4f30b8 100644 --- a/src/builder/bundless/dts/index.ts +++ b/src/builder/bundless/dts/index.ts @@ -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'; /** @@ -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); diff --git a/src/constants.ts b/src/constants.ts index f7b7ff33..f7a6aef6 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -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 = [ '**/.*', '**/.*/**', diff --git a/src/utils.ts b/src/utils.ts index 629851e1..b860a6b1 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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> = {}; +/** + * get cache path + */ +export function getCachePath() { + return process.env.FATHER_CACHE_DIR || DEFAULT_CACHE_PATH; +} + /** * get file-system cache for specific namespace */ @@ -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) })); } /**