Skip to content

Commit 3fd3364

Browse files
authored
[rcr] Update default runtimeModule to react-compiler-runtime (#31144)
Updates the compiler to always import from `react-compiler-runtime` by default. The runtime then decides whether to use the official or userspace implementation of useMemoCache.
1 parent 8dd4cda commit 3fd3364

File tree

8 files changed

+124
-14
lines changed

8 files changed

+124
-14
lines changed

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/packages/babel-plugin-react-compiler/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"dist"
99
],
1010
"scripts": {
11+
"postinstall": "./scripts/link-react-compiler-runtime.sh",
1112
"build": "rimraf dist && rollup --config --bundleConfigAsCjs",
1213
"test": "yarn snap:ci",
1314
"jest": "yarn build && ts-node node_modules/.bin/jest",
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/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+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
## Input
3+
4+
```javascript
5+
// @target="18"
6+
7+
function Component() {
8+
return <div>Hello world</div>;
9+
}
10+
11+
export const FIXTURE_ENTRYPOINT = {
12+
fn: Component,
13+
params: [],
14+
isComponent: true,
15+
};
16+
17+
```
18+
19+
## Code
20+
21+
```javascript
22+
import { c as _c } from "react-compiler-runtime"; // @target="18"
23+
24+
function Component() {
25+
const $ = _c(1);
26+
let t0;
27+
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
28+
t0 = <div>Hello world</div>;
29+
$[0] = t0;
30+
} else {
31+
t0 = $[0];
32+
}
33+
return t0;
34+
}
35+
36+
export const FIXTURE_ENTRYPOINT = {
37+
fn: Component,
38+
params: [],
39+
isComponent: true,
40+
};
41+
42+
```
43+
44+
### Eval output
45+
(kind: ok) <div>Hello world</div>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @target="18"
2+
3+
function Component() {
4+
return <div>Hello world</div>;
5+
}
6+
7+
export const FIXTURE_ENTRYPOINT = {
8+
fn: Component,
9+
params: [],
10+
isComponent: true,
11+
};

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/userspace-use-memo-cache.expect.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@
22
## Input
33

44
```javascript
5-
// @runtimeModule="react-forget-runtime"
5+
// @runtimeModule="react-compiler-runtime"
6+
import {useState} from 'react';
7+
68
function Component(props) {
79
const [x, setX] = useState(1);
810
let y;
911
if (props.cond) {
1012
y = x * 2;
1113
}
1214
return (
13-
<Button
15+
<button
1416
onClick={() => {
1517
setX(10 * y);
16-
}}></Button>
18+
}}>
19+
Click me
20+
</button>
1721
);
1822
}
1923

@@ -28,7 +32,9 @@ export const FIXTURE_ENTRYPOINT = {
2832
## Code
2933

3034
```javascript
31-
import { c as _c } from "react-forget-runtime"; // @runtimeModule="react-forget-runtime"
35+
import { c as _c } from "react/compiler-runtime"; // @runtimeModule="react-compiler-runtime"
36+
import { useState } from "react";
37+
3238
function Component(props) {
3339
const $ = _c(5);
3440
const [x, setX] = useState(1);
@@ -48,11 +54,13 @@ function Component(props) {
4854
let t1;
4955
if ($[3] !== t0) {
5056
t1 = (
51-
<Button
57+
<button
5258
onClick={() => {
5359
setX(10 * y);
5460
}}
55-
/>
61+
>
62+
Click me
63+
</button>
5664
);
5765
$[3] = t0;
5866
$[4] = t1;

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/userspace-use-memo-cache.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
// @runtimeModule="react-forget-runtime"
1+
// @runtimeModule="react-compiler-runtime"
2+
import {useState} from 'react';
3+
24
function Component(props) {
35
const [x, setX] = useState(1);
46
let y;
57
if (props.cond) {
68
y = x * 2;
79
}
810
return (
9-
<Button
11+
<button
1012
onClick={() => {
1113
setX(10 * y);
12-
}}></Button>
14+
}}>
15+
Click me
16+
</button>
1317
);
1418
}
1519

0 commit comments

Comments
 (0)