From b446af054056a01946f2f144c16db116b6d23db1 Mon Sep 17 00:00:00 2001 From: songsz Date: Tue, 5 Sep 2023 14:03:45 +0800 Subject: [PATCH] feat(monorepo): add monorepoRedirect.useRootProject (#11603) --- docs/docs/docs/api/config.md | 4 +++- .../preset-umi/src/features/monorepo/redirect.ts | 12 +++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/docs/docs/docs/api/config.md b/docs/docs/docs/api/config.md index ec3998adf07a..411a5df4e4a8 100644 --- a/docs/docs/docs/api/config.md +++ b/docs/docs/docs/api/config.md @@ -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` 场景改动子包不热更新的问题。 @@ -1098,6 +1098,8 @@ monorepoRedirect: { peerDeps: true } 经过重定向,依赖全局唯一,便可以在开发时保持和在 npm 上安装包后的体验一致。 +useRootProject: 当你的项目不在 monorepo 子文件夹里,而在 monorepo 根的话,你可以开启这个选项,以使 monorepoRedirect 生效。 + ## mpa - 类型:`object` diff --git a/packages/preset-umi/src/features/monorepo/redirect.ts b/packages/preset-umi/src/features/monorepo/redirect.ts index 31ae197ee687..58aed1e8f13d 100644 --- a/packages/preset-umi/src/features/monorepo/redirect.ts +++ b/packages/preset-umi/src/features/monorepo/redirect.ts @@ -11,6 +11,7 @@ interface IConfigs { srcDir?: string[]; exclude?: RegExp[]; peerDeps?: boolean; + useRootProject?: boolean; } export default (api: IApi) => { @@ -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); @@ -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(