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: support react forget by config #12396

Merged
merged 1 commit into from
May 16, 2024
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
18 changes: 18 additions & 0 deletions docs/docs/docs/api/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,24 @@ favicons: [
]
```

## forget

- 类型:`{ ReactCompilerConfig: object }`
- 默认值:`null`

是否开启 React Compiler(React Forget)功能。参考 https://react.dev/learn/react-compiler 。

```ts
forget: {
ReactCompilerConfig: {},
},
```

注意:

1、forget 和 mfsu、mako 暂时不兼容,如果开启了 forget,同时 mfsu、mako 有打开时会抛错。
2、forget 需要 react 19,使用时,请手动安装 react@rc 和 react-dom@rc 到项目依赖。

fz6m marked this conversation as resolved.
Show resolved Hide resolved
## forkTSChecker

- 类型:`object`
Expand Down
1 change: 1 addition & 0 deletions packages/preset-umi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@umijs/utils": "workspace:*",
"@umijs/zod2ts": "workspace:*",
"babel-plugin-dynamic-import-node": "2.3.3",
"babel-plugin-react-compiler": "0.0.0-experimental-c23de8d-20240515",
"click-to-react-component": "^1.0.8",
"core-js": "3.34.0",
"current-script-polyfill": "1.0.0",
Expand Down
48 changes: 48 additions & 0 deletions packages/preset-umi/src/features/forget/forget.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { IApi } from '../../types';

export default (api: IApi) => {
api.describe({
key: 'forget',
config: {
schema({ zod }) {
return zod.object({
ReactCompilerConfig: zod.object({}).optional(),
});
},
},
enableBy: api.EnableBy.config,
});

api.onCheckConfig(() => {
if (api.config.mfsu) {
throw new Error(
`forget is not compatible with mfsu, please disable mfsu first.`,
);
}
if (api.config.mako) {
throw new Error(
`forget is not compatible with mako, please disable mako first.`,
);
}
});

api.onCheck(() => {
let reactMajorVersion = api.appData.react.version.split('.')[0];
if (reactMajorVersion < 19) {
throw new Error(
`forget is only compatible with React 19 and above, please upgrade your React version.`,
);
}
});

api.modifyConfig((memo) => {
let ReactCompilerConfig = api.userConfig.forget.ReactCompilerConfig || {};
return {
...memo,
extraBabelPlugins: [
...(memo.extraBabelPlugins || []),
[require.resolve('babel-plugin-react-compiler'), ReactCompilerConfig],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

由于 babel-plugin-react-compiler 还是试验阶段的库,所以可能每天或者经常都被发布更新,可能存在很多 bug ,在不断的修复和发布过程中,很快 umi 仓库内锁定的这个依赖的版本就会陈旧,所以应该优先读一下用户自己安装的 babel-plugin-react-compiler ,如果用户没安装再用内置的。

],
};
});
};
1 change: 1 addition & 0 deletions packages/preset-umi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export default () => {
require.resolve('./features/mako/mako'),
require.resolve('./features/hmrGuardian/hmrGuardian'),
require.resolve('./features/routePreloadOnLoad/routePreloadOnLoad'),
require.resolve('./features/forget/forget'),

// commands
require.resolve('./commands/build'),
Expand Down
95 changes: 83 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading