Skip to content

Commit 53e3ae0

Browse files
authored
feat: support automatic jsx runtime while using react@>=16.14.0 (#6612)
1 parent 35e45f8 commit 53e3ae0

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

.changeset/funny-beds-arrive.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@modern-js/plugin-testing': patch
3+
'@modern-js/uni-builder': patch
4+
'@modern-js/utils': patch
5+
---
6+
7+
fix: React is undefined while using react@16.14.0 and webpack mode
8+
fix: 使用 react@16.14.0 且在 webpack 模式下时报错 React is undefined

packages/cli/uni-builder/src/webpack/plugins/babel.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { BabelConfig } from '@modern-js/babel-preset';
22
import { getBabelConfigForNode } from '@modern-js/babel-preset/node';
33
import { getBabelConfigForWeb } from '@modern-js/babel-preset/web';
4-
import { applyOptionsChain, isBeyondReact17 } from '@modern-js/utils';
4+
import { applyOptionsChain, isSupportAutomaticJsx } from '@modern-js/utils';
55
import { logger } from '@rsbuild/core';
66
import type {
77
NormalizedEnvironmentConfig,
@@ -25,7 +25,7 @@ import {
2525
* webpack mode: uni-builder:babel -> uni-builder:ts-loader -> rsbuild-webpack:swc
2626
*/
2727
export const getPresetReact = (rootPath: string, isProd: boolean) => {
28-
const isNewJsx = isBeyondReact17(rootPath);
28+
const isNewJsx = isSupportAutomaticJsx(rootPath);
2929

3030
const presetReactOptions = {
3131
development: !isProd,

packages/runtime/plugin-testing/src/base/config/transformer/babelTransformer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { isBeyondReact17 } from '@modern-js/utils';
1+
import { isSupportAutomaticJsx } from '@modern-js/utils';
22
import babelJest from 'babel-jest';
33

4-
const isNewJsx = isBeyondReact17(process.cwd());
4+
const isNewJsx = isSupportAutomaticJsx(process.cwd());
55

66
const babelTransformer = (babelJest.createTransformer as any)?.({
77
presets: [

packages/toolkit/utils/src/cli/is/project.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ export const isWebOnly = async () => {
7979
return Boolean(options['web-only']);
8080
};
8181

82+
/**
83+
* @deprecated Use {@link isSupportAutomaticJsx} to check if the project supports automatic JSX instead.
84+
*/
8285
export const isBeyondReact17 = (cwd: string) => {
8386
const pkgPath = pkgUp.sync({ cwd });
8487

@@ -99,6 +102,26 @@ export const isBeyondReact17 = (cwd: string) => {
99102
return semver.satisfies(semver.minVersion(deps.react)!, '>=17.0.0');
100103
};
101104

105+
export const isSupportAutomaticJsx = (cwd: string) => {
106+
const pkgPath = pkgUp.sync({ cwd });
107+
108+
if (!pkgPath) {
109+
return false;
110+
}
111+
112+
const pkgInfo = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
113+
const deps = {
114+
...pkgInfo.devDependencies,
115+
...pkgInfo.dependencies,
116+
};
117+
118+
if (typeof deps.react !== 'string') {
119+
return false;
120+
}
121+
122+
return semver.satisfies(semver.minVersion(deps.react)!, '>=16.14.0');
123+
};
124+
102125
export const isReact18 = (cwd: string = process.cwd()) => {
103126
const pkgPath = path.join(cwd, 'package.json');
104127

0 commit comments

Comments
 (0)