Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: cache dir config can control compile fs cache location #1984

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading