@@ -83,10 +83,7 @@ export const isVersionBeyond17 = (version: string): boolean => {
83
83
return semver . gte ( semver . minVersion ( version ) ! , '17.0.0' ) ;
84
84
} ;
85
85
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 => {
90
87
const pkgPath = pkgUp . sync ( { cwd } ) ;
91
88
92
89
if ( ! pkgPath ) {
@@ -102,48 +99,46 @@ export const isBeyondReact17 = (cwd: string) => {
102
99
if ( typeof deps . react !== 'string' ) {
103
100
return false ;
104
101
}
102
+ try {
103
+ const reactPath = require . resolve ( 'react/package.json' , { paths : [ cwd ] } ) ;
105
104
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 ;
111
106
112
- if ( ! pkgPath ) {
107
+ return reactVersion ;
108
+ } catch ( error ) {
109
+ console . error ( 'Failed to resolve React version:' , error ) ;
113
110
return false ;
114
111
}
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 ) ;
115
118
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 ) {
123
120
return false ;
124
121
}
125
-
126
- return semver . satisfies ( semver . minVersion ( deps . react ) ! , '>=16.14.0' ) ;
122
+ return isVersionBeyond17 ( reactVersion ) ;
127
123
} ;
128
124
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 ) ;
131
127
132
- if ( ! fs . existsSync ( pkgPath ) ) {
128
+ if ( ! reactVersion ) {
133
129
return false ;
134
130
}
135
131
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
+ } ;
141
134
142
- if ( typeof deps . react !== 'string' ) {
135
+ export const isReact18 = ( cwd : string = process . cwd ( ) ) => {
136
+ const reactVersion = getReactVersion ( cwd ) ;
137
+
138
+ if ( ! reactVersion ) {
143
139
return false ;
144
140
}
145
-
146
- return semver . gte ( semver . minVersion ( deps . react ) ! , '18.0.0' ) ;
141
+ return semver . gte ( semver . minVersion ( reactVersion ) ! , '18.0.0' ) ;
147
142
} ;
148
143
149
144
/**
0 commit comments