@@ -14,6 +14,7 @@ import {
1414 parseEnvironmentConfig ,
1515} from '../HIR/Environment' ;
1616import { hasOwnProperty } from '../Utils/utils' ;
17+ import { fromZodError } from 'zod-validation-error' ;
1718
1819const PanicThresholdOptionsSchema = z . enum ( [
1920 /*
@@ -121,8 +122,19 @@ export type PluginOptions = {
121122 * Set this flag (on by default) to automatically check for this library and activate the support.
122123 */
123124 enableReanimatedCheck : boolean ;
125+
126+ /**
127+ * The minimum major version of React that the compiler should emit code for. If the target is 19
128+ * or higher, the compiler emits direct imports of React runtime APIs needed by the compiler. On
129+ * versions prior to 19, an extra runtime package react-compiler-runtime is necessary to provide
130+ * a userspace approximation of runtime APIs.
131+ */
132+ target : CompilerReactTarget ;
124133} ;
125134
135+ const CompilerReactTargetSchema = z . enum ( [ '17' , '18' , '19' ] ) ;
136+ export type CompilerReactTarget = z . infer < typeof CompilerReactTargetSchema > ;
137+
126138const CompilationModeSchema = z . enum ( [
127139 /*
128140 * Compiles functions annotated with "use forget" or component/hook-like functions.
@@ -210,6 +222,7 @@ export const defaultOptions: PluginOptions = {
210222 return filename . indexOf ( 'node_modules' ) === - 1 ;
211223 } ,
212224 enableReanimatedCheck : true ,
225+ target : '19' ,
213226} as const ;
214227
215228export function parsePluginOptions ( obj : unknown ) : PluginOptions {
@@ -222,25 +235,49 @@ export function parsePluginOptions(obj: unknown): PluginOptions {
222235 // normalize string configs to be case insensitive
223236 value = value . toLowerCase ( ) ;
224237 }
225- if ( key === 'environment' ) {
226- const environmentResult = parseEnvironmentConfig ( value ) ;
227- if ( environmentResult . isErr ( ) ) {
228- CompilerError . throwInvalidConfig ( {
229- reason :
230- 'Error in validating environment config. This is an advanced setting and not meant to be used directly' ,
231- description : environmentResult . unwrapErr ( ) . toString ( ) ,
232- suggestions : null ,
233- loc : null ,
234- } ) ;
238+ if ( isCompilerFlag ( key ) ) {
239+ switch ( key ) {
240+ case 'environment' : {
241+ const environmentResult = parseEnvironmentConfig ( value ) ;
242+ if ( environmentResult . isErr ( ) ) {
243+ CompilerError . throwInvalidConfig ( {
244+ reason :
245+ 'Error in validating environment config. This is an advanced setting and not meant to be used directly' ,
246+ description : environmentResult . unwrapErr ( ) . toString ( ) ,
247+ suggestions : null ,
248+ loc : null ,
249+ } ) ;
250+ }
251+ parsedOptions [ key ] = environmentResult . unwrap ( ) ;
252+ break ;
253+ }
254+ case 'target' : {
255+ parsedOptions [ key ] = parseTargetConfig ( value ) ;
256+ break ;
257+ }
258+ default : {
259+ parsedOptions [ key ] = value ;
260+ }
235261 }
236- parsedOptions [ key ] = environmentResult . unwrap ( ) ;
237- } else if ( isCompilerFlag ( key ) ) {
238- parsedOptions [ key ] = value ;
239262 }
240263 }
241264 return { ...defaultOptions , ...parsedOptions } ;
242265}
243266
267+ export function parseTargetConfig ( value : unknown ) : CompilerReactTarget {
268+ const parsed = CompilerReactTargetSchema . safeParse ( value ) ;
269+ if ( parsed . success ) {
270+ return parsed . data ;
271+ } else {
272+ CompilerError . throwInvalidConfig ( {
273+ reason : 'Not a valid target' ,
274+ description : `${ fromZodError ( parsed . error ) } ` ,
275+ suggestions : null ,
276+ loc : null ,
277+ } ) ;
278+ }
279+ }
280+
244281function isCompilerFlag ( s : string ) : s is keyof PluginOptions {
245282 return hasOwnProperty ( defaultOptions , s ) ;
246283}
0 commit comments