Skip to content

Commit 632f88d

Browse files
authored
[compiler] Allow ReactElement symbol to be configured when inlining jsx (#30996)
Based on #30995 ([rendered diff](https://github.com/jackpope/react/compare/inline-jsx-2...jackpope:react:inline-jsx-3?expand=1)) ____ Some apps still use `react.element` symbols. Not only do we want to test there but we also want to be able to upgrade those sites to `react.transitional.element` without blocking on the compiler (we can change the symbol feature flag and compiler config at the same time). The compiler runtime uses `react.transitional.element`, so the snap fixture will fail if we change the default here. However I confirmed that commenting out the fixture entrypoint and running snap with `react.element` will update the fixture symbols as expected.
1 parent d5e955d commit 632f88d

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,8 @@ function* runWithEnvironment(
352352
});
353353
}
354354

355-
if (env.config.enableInlineJsxTransform) {
356-
inlineJsxTransform(hir);
355+
if (env.config.inlineJsxTransform) {
356+
inlineJsxTransform(hir, env.config.inlineJsxTransform);
357357
yield log({
358358
kind: 'hir',
359359
name: 'inlineJsxTransform',

compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ import {
5050
import {Scope as BabelScope} from '@babel/traverse';
5151
import {TypeSchema} from './TypeSchema';
5252

53+
export const ReactElementSymbolSchema = z.object({
54+
elementSymbol: z.union([
55+
z.literal('react.element'),
56+
z.literal('react.transitional.element'),
57+
]),
58+
});
59+
5360
export const ExternalFunctionSchema = z.object({
5461
// Source for the imported module that exports the `importSpecifierName` functions
5562
source: z.string(),
@@ -237,8 +244,10 @@ const EnvironmentConfigSchema = z.object({
237244
* Enables inlining ReactElement object literals in place of JSX
238245
* An alternative to the standard JSX transform which replaces JSX with React's jsxProd() runtime
239246
* Currently a prod-only optimization, requiring Fast JSX dependencies
247+
*
248+
* The symbol configuration is set for backwards compatability with pre-React 19 transforms
240249
*/
241-
enableInlineJsxTransform: z.boolean().default(false),
250+
inlineJsxTransform: ReactElementSymbolSchema.nullish(),
242251

243252
/*
244253
* Enable validation of hooks to partially check that the component honors the rules of hooks.

compiler/packages/babel-plugin-react-compiler/src/Optimization/InlineJsxTransform.ts

+9-12
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
markPredecessors,
2424
reversePostorderBlocks,
2525
} from '../HIR/HIRBuilder';
26-
import {CompilerError} from '..';
26+
import {CompilerError, EnvironmentConfig} from '..';
2727

2828
function createSymbolProperty(
2929
fn: HIRFunction,
@@ -316,7 +316,12 @@ function createPropsProperties(
316316
}
317317

318318
// TODO: Make PROD only with conditional statements
319-
export function inlineJsxTransform(fn: HIRFunction): void {
319+
export function inlineJsxTransform(
320+
fn: HIRFunction,
321+
inlineJsxTransformConfig: NonNullable<
322+
EnvironmentConfig['inlineJsxTransform']
323+
>,
324+
): void {
320325
for (const [, block] of fn.body.blocks) {
321326
let nextInstructions: Array<Instruction> | null = null;
322327
for (let i = 0; i < block.instructions.length; i++) {
@@ -344,11 +349,7 @@ export function inlineJsxTransform(fn: HIRFunction): void {
344349
instr,
345350
nextInstructions,
346351
'$$typeof',
347-
/**
348-
* TODO: Add this to config so we can switch between
349-
* react.element / react.transitional.element
350-
*/
351-
'react.transitional.element',
352+
inlineJsxTransformConfig.elementSymbol,
352353
),
353354
createTagProperty(fn, instr, nextInstructions, instr.value.tag),
354355
refProperty,
@@ -384,11 +385,7 @@ export function inlineJsxTransform(fn: HIRFunction): void {
384385
instr,
385386
nextInstructions,
386387
'$$typeof',
387-
/**
388-
* TODO: Add this to config so we can switch between
389-
* react.element / react.transitional.element
390-
*/
391-
'react.transitional.element',
388+
inlineJsxTransformConfig.elementSymbol,
392389
),
393390
createSymbolProperty(
394391
fn,

compiler/packages/snap/src/compiler.ts

+7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import type {
2121
} from 'babel-plugin-react-compiler/src/Entrypoint';
2222
import type {Effect, ValueKind} from 'babel-plugin-react-compiler/src/HIR';
2323
import type {
24+
EnvironmentConfig,
2425
Macro,
2526
MacroMethod,
2627
parseConfigPragma as ParseConfigPragma,
@@ -201,6 +202,11 @@ function makePluginOptions(
201202
};
202203
}
203204

205+
let inlineJsxTransform: EnvironmentConfig['inlineJsxTransform'] = null;
206+
if (firstLine.includes('@enableInlineJsxTransform')) {
207+
inlineJsxTransform = {elementSymbol: 'react.transitional.element'};
208+
}
209+
204210
let logs: Array<{filename: string | null; event: LoggerEvent}> = [];
205211
let logger: Logger | null = null;
206212
if (firstLine.includes('@logger')) {
@@ -230,6 +236,7 @@ function makePluginOptions(
230236
enableChangeDetectionForDebugging,
231237
lowerContextAccess,
232238
validateBlocklistedImports,
239+
inlineJsxTransform,
233240
},
234241
compilationMode,
235242
logger,

0 commit comments

Comments
 (0)