Skip to content

Commit

Permalink
refactor: cache dir config can control compile fs cache location (#1984)
Browse files Browse the repository at this point in the history
  • Loading branch information
PeachScript committed Dec 13, 2023
1 parent 65e9759 commit 7975dca
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ export const USELESS_TMP_FILES = ['tsconfig.json', 'typings.d.ts'];
export const VERSION_2_LEVEL_NAV = '^2.2.0';

export const VERSION_2_DEPRECATE_SOFT_BREAKS = '^2.2.0';

export const FS_CACHE_DIR = 'node_modules/.cache/dumi';
29 changes: 20 additions & 9 deletions src/features/compile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { IDemoLoaderOptions } from '@/loaders/demo';
import type { IMdLoaderOptions } from '@/loaders/markdown';
import ReactTechStack from '@/techStacks/react';
import type { IApi, IDumiTechStack } from '@/types';
import { _setFSCacheDir } from '@/utils';
import path from 'path';
import { addAtomMeta, addExampleAssets } from '../assets';

export default (api: IApi) => {
Expand All @@ -14,17 +16,26 @@ export default (api: IApi) => {
fn: () => new ReactTechStack(),
});

// add customize option for babel-loader, to avoid collect wrong deps result in MFSU
api.modifyConfig((memo) => {
if (memo.babelLoaderCustomize) {
api.logger.warn(
'Config `babelLoaderCustomize` will be override by dumi, please report issue if you need it.',
);
}
api.modifyConfig({
stage: Infinity,
fn: (memo) => {
// add customize option for babel-loader, to avoid collect wrong deps result in MFSU
if (memo.babelLoaderCustomize) {
api.logger.warn(
'Config `babelLoaderCustomize` will be override by dumi, please report issue if you need it.',
);
}

memo.babelLoaderCustomize = require.resolve('./babelLoaderCustomize');
memo.babelLoaderCustomize = require.resolve('./babelLoaderCustomize');

return memo;
// configure dumi fs cache dir
const cacheDirPath =
api.userConfig.cacheDirectoryPath || memo.cacheDirectoryPath;

if (cacheDirPath) _setFSCacheDir(path.join(cacheDirPath, 'dumi'));

return memo;
},
});

// configure loader to compile markdown
Expand Down
8 changes: 6 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fs from 'fs';
import yaml from 'js-yaml';
import path from 'path';
import { lodash, logger, winPath } from 'umi/plugin-utils';
import { FS_CACHE_DIR } from './constants';

/**
* get route path from file-system path
Expand Down Expand Up @@ -81,14 +82,17 @@ export function parseCodeFrontmatter(raw: string) {
/**
* get file-system cache for specific namespace
*/
let cacheDir = FS_CACHE_DIR;
const caches: Record<string, ReturnType<typeof Cache>> = {};
const CACHE_PATH = 'node_modules/.cache/dumi';
export function _setFSCacheDir(dir: string) {
cacheDir = dir;
}
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;
}
return (caches[ns] ??= Cache({ basePath: path.join(CACHE_PATH, ns) }));
return (caches[ns] ??= Cache({ basePath: path.resolve(cacheDir, ns) }));
}

/**
Expand Down

0 comments on commit 7975dca

Please sign in to comment.