Skip to content

Commit

Permalink
[compiler] Add meta internal option for useMemoCache import
Browse files Browse the repository at this point in the history
Adds `target: 'donotuse_meta_internal'`, which inserts useMemoCache imports directly from `react`. Note that this is only valid for Meta bundles, as others do not [re-export the `c` function](https://github.com/facebook/react/blob/5b0ef217ef32333a8e56f39be04327c89efa346f/packages/react/index.fb.js#L68-L70).

```js
// target=donotuse_meta_internal
import {c as _c} from 'react';

// target=19
import {c as _c} from 'react/compiler-runtime';

// target=17,18
import {c as _c} from 'react-compiler-runtime';
```

Meta is a bit special in that react runtime and compiler are guaranteed to be up-to-date and compatible. It also has its own bundling and module resolution logic, which makes importing from `react/compiler-runtime` tricky.

I'm also fine with implementing the alternative which adds an internal stub for `react-compiler-runtime` and [bundles](https://github.com/facebook/react/blob/5b0ef217ef32333a8e56f39be04327c89efa346f/scripts/rollup/bundles.js#L120) the runtime for internal builds.
  • Loading branch information
mofeiZ committed Dec 2, 2024
1 parent 5b0ef21 commit 138f747
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,19 @@ export type PluginOptions = {
target: CompilerReactTarget;
};

const CompilerReactTargetSchema = z.enum(['17', '18', '19']);
const CompilerReactTargetSchema = z.enum([
'17',
'18',
'19',
/**
* Used exclusively for Meta apps which are guaranteed to have compatible
* react runtime and compiler versions. Note that only the FB-internal bundles
* re-export useMemoCache (see
* https://github.com/facebook/react/blob/5b0ef217ef32333a8e56f39be04327c89efa346f/packages/react/index.fb.js#L68-L70),
* so this option is invalid / creates runtime errors for open-source users.
*/
'donotuse_meta_internal',
]);
export type CompilerReactTarget = z.infer<typeof CompilerReactTargetSchema>;

const CompilationModeSchema = z.enum([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1125,7 +1125,8 @@ function checkFunctionReferencedBeforeDeclarationAtTopLevel(

type ReactCompilerRuntimeModule =
| 'react/compiler-runtime' // from react namespace
| 'react-compiler-runtime'; // npm package
| 'react-compiler-runtime' // npm package
| 'react';
function getReactCompilerRuntimeModule(
opts: PluginOptions,
): ReactCompilerRuntimeModule {
Expand All @@ -1140,6 +1141,10 @@ function getReactCompilerRuntimeModule(
moduleName = 'react/compiler-runtime';
break;
}
case 'donotuse_meta_internal': {
moduleName = 'react';
break;
}
default:
CompilerError.invariant(moduleName != null, {
reason: 'Expected target to already be validated',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

## Input

```javascript
// @target="donotuse_meta_internal"

function Component() {
return <div>Hello world</div>;
}

export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [],
isComponent: true,
};

```

## Code

```javascript
import { c as _c } from "react"; // @target="donotuse_meta_internal"

function Component() {
const $ = _c(1);
let t0;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
t0 = <div>Hello world</div>;
$[0] = t0;
} else {
t0 = $[0];
}
return t0;
}

export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [],
isComponent: true,
};

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @target="donotuse_meta_internal"

function Component() {
return <div>Hello world</div>;
}

export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [],
isComponent: true,
};
1 change: 1 addition & 0 deletions compiler/packages/snap/src/SproutTodoFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ const skipFilter = new Set([
// Depends on external functions
'idx-method-no-outlining-wildcard',
'idx-method-no-outlining',
'target-flag-meta-internal',

// needs to be executed as a module
'meta-property',
Expand Down

0 comments on commit 138f747

Please sign in to comment.