Skip to content

Commit 366cb0d

Browse files
authored
cleanup: remove old code/files, improve treeshaking (#309)
* Remove dead code * dedupe window checks * Update helpers.ts * change installation * Create .npmrc
1 parent b6de875 commit 366cb0d

File tree

27 files changed

+957
-1510
lines changed

27 files changed

+957
-1510
lines changed

.npmrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
prefer-workspace-packages=true
2+
link-workspace-packages=true

packages/scan/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@
228228
"@preact/signals": "^1.3.1",
229229
"@rollup/pluginutils": "^5.1.3",
230230
"@types/node": "^20.17.9",
231-
"bippy": "0.2.7",
231+
"bippy": "^0.3.7",
232232
"esbuild": "^0.24.0",
233233
"estree-walker": "^3.0.3",
234234
"kleur": "^4.1.5",

packages/scan/src/auto.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { scan } from './index';
2-
import { init } from './install-hook';
1+
// Prioritize bippy side-effect
2+
import 'bippy';
33

4-
init();
4+
import { IS_CLIENT } from '~web/utils/constants';
5+
import { scan } from './index';
56

6-
if (typeof window !== 'undefined') {
7+
if (IS_CLIENT) {
78
scan();
89
window.reactScan = scan;
910
}

packages/scan/src/core/index.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import {
99
import type { ComponentType } from 'preact';
1010
import type { ReactNode } from 'preact/compat';
1111
import type { RenderData } from 'src/core/utils';
12-
// import { initReactScanOverlay } from '~web/overlay';
1312
import { initReactScanInstrumentation } from 'src/new-outlines';
1413
import styles from '~web/assets/css/styles.css';
1514
import { createToolbar } from '~web/toolbar';
15+
import { IS_CLIENT } from '~web/utils/constants';
1616
import { readLocalStorage, saveLocalStorage } from '~web/utils/helpers';
1717
import type { Outline } from '~web/utils/outline';
1818
import type { States } from '~web/views/inspector/utils';
@@ -272,9 +272,7 @@ export type ChangesListener = (changes: ChangesPayload) => void;
272272

273273
export const Store: StoreType = {
274274
wasDetailsOpen: signal(true),
275-
isInIframe: signal(
276-
typeof window !== 'undefined' && window.self !== window.top,
277-
),
275+
isInIframe: signal(IS_CLIENT && window.self !== window.top),
278276
inspectState: signal<States>({
279277
kind: 'uninitialized',
280278
}),
@@ -483,7 +481,7 @@ export const getIsProduction = () => {
483481

484482
export const start = () => {
485483
try {
486-
if (typeof window === 'undefined') {
484+
if (!IS_CLIENT) {
487485
return;
488486
}
489487

@@ -514,7 +512,7 @@ export const start = () => {
514512
initToolbar(!!options.value.showToolbar);
515513
});
516514

517-
const isUsedInBrowserExtension = typeof window !== 'undefined';
515+
const isUsedInBrowserExtension = IS_CLIENT;
518516
if (!Store.monitor.value && !isUsedInBrowserExtension) {
519517
setTimeout(() => {
520518
if (isInstrumentationActive()) return;

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,22 @@
1313
* From empirical ad-hoc testing, this breaks in certain scenarios.
1414
*/
1515
import * as React from 'react';
16+
import { IS_CLIENT } from '~web/utils/constants';
1617

1718
/**
1819
* useRef will be undefined in "use server"
1920
*
2021
* @see https://nextjs.org/docs/messages/react-client-hook-in-server-component
2122
*/
22-
export const isRSC = () => !React.useRef;
23-
export const isSSR = () => typeof window === 'undefined' || isRSC();
23+
const isRSC = () => !React.useRef;
24+
export const isSSR = () => !IS_CLIENT || isRSC();
2425

2526
interface WindowWithCypress extends Window {
2627
Cypress?: unknown;
2728
}
2829

2930
export const isTest =
30-
(typeof window !== 'undefined' &&
31+
(IS_CLIENT &&
3132
/**
3233
* @see https://docs.cypress.io/faq/questions/using-cypress-faq#Is-there-any-way-to-detect-if-my-app-is-running-under-Cypress
3334
*/

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
isCompositeFiber,
77
} from 'bippy';
88
import { type FC, useEffect } from 'react';
9+
import { IS_CLIENT } from '~web/utils/constants';
910
import {
1011
type MonitoringOptions,
1112
ReactScanInternals,
@@ -76,7 +77,7 @@ export const Monitoring: FC<MonitoringProps> = ({
7677
// When using Monitoring without framework, we need to compute the route from the path and params
7778
if (!route && path && params) {
7879
Store.monitor.value.route = computeRoute(path, params);
79-
} else if (typeof window !== 'undefined') {
80+
} else if (IS_CLIENT) {
8081
Store.monitor.value.route =
8182
route ?? path ?? new URL(window.location.toString()).pathname; // this is inaccurate on vanilla react if the path is not provided but used for session route
8283
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { IS_CLIENT } from '~web/utils/constants';
12
import { onIdle } from '~web/utils/helpers';
23
import { isSSR } from './constants';
34
import { Device, type Session } from './types';
@@ -132,10 +133,8 @@ export const getSession = async ({
132133
return session;
133134
};
134135

135-
136-
137136
export const not_globally_unique_generateId = () => {
138-
if (typeof window === 'undefined') {
137+
if (!IS_CLIENT) {
139138
return '0';
140139
}
141140

@@ -146,4 +145,4 @@ export const not_globally_unique_generateId = () => {
146145
}
147146
// @ts-expect-error
148147
return `${++window.reactScanIdCounter}`;
149-
};
148+
};

packages/scan/src/core/worker/deferred.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

packages/scan/src/core/worker/smol-extension.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

packages/scan/src/core/worker/smol.ts

Lines changed: 0 additions & 102 deletions
This file was deleted.

0 commit comments

Comments
 (0)