Skip to content

Commit

Permalink
refactor: enable mfsu for pnpm project (#1545)
Browse files Browse the repository at this point in the history
* refactor: enable mfsu for pnpm project

* refactor: update logic
  • Loading branch information
PeachScript committed Mar 17, 2023
1 parent 95c940f commit 0d2d7ac
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 43 deletions.
12 changes: 0 additions & 12 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,4 @@ export const PICKED_PKG_FIELDS = {
authors: '',
};

export const CLIENT_DEPS = [
'@ant-design/icons-svg',
'@makotot/ghostui',
'deepmerge',
'lodash.throttle',
'prism-react-renderer',
'prismjs',
'rc-tabs',
'react-copy-to-clipboard',
'react-intl',
];

export const USELESS_TMP_FILES = ['tsconfig.json', 'typings.d.ts'];
45 changes: 14 additions & 31 deletions src/features/derivative.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CLIENT_DEPS, LOCAL_PAGES_DIR, USELESS_TMP_FILES } from '@/constants';
import { LOCAL_PAGES_DIR, USELESS_TMP_FILES } from '@/constants';
import type { IApi } from '@/types';
import assert from 'assert';
import fs from 'fs';
Expand Down Expand Up @@ -109,36 +109,19 @@ export default (api: IApi) => {
]);

api.modifyDefaultConfig((memo) => {
if (api.userConfig.mfsu !== false) {
if (
fs.existsSync(path.join(api.cwd, 'node_modules', '.pnpm')) ||
process.platform === 'win32'
) {
// FIXME: mfsu compatibility for pnpm and windows
// mfsu normal model will broken in pnpm mode, because dumi exclude client
// files in mfsu mode and umi cannot resolve nested deps from dumi client
// and mfsu will broken on window platform with unknown reason
memo.mfsu = false;
} else {
// only normal mode is supported, because src is not fixed in dumi project, eager mode may scan wrong dir
memo.mfsu.strategy = 'normal';

// make react singleton, because MFSU only process import
// but the parsed code block demo will has require('react')
memo.mfsu.shared = {
react: { singleton: true },
'react-dom': { singleton: true },
};

// alias all client dependencies, to make sure normal mfsu can resolve them, until umi fixed
// ref: https://github.com/umijs/umi/blob/de59054b2afe6ba92d0b52b530d71612ac4055a8/packages/mfsu/src/dep/dep.ts#L91-L92
CLIENT_DEPS.forEach((pkg) => {
memo.alias ??= {};
memo.alias[pkg] = winPath(
path.dirname(require.resolve(`${pkg}/package.json`)),
);
});
}
if (process.platform === 'win32') {
// FIXME: mfsu will broken on window platform for unknown reason
memo.mfsu = false;
} else if (api.userConfig.mfsu !== false) {
// only normal mode is supported, because src is not fixed in dumi project, eager mode may scan wrong dir
memo.mfsu.strategy = 'normal';

// make react singleton, because MFSU only process import
// but the parsed code block demo will has require('react')
memo.mfsu.shared = {
react: { singleton: true },
'react-dom': { singleton: true },
};
}

// enable conventional routes
Expand Down

0 comments on commit 0d2d7ac

Please sign in to comment.