Skip to content

Commit 3634e1b

Browse files
committed
[rcr] Add target flag to compiler
ghstack-source-id: 8ca638b ghstack-comment-id: 2397700117 Pull Request resolved: #31143
1 parent ebe90ac commit 3634e1b

File tree

2 files changed

+59
-13
lines changed

2 files changed

+59
-13
lines changed

compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Options.ts

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
parseEnvironmentConfig,
1515
} from '../HIR/Environment';
1616
import {hasOwnProperty} from '../Utils/utils';
17+
import {fromZodError} from 'zod-validation-error';
1718

1819
const 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+
126138
const 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

215228
export 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+
244281
function isCompilerFlag(s: string): s is keyof PluginOptions {
245282
return hasOwnProperty(defaultOptions, s);
246283
}

compiler/packages/snap/src/compiler.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ function makePluginOptions(
5656
let enableChangeDetectionForDebugging = null;
5757
let customMacros: null | Array<Macro> = null;
5858
let validateBlocklistedImports = null;
59+
let target = '19' as const;
5960

6061
if (firstLine.indexOf('@compilationMode(annotation)') !== -1) {
6162
assert(
@@ -107,6 +108,13 @@ function makePluginOptions(
107108
if (runtimeModuleMatch) {
108109
runtimeModule = runtimeModuleMatch[1];
109110
}
111+
112+
const targetMatch = /@target="([^"]+)"/.exec(firstLine);
113+
if (targetMatch) {
114+
// @ts-ignore
115+
target = targetMatch[1];
116+
}
117+
110118
if (firstLine.includes('@panicThreshold(none)')) {
111119
panicThreshold = 'none';
112120
}
@@ -248,6 +256,7 @@ function makePluginOptions(
248256
flowSuppressions,
249257
ignoreUseNoForget,
250258
enableReanimatedCheck: false,
259+
target,
251260
};
252261
return [options, logs];
253262
}

0 commit comments

Comments
 (0)