Skip to content

Commit 553b0d0

Browse files
committed
Update
[ghstack-poisoned]
2 parents bec44a7 + 1420234 commit 553b0d0

133 files changed

Lines changed: 4582 additions & 912 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

compiler/apps/playground/babel.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = function (api) {
1313
[
1414
'babel-plugin-react-compiler',
1515
{
16-
runtimeModule: 'react-compiler-runtime',
16+
target: '18',
1717
},
1818
],
1919
],

compiler/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
"test": "yarn workspaces run test",
2020
"snap": "yarn workspace babel-plugin-react-compiler run snap",
2121
"snap:build": "yarn workspace snap run build",
22-
"postinstall": "perl -p -i -e 's/react\\.element/react.transitional.element/' node_modules/fbt/lib/FbtReactUtil.js && perl -p -i -e 's/didWarnAboutUsingAct = false;/didWarnAboutUsingAct = true;/' node_modules/react-dom/cjs/react-dom-test-utils.development.js",
2322
"npm:publish": "node scripts/release/publish"
2423
},
2524
"dependencies": {

compiler/packages/babel-plugin-react-compiler/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
],
1010
"scripts": {
1111
"build": "rimraf dist && rollup --config --bundleConfigAsCjs",
12-
"test": "yarn snap:ci",
12+
"test": "./scripts/link-react-compiler-runtime.sh && yarn snap:ci",
1313
"jest": "yarn build && ts-node node_modules/.bin/jest",
1414
"snap": "node ../snap/dist/main.js",
1515
"snap:build": "yarn workspace snap run build",
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
# Copyright (c) Meta Platforms, Inc. and affiliates.
3+
#
4+
# This source code is licensed under the MIT license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
set -eo pipefail
8+
9+
yarn --silent workspace react-compiler-runtime link
10+
yarn --silent workspace babel-plugin-react-compiler link react-compiler-runtime

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

Lines changed: 50 additions & 25 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
/*
@@ -86,17 +87,6 @@ export type PluginOptions = {
8687
*/
8788
compilationMode: CompilationMode;
8889

89-
/*
90-
* If enabled, Forget will import `useMemoCache` from the given module
91-
* instead of `react/compiler-runtime`.
92-
*
93-
* ```
94-
* // If set to "react-compiler-runtime"
95-
* import {c as useMemoCache} from 'react-compiler-runtime';
96-
* ```
97-
*/
98-
runtimeModule?: string | null | undefined;
99-
10090
/**
10191
* By default React Compiler will skip compilation of code that suppresses the default
10292
* React ESLint rules, since this is a strong indication that the code may be breaking React rules
@@ -121,8 +111,19 @@ export type PluginOptions = {
121111
* Set this flag (on by default) to automatically check for this library and activate the support.
122112
*/
123113
enableReanimatedCheck: boolean;
114+
115+
/**
116+
* The minimum major version of React that the compiler should emit code for. If the target is 19
117+
* or higher, the compiler emits direct imports of React runtime APIs needed by the compiler. On
118+
* versions prior to 19, an extra runtime package react-compiler-runtime is necessary to provide
119+
* a userspace approximation of runtime APIs.
120+
*/
121+
target: CompilerReactTarget;
124122
};
125123

124+
const CompilerReactTargetSchema = z.enum(['17', '18', '19']);
125+
export type CompilerReactTarget = z.infer<typeof CompilerReactTargetSchema>;
126+
126127
const CompilationModeSchema = z.enum([
127128
/*
128129
* Compiles functions annotated with "use forget" or component/hook-like functions.
@@ -202,14 +203,14 @@ export const defaultOptions: PluginOptions = {
202203
logger: null,
203204
gating: null,
204205
noEmit: false,
205-
runtimeModule: null,
206206
eslintSuppressionRules: null,
207207
flowSuppressions: true,
208208
ignoreUseNoForget: false,
209209
sources: filename => {
210210
return filename.indexOf('node_modules') === -1;
211211
},
212212
enableReanimatedCheck: true,
213+
target: '19',
213214
} as const;
214215

215216
export function parsePluginOptions(obj: unknown): PluginOptions {
@@ -222,25 +223,49 @@ export function parsePluginOptions(obj: unknown): PluginOptions {
222223
// normalize string configs to be case insensitive
223224
value = value.toLowerCase();
224225
}
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-
});
226+
if (isCompilerFlag(key)) {
227+
switch (key) {
228+
case 'environment': {
229+
const environmentResult = parseEnvironmentConfig(value);
230+
if (environmentResult.isErr()) {
231+
CompilerError.throwInvalidConfig({
232+
reason:
233+
'Error in validating environment config. This is an advanced setting and not meant to be used directly',
234+
description: environmentResult.unwrapErr().toString(),
235+
suggestions: null,
236+
loc: null,
237+
});
238+
}
239+
parsedOptions[key] = environmentResult.unwrap();
240+
break;
241+
}
242+
case 'target': {
243+
parsedOptions[key] = parseTargetConfig(value);
244+
break;
245+
}
246+
default: {
247+
parsedOptions[key] = value;
248+
}
235249
}
236-
parsedOptions[key] = environmentResult.unwrap();
237-
} else if (isCompilerFlag(key)) {
238-
parsedOptions[key] = value;
239250
}
240251
}
241252
return {...defaultOptions, ...parsedOptions};
242253
}
243254

255+
export function parseTargetConfig(value: unknown): CompilerReactTarget {
256+
const parsed = CompilerReactTargetSchema.safeParse(value);
257+
if (parsed.success) {
258+
return parsed.data;
259+
} else {
260+
CompilerError.throwInvalidConfig({
261+
reason: 'Not a valid target',
262+
description: `${fromZodError(parsed.error)}`,
263+
suggestions: null,
264+
loc: null,
265+
});
266+
}
267+
}
268+
244269
function isCompilerFlag(s: string): s is keyof PluginOptions {
245270
return hasOwnProperty(defaultOptions, s);
246271
}

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

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,6 @@ export function compileProgram(
298298
return;
299299
}
300300
const useMemoCacheIdentifier = program.scope.generateUidIdentifier('c');
301-
const moduleName = pass.opts.runtimeModule ?? 'react/compiler-runtime';
302301

303302
/*
304303
* Record lint errors and critical errors as depending on Forget's config,
@@ -605,7 +604,7 @@ export function compileProgram(
605604
if (needsMemoCacheFunctionImport) {
606605
updateMemoCacheFunctionImport(
607606
program,
608-
moduleName,
607+
getReactCompilerRuntimeModule(pass.opts),
609608
useMemoCacheIdentifier.name,
610609
);
611610
}
@@ -638,8 +637,12 @@ function shouldSkipCompilation(
638637
}
639638
}
640639

641-
const moduleName = pass.opts.runtimeModule ?? 'react/compiler-runtime';
642-
if (hasMemoCacheFunctionImport(program, moduleName)) {
640+
if (
641+
hasMemoCacheFunctionImport(
642+
program,
643+
getReactCompilerRuntimeModule(pass.opts),
644+
)
645+
) {
643646
return true;
644647
}
645648
return false;
@@ -1126,3 +1129,31 @@ function checkFunctionReferencedBeforeDeclarationAtTopLevel(
11261129

11271130
return errors.details.length > 0 ? errors : null;
11281131
}
1132+
1133+
type ReactCompilerRuntimeModule =
1134+
| 'react/compiler-runtime' // from react namespace
1135+
| 'react-compiler-runtime'; // npm package
1136+
function getReactCompilerRuntimeModule(
1137+
opts: PluginOptions,
1138+
): ReactCompilerRuntimeModule {
1139+
let moduleName: ReactCompilerRuntimeModule | null = null;
1140+
switch (opts.target) {
1141+
case '17':
1142+
case '18': {
1143+
moduleName = 'react-compiler-runtime';
1144+
break;
1145+
}
1146+
case '19': {
1147+
moduleName = 'react/compiler-runtime';
1148+
break;
1149+
}
1150+
default:
1151+
CompilerError.invariant(moduleName != null, {
1152+
reason: 'Expected target to already be validated',
1153+
description: null,
1154+
loc: null,
1155+
suggestions: null,
1156+
});
1157+
}
1158+
return moduleName;
1159+
}

0 commit comments

Comments
 (0)