Skip to content

Commit

Permalink
feat(monorepo): add monorepoRedirect.useRootProject (#11603)
Browse files Browse the repository at this point in the history
  • Loading branch information
songsz committed Sep 5, 2023
1 parent 344ff1a commit b446af0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
4 changes: 3 additions & 1 deletion docs/docs/docs/api/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,7 @@ mountElementId: 'container'

## monorepoRedirect

- 类型:`{ srcDir?: string[], exclude?: RegExp[], peerDeps?: boolean }`
- 类型:`{ srcDir?: string[], exclude?: RegExp[], peerDeps?: boolean, useRootProject?: boolean }`
- 默认值:`false`

在 monorepo 中使用 Umi 时,你可能需要引入其他子包的组件、工具方法等,通过开启此选项来重定向这些子包的导入到他们的源码位置(默认为 `src` 文件夹),这也可以解决 `MFSU` 场景改动子包不热更新的问题。
Expand Down Expand Up @@ -1098,6 +1098,8 @@ monorepoRedirect: { peerDeps: true }

经过重定向,依赖全局唯一,便可以在开发时保持和在 npm 上安装包后的体验一致。

useRootProject: 当你的项目不在 monorepo 子文件夹里,而在 monorepo 根的话,你可以开启这个选项,以使 monorepoRedirect 生效。

## mpa

- 类型:`object`
Expand Down
12 changes: 9 additions & 3 deletions packages/preset-umi/src/features/monorepo/redirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface IConfigs {
srcDir?: string[];
exclude?: RegExp[];
peerDeps?: boolean;
useRootProject?: boolean;
}

export default (api: IApi) => {
Expand All @@ -34,10 +35,17 @@ export default (api: IApi) => {
});

api.modifyConfig(async (memo) => {
const config: IConfigs = memo.monorepoRedirect || {};
const {
exclude = [],
srcDir = ['src'],
peerDeps = false,
useRootProject = false,
} = config;
const currentProjectRoot = process.env.APP_ROOT ? process.cwd() : api.cwd;
const rootPkg = await pkgUp({
// APP_ROOT: https://github.com/umijs/umi/issues/9461
cwd: dirname(currentProjectRoot),
cwd: useRootProject ? currentProjectRoot : dirname(currentProjectRoot),
});
if (!rootPkg) return memo;
const root = dirname(rootPkg);
Expand All @@ -46,8 +54,6 @@ export default (api: IApi) => {
`The 'monorepoRedirect' option can only be used in monorepo, you don't need configure.`,
);

const config: IConfigs = memo.monorepoRedirect || {};
const { exclude = [], srcDir = ['src'], peerDeps = false } = config;
// Note: not match `umi` package in local dev
if (isLocalDev()) {
logger.info(
Expand Down

0 comments on commit b446af0

Please sign in to comment.