Skip to content

Commit 4560e31

Browse files
lxsmnsycRobPruzan
andauthored
fix: playwright and react 18 compat (#238)
* fix react 18 compat * Update package.json * Update cli.mts * Update index.ts * Update index.ts * Update pnpm-lock.yaml * fix: hack to autorefresh playwright after script is ran --------- Co-authored-by: RobPruzan <[email protected]>
1 parent 931a01f commit 4560e31

File tree

7 files changed

+38
-32
lines changed

7 files changed

+38
-32
lines changed

packages/kitchen-sink/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"bippy": "^0.0.21",
1313
"react": "19.0.0",
1414
"react-dom": "19.0.0",
15-
"react-scan": "^0.0.34",
15+
"react-scan": "^0.1.3",
1616
"sugar-high": "^0.7.5"
1717
},
1818
"devDependencies": {

packages/scan/src/cli.mts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { spawn } from 'node:child_process';
2-
import fs from 'node:fs';
3-
import path from 'node:path';
41
import { cancel, confirm, intro, isCancel, spinner } from '@clack/prompts';
52
import { bgMagenta, dim, red } from 'kleur';
63
import mri from 'mri';
4+
import { spawn } from 'node:child_process';
5+
import fs from 'node:fs/promises';
6+
import path from 'node:path';
77
import {
88
type Browser,
99
type BrowserContext,
@@ -227,7 +227,7 @@ const init = async () => {
227227

228228
const page = await context.newPage();
229229

230-
const scriptContent = fs.readFileSync(
230+
const scriptContent = await fs.readFile(
231231
path.resolve(__dirname, './auto.global.js'),
232232
'utf8',
233233
);
@@ -237,12 +237,12 @@ const init = async () => {
237237
const urlString = inferValidURL(inputUrl);
238238

239239
await page.goto(urlString);
240-
await page.waitForLoadState('load');
241-
await page.waitForTimeout(500);
242240

243-
await page.addScriptTag({
241+
await page.addInitScript({
244242
content: `${scriptContent}\n//# sourceURL=react-scan.js`,
245243
});
244+
await page.waitForLoadState('load');
245+
await page.waitForTimeout(500);
246246

247247
const pollReport = async () => {
248248
if (page.url() !== currentURL) return;
@@ -296,9 +296,12 @@ const init = async () => {
296296

297297
await page.waitForTimeout(100);
298298

299+
// TODO: determine why this is needed and fix root cause
300+
await page.reload();
301+
299302
await page.evaluate(() => {
300303
if (typeof globalThis.reactScan !== 'function') return;
301-
globalThis.reactScan({ report: true });
304+
globalThis.reactScan({});
302305
});
303306

304307
interval = setInterval(() => {

packages/scan/src/core/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ const initRootContainer = (): RootContainer => {
5656
'image/svg+xml',
5757
).documentElement;
5858

59-
6059
fragment.appendChild(iconSprite);
6160
fragment.appendChild(cssStyles);
6261
shadowRoot.appendChild(fragment);
@@ -497,8 +496,9 @@ export const start = () => {
497496

498497
const options = getOptions();
499498

500-
idempotent_createToolbar(!!options.value.showToolbar);
501-
initReactScanInstrumentation();
499+
initReactScanInstrumentation(() =>
500+
idempotent_createToolbar(!!options.value.showToolbar),
501+
);
502502

503503
const isUsedInBrowserExtension = typeof window !== 'undefined';
504504
if (!Store.monitor.value && !isUsedInBrowserExtension) {

packages/scan/src/core/instrumentation.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ interface InstrumentationConfig {
338338
onCommitFinish: OnCommitFinishHandler;
339339
onError: OnErrorHandler;
340340
onActive?: OnActiveHandler;
341+
onPostCommitFiberRoot: () => void;
341342
// monitoring does not need to track changes, and it adds overhead to leave it on
342343
trackChanges: boolean;
343344
// allows monitoring to continue tracking renders even if react scan dev mode is disabled
@@ -611,6 +612,12 @@ export const createInstrumentation = (
611612
instance.config.onCommitFinish();
612613
}
613614
},
615+
onPostCommitFiberRoot() {
616+
const allInstances = getAllInstances();
617+
for (const instance of allInstances) {
618+
instance.config.onPostCommitFiberRoot();
619+
}
620+
},
614621
});
615622
}
616623
return instrumentation;

packages/scan/src/core/monitor/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ export const startMonitoring = () => {
135135
onCommitFinish() {
136136
// ReactScanInternals.options.value.onCommitFinish?.();
137137
},
138+
onPostCommitFiberRoot() {
139+
// ...
140+
},
138141
trackChanges: false,
139142
forceAlwaysTrackRenders: true,
140143
});

packages/scan/src/new-outlines/index.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,9 @@ export const getCanvasEl = () => {
289289

290290
if (IS_OFFSCREEN_CANVAS_WORKER_SUPPORTED) {
291291
try {
292-
const useExtensionWorker = readLocalStorage<boolean>('use-extension-worker');
292+
const useExtensionWorker = readLocalStorage<boolean>(
293+
'use-extension-worker',
294+
);
293295
removeLocalStorage('use-extension-worker');
294296

295297
if (useExtensionWorker) {
@@ -428,7 +430,7 @@ export const isValidFiber = (fiber: Fiber) => {
428430

429431
return true;
430432
};
431-
export const initReactScanInstrumentation = () => {
433+
export const initReactScanInstrumentation = (setupToolbar: () => void) => {
432434
if (hasStopped()) return;
433435
// todo: don't hardcode string getting weird ref error in iife when using process.env
434436
const instrumentation = createInstrumentation('react-scan-devtools-0.1.0', {
@@ -438,10 +440,6 @@ export const initReactScanInstrumentation = () => {
438440
onActive: () => {
439441
if (hasStopped()) return;
440442

441-
const host = getCanvasEl();
442-
if (host) {
443-
document.documentElement.appendChild(host);
444-
}
445443
globalThis.__REACT_SCAN__ = {
446444
ReactScanInternals,
447445
};
@@ -480,6 +478,13 @@ export const initReactScanInstrumentation = () => {
480478
onCommitFinish: () => {
481479
ReactScanInternals.options.value.onCommitFinish?.();
482480
},
481+
onPostCommitFiberRoot() {
482+
const host = getCanvasEl();
483+
if (host) {
484+
document.documentElement.appendChild(host);
485+
}
486+
setupToolbar();
487+
},
483488
trackChanges: false,
484489
});
485490
ReactScanInternals.instrumentation = instrumentation;

pnpm-lock.yaml

Lines changed: 2 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)