Skip to content

Commit 0d47cb8

Browse files
authored
feat: use require.resolve to check react version to support pnpm catalog (#6919)
1 parent 985ee96 commit 0d47cb8

File tree

2 files changed

+32
-30
lines changed

2 files changed

+32
-30
lines changed

.changeset/hungry-eggs-live.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@modern-js/utils': patch
3+
---
4+
5+
feat: use require.resolve to check react version to fix use pnpm catalog in pnpm workspace
6+
7+
feat: 使用 require.resolve 来检查 react 版本,以修复 pnpm workspace 中使用 pnpm catalog 的问题

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

+25-30
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,7 @@ export const isVersionBeyond17 = (version: string): boolean => {
8383
return semver.gte(semver.minVersion(version)!, '17.0.0');
8484
};
8585

86-
/**
87-
* @deprecated Use {@link isSupportAutomaticJsx} to check if the project supports automatic JSX instead.
88-
*/
89-
export const isBeyondReact17 = (cwd: string) => {
86+
export const getReactVersion = (cwd: string): string | false => {
9087
const pkgPath = pkgUp.sync({ cwd });
9188

9289
if (!pkgPath) {
@@ -102,48 +99,46 @@ export const isBeyondReact17 = (cwd: string) => {
10299
if (typeof deps.react !== 'string') {
103100
return false;
104101
}
102+
try {
103+
const reactPath = require.resolve('react/package.json', { paths: [cwd] });
105104

106-
return isVersionBeyond17(deps.react);
107-
};
108-
109-
export const isSupportAutomaticJsx = (cwd: string) => {
110-
const pkgPath = pkgUp.sync({ cwd });
105+
const reactVersion = JSON.parse(fs.readFileSync(reactPath, 'utf8')).version;
111106

112-
if (!pkgPath) {
107+
return reactVersion;
108+
} catch (error) {
109+
console.error('Failed to resolve React version:', error);
113110
return false;
114111
}
112+
};
113+
/**
114+
* @deprecated Use {@link isSupportAutomaticJsx} to check if the project supports automatic JSX instead.
115+
*/
116+
export const isBeyondReact17 = (cwd: string) => {
117+
const reactVersion = getReactVersion(cwd);
115118

116-
const pkgInfo = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
117-
const deps = {
118-
...pkgInfo.devDependencies,
119-
...pkgInfo.dependencies,
120-
};
121-
122-
if (typeof deps.react !== 'string') {
119+
if (!reactVersion) {
123120
return false;
124121
}
125-
126-
return semver.satisfies(semver.minVersion(deps.react)!, '>=16.14.0');
122+
return isVersionBeyond17(reactVersion);
127123
};
128124

129-
export const isReact18 = (cwd: string = process.cwd()) => {
130-
const pkgPath = path.join(cwd, 'package.json');
125+
export const isSupportAutomaticJsx = (cwd: string) => {
126+
const reactVersion = getReactVersion(cwd);
131127

132-
if (!fs.existsSync(pkgPath)) {
128+
if (!reactVersion) {
133129
return false;
134130
}
135131

136-
const pkgInfo = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
137-
const deps = {
138-
...pkgInfo.devDependencies,
139-
...pkgInfo.dependencies,
140-
};
132+
return semver.satisfies(semver.minVersion(reactVersion)!, '>=16.14.0');
133+
};
141134

142-
if (typeof deps.react !== 'string') {
135+
export const isReact18 = (cwd: string = process.cwd()) => {
136+
const reactVersion = getReactVersion(cwd);
137+
138+
if (!reactVersion) {
143139
return false;
144140
}
145-
146-
return semver.gte(semver.minVersion(deps.react)!, '18.0.0');
141+
return semver.gte(semver.minVersion(reactVersion)!, '18.0.0');
147142
};
148143

149144
/**

0 commit comments

Comments
 (0)