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

RFC:react 支持 classical 的 runtime 配置 #871

Closed
sorrycc opened this issue Jan 16, 2024 · 0 comments · Fixed by #872
Closed

RFC:react 支持 classical 的 runtime 配置 #871

sorrycc opened this issue Jan 16, 2024 · 0 comments · Fixed by #872
Labels

Comments

@sorrycc
Copy link
Member

sorrycc commented Jan 16, 2024

问题

#828

分析

runtime 有两个配置,automatic 和 classic。参考 https://babeljs.io/docs/babel-plugin-transform-react-jsx#runtime

比如

function App() {
  return <div>1</div>;
}

automatic 时的产物如下,

import { jsx as _jsx } from "react/jsx-runtime";
function App() {
  return /*#__PURE__*/_jsx("div", {
    children: "1"
  });
}

classic 的产物如下,

function App() {
  return /*#__PURE__*/React.createElement("div", null, "1");
}

Umi 里是基于项目的 React 版本会自动判断,runtime: isGTEReact17 ? "automatic" : "classic",,参考 https://github.com/umijs/umi/blob/6f63435/packages/preset-umi/src/commands/dev/getBabelOpts.ts#L12

方案

Mako 层提供配置,Umi 层和现有逻辑保持一致。

1、Mako 层提供 react 配置,包含 runtime、import_source、pragma 三个配置。

runtime: "automatic" | "classic",
pragma: string,
import_source: string,

默认值是:

runtime: "automatic",
pragma: "React.createElement"
import_source: "react"

emotion 插件修改配置项:

pragma: "jsx"
import_source: "@emotion/react"

2、bundler-okam 层基于透传 react opts,这个目前没有,需在 umi 层新增。

const isGTEReact17 = semver.gte(opts.api.appData.react.version, '17.0.0');
react: {
  runtime: isGTEReact17 ? "automatic" : "classic",
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant