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

feat(monorepo): add monorepoRedirect.useRootProject (#11603) #11604

Merged
merged 1 commit into from
Sep 14, 2023
Merged
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
4 changes: 3 additions & 1 deletion docs/docs/docs/api/config.md
Original file line number Diff line number Diff line change
@@ -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`
12 changes: 9 additions & 3 deletions packages/preset-umi/src/features/monorepo/redirect.ts
Original file line number Diff line number Diff line change
@@ -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(