Skip to content

Commit

Permalink
feat: support react/jsx-runtime (#12219)
Browse files Browse the repository at this point in the history
* feat: support react/jsx-runtime

* chore: isGTEReact16 > shouldUseAutomaticRuntime
  • Loading branch information
Wu-kung committed Mar 22, 2024
1 parent b4098c2 commit 2c43662
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 31 deletions.
7 changes: 7 additions & 0 deletions examples/with-react-16/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/.env.local
/.umirc.local.ts
/.umirc.local.js
/config/config.local.ts
/config/config.local.js
/src/.umi
/.umi
4 changes: 4 additions & 0 deletions examples/with-react-16/.umirc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
routes: [{ path: '/', component: 'index' }],
npmClient: 'pnpm',
};
16 changes: 16 additions & 0 deletions examples/with-react-16/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "@example/with-react-16",
"private": true,
"scripts": {
"build": "umi build",
"dev": "umi dev",
"preview": "umi preview",
"setup": "umi setup",
"start": "npm run dev"
},
"dependencies": {
"react": "^16.14.0",
"react-dom": "^16.14.0",
"umi": "workspace:*"
}
}
6 changes: 6 additions & 0 deletions examples/with-react-16/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { useState } from 'react';

export default function HomePage() {
const [count] = useState(0);
return <div>React: {count}</div>;
}
6 changes: 3 additions & 3 deletions packages/preset-umi/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ umi build --clean
umi: join(api.paths.absTmpPath, 'umi.ts'),
},
});
const isGTEReact17 =
const shouldUseAutomaticRuntime =
api.appData.react?.version &&
semver.gte(api.appData.react.version, '17.0.0');
semver.gte(api.appData.react.version, '16.14.0');
const opts = {
react: {
runtime: isGTEReact17 ? 'automatic' : 'classic',
runtime: shouldUseAutomaticRuntime ? 'automatic' : 'classic',
},
config: api.config,
cwd: api.cwd,
Expand Down
6 changes: 3 additions & 3 deletions packages/preset-umi/src/commands/dev/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,12 @@ PORT=8888 umi dev
},
});

const isGTEReact17 =
const shouldUseAutomaticRuntime =
api.appData.react?.version &&
semver.gte(api.appData.react.version, '17.0.0');
semver.gte(api.appData.react.version, '16.14.0');
const opts: any = {
react: {
runtime: isGTEReact17 ? 'automatic' : 'classic',
runtime: shouldUseAutomaticRuntime ? 'automatic' : 'classic',
},
config: api.config,
pkg: api.pkg,
Expand Down
6 changes: 3 additions & 3 deletions packages/preset-umi/src/commands/dev/getBabelOpts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { IApi } from '../../types';

export async function getBabelOpts(opts: { api: IApi }) {
// TODO: 支持用户自定义
const isGTEReact17 = semver.gte(opts.api.appData.react.version, '17.0.0');
const shouldUseAutomaticRuntime = semver.gte(opts.api.appData.react.version, '16.14.0');
const babelPresetOpts = await opts.api.applyPlugins({
key: 'modifyBabelPresetOpts',
initialValue: {
presetEnv: {},
presetReact: {
runtime: isGTEReact17 ? 'automatic' : 'classic',
runtime: shouldUseAutomaticRuntime ? 'automatic' : 'classic',
// importSource cannot be set when runtime is classic
...(isGTEReact17 ? {} : { importSource: undefined }),
...(shouldUseAutomaticRuntime ? {} : { importSource: undefined }),
},
presetTypeScript: {},
pluginTransformRuntime: {},
Expand Down
62 changes: 40 additions & 22 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 2c43662

Please sign in to comment.