-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
compiler: fixtures for fast-refresh mode (w todos)
ghstack-source-id: 65dd14fe9b37328bd60fe791b23dde54da10b285 Pull Request resolved: #29175
- Loading branch information
1 parent
c272789
commit afb2c39
Showing
8 changed files
with
302 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...ests__/fixtures/compiler/fast-refresh-dont-refresh-const-changes-prod.expect.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @compilationMode(infer) | ||
import { useEffect, useMemo, useState } from "react"; | ||
import { ValidateMemoization } from "shared-runtime"; | ||
|
||
let pretendConst = 0; | ||
|
||
function unsafeResetConst() { | ||
pretendConst = 0; | ||
} | ||
|
||
function unsafeUpdateConst() { | ||
pretendConst += 1; | ||
} | ||
|
||
function Component() { | ||
useState(() => { | ||
// unsafe: reset the constant when first rendering the instance | ||
unsafeResetConst(); | ||
}); | ||
// UNSAFE! changing a module variable that is read by a component is normally | ||
// unsafe, but in this case we're simulating a fast refresh between each render | ||
unsafeUpdateConst(); | ||
|
||
// In production mode (no @enableResetCacheOnSourceFileChanges) memo caches are not | ||
// reset unless the deps change | ||
const value = useMemo(() => [{ pretendConst }], []); | ||
|
||
return <ValidateMemoization inputs={[]} output={value} />; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{}], | ||
sequentialRenders: [{}, {}], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; // @compilationMode(infer) | ||
import { useEffect, useMemo, useState } from "react"; | ||
import { ValidateMemoization } from "shared-runtime"; | ||
|
||
let pretendConst = 0; | ||
|
||
function unsafeResetConst() { | ||
pretendConst = 0; | ||
} | ||
|
||
function unsafeUpdateConst() { | ||
pretendConst += 1; | ||
} | ||
|
||
function Component() { | ||
const $ = _c(3); | ||
let t0; | ||
if ($[0] === Symbol.for("react.memo_cache_sentinel")) { | ||
t0 = () => { | ||
unsafeResetConst(); | ||
}; | ||
$[0] = t0; | ||
} else { | ||
t0 = $[0]; | ||
} | ||
useState(t0); | ||
|
||
unsafeUpdateConst(); | ||
let t1; | ||
let t2; | ||
if ($[1] === Symbol.for("react.memo_cache_sentinel")) { | ||
t2 = [{ pretendConst }]; | ||
$[1] = t2; | ||
} else { | ||
t2 = $[1]; | ||
} | ||
t1 = t2; | ||
const value = t1; | ||
let t3; | ||
if ($[2] === Symbol.for("react.memo_cache_sentinel")) { | ||
t3 = <ValidateMemoization inputs={[]} output={value} />; | ||
$[2] = t3; | ||
} else { | ||
t3 = $[2]; | ||
} | ||
return t3; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{}], | ||
sequentialRenders: [{}, {}], | ||
}; | ||
|
||
``` | ||
### Eval output | ||
(kind: ok) <div>{"inputs":[],"output":[{"pretendConst":1}]}</div> | ||
<div>{"inputs":[],"output":[{"pretendConst":1}]}</div> |
35 changes: 35 additions & 0 deletions
35
...-compiler/src/__tests__/fixtures/compiler/fast-refresh-dont-refresh-const-changes-prod.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// @compilationMode(infer) | ||
import { useEffect, useMemo, useState } from "react"; | ||
import { ValidateMemoization } from "shared-runtime"; | ||
|
||
let pretendConst = 0; | ||
|
||
function unsafeResetConst() { | ||
pretendConst = 0; | ||
} | ||
|
||
function unsafeUpdateConst() { | ||
pretendConst += 1; | ||
} | ||
|
||
function Component() { | ||
useState(() => { | ||
// unsafe: reset the constant when first rendering the instance | ||
unsafeResetConst(); | ||
}); | ||
// UNSAFE! changing a module variable that is read by a component is normally | ||
// unsafe, but in this case we're simulating a fast refresh between each render | ||
unsafeUpdateConst(); | ||
|
||
// In production mode (no @enableResetCacheOnSourceFileChanges) memo caches are not | ||
// reset unless the deps change | ||
const value = useMemo(() => [{ pretendConst }], []); | ||
|
||
return <ValidateMemoization inputs={[]} output={value} />; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{}], | ||
sequentialRenders: [{}, {}], | ||
}; |
112 changes: 112 additions & 0 deletions
112
...__tests__/fixtures/compiler/fast-refresh-refresh-on-const-changes-dev.expect.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @compilationMode(infer) @enableResetCacheOnSourceFileChanges | ||
import { useEffect, useMemo, useState } from "react"; | ||
import { ValidateMemoization } from "shared-runtime"; | ||
|
||
let pretendConst = 0; | ||
|
||
function unsafeResetConst() { | ||
pretendConst = 0; | ||
} | ||
|
||
function unsafeUpdateConst() { | ||
pretendConst += 1; | ||
} | ||
|
||
function Component() { | ||
useState(() => { | ||
// unsafe: reset the constant when first rendering the instance | ||
unsafeResetConst(); | ||
}); | ||
// UNSAFE! changing a module variable that is read by a component is normally | ||
// unsafe, but in this case we're simulating a fast refresh between each render | ||
unsafeUpdateConst(); | ||
|
||
// TODO: In fast refresh mode (@enableResetCacheOnSourceFileChanges) Forget should | ||
// reset on changes to globals that impact the component/hook, effectively memoizing | ||
// as if value was reactive. However, we don't want to actually treat globals as | ||
// reactive (though that would be trivial) since it could change compilation too much | ||
// btw dev and prod. Instead, we should reset the cache via a secondary mechanism. | ||
const value = useMemo(() => [{ pretendConst }], [pretendConst]); | ||
|
||
return <ValidateMemoization inputs={[pretendConst]} output={value} />; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{}], | ||
sequentialRenders: [{}, {}], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; // @compilationMode(infer) @enableResetCacheOnSourceFileChanges | ||
import { useEffect, useMemo, useState } from "react"; | ||
import { ValidateMemoization } from "shared-runtime"; | ||
|
||
let pretendConst = 0; | ||
|
||
function unsafeResetConst() { | ||
pretendConst = 0; | ||
} | ||
|
||
function unsafeUpdateConst() { | ||
pretendConst += 1; | ||
} | ||
|
||
function Component() { | ||
const $ = _c(4); | ||
if ( | ||
$[0] !== "4bf230b116dd95f382060ad17350e116395e41ed757e51fd074ea0b4ed281272" | ||
) { | ||
for (let $i = 0; $i < 4; $i += 1) { | ||
$[$i] = Symbol.for("react.memo_cache_sentinel"); | ||
} | ||
$[0] = "4bf230b116dd95f382060ad17350e116395e41ed757e51fd074ea0b4ed281272"; | ||
} | ||
let t0; | ||
if ($[1] === Symbol.for("react.memo_cache_sentinel")) { | ||
t0 = () => { | ||
unsafeResetConst(); | ||
}; | ||
$[1] = t0; | ||
} else { | ||
t0 = $[1]; | ||
} | ||
useState(t0); | ||
|
||
unsafeUpdateConst(); | ||
let t1; | ||
let t2; | ||
if ($[2] === Symbol.for("react.memo_cache_sentinel")) { | ||
t2 = [{ pretendConst }]; | ||
$[2] = t2; | ||
} else { | ||
t2 = $[2]; | ||
} | ||
t1 = t2; | ||
const value = t1; | ||
let t3; | ||
if ($[3] === Symbol.for("react.memo_cache_sentinel")) { | ||
t3 = <ValidateMemoization inputs={[pretendConst]} output={value} />; | ||
$[3] = t3; | ||
} else { | ||
t3 = $[3]; | ||
} | ||
return t3; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{}], | ||
sequentialRenders: [{}, {}], | ||
}; | ||
|
||
``` | ||
38 changes: 38 additions & 0 deletions
38
...act-compiler/src/__tests__/fixtures/compiler/fast-refresh-refresh-on-const-changes-dev.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// @compilationMode(infer) @enableResetCacheOnSourceFileChanges | ||
import { useEffect, useMemo, useState } from "react"; | ||
import { ValidateMemoization } from "shared-runtime"; | ||
|
||
let pretendConst = 0; | ||
|
||
function unsafeResetConst() { | ||
pretendConst = 0; | ||
} | ||
|
||
function unsafeUpdateConst() { | ||
pretendConst += 1; | ||
} | ||
|
||
function Component() { | ||
useState(() => { | ||
// unsafe: reset the constant when first rendering the instance | ||
unsafeResetConst(); | ||
}); | ||
// UNSAFE! changing a module variable that is read by a component is normally | ||
// unsafe, but in this case we're simulating a fast refresh between each render | ||
unsafeUpdateConst(); | ||
|
||
// TODO: In fast refresh mode (@enableResetCacheOnSourceFileChanges) Forget should | ||
// reset on changes to globals that impact the component/hook, effectively memoizing | ||
// as if value was reactive. However, we don't want to actually treat globals as | ||
// reactive (though that would be trivial) since it could change compilation too much | ||
// btw dev and prod. Instead, we should reset the cache via a secondary mechanism. | ||
const value = useMemo(() => [{ pretendConst }], [pretendConst]); | ||
|
||
return <ValidateMemoization inputs={[pretendConst]} output={value} />; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{}], | ||
sequentialRenders: [{}, {}], | ||
}; |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters