Skip to content

Commit

Permalink
feat: support react forget by config
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed May 16, 2024
1 parent d6a841c commit 3313b55
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 12 deletions.
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 到项目依赖。

## 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],
],
};
});
};
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.

0 comments on commit 3313b55

Please sign in to comment.