Skip to content

Commit 0fda41a

Browse files
authored
fix(react-query-devtools): only export devtools in development mode (#3861)
1 parent de937e7 commit 0fda41a

File tree

7 files changed

+47
-27
lines changed

7 files changed

+47
-27
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"test:jest:dev": "jest --config ./jest.config.ts --watch",
1616
"test:size": "npm run build && bundlewatch",
1717
"build": "rollup --config rollup.config.js && npm run typecheck",
18+
"postbuild": "cp ./packages/react-query-devtools/src/noop.ts ./packages/react-query-devtools/build/esm/noop.js",
1819
"typecheck": "tsc -b",
1920
"watch": "concurrently --kill-others \"rollup --config rollup.config.js -w\" \"npm run typecheck -- --watch\" \"npm run test\"",
2021
"linkAll": "lerna exec 'npm run link' --parallel",

packages/react-query-devtools/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
"build/*",
1919
"src"
2020
],
21+
"exports": {
22+
"development": {
23+
"default": "./build/esm/index.js"
24+
},
25+
"default": "./build/esm/noop.js"
26+
},
2127
"scripts": {
2228
"test:eslint": "../../node_modules/.bin/eslint --ext .ts,.tsx ./src",
2329
"compile": "../../node_modules/.bin/tsc -p tsconfig.json --noEmit --emitDeclarationOnly false"

packages/react-query-devtools/src/__tests__/devtools.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { fireEvent, screen, waitFor, act } from '@testing-library/react'
33
import { ErrorBoundary } from 'react-error-boundary'
44
import '@testing-library/jest-dom'
55
import { useQuery, QueryClient } from '@tanstack/react-query'
6-
import { sortFns } from '../devtools'
6+
import { sortFns } from '../utils'
77
import {
88
getByTextContent,
99
renderWithClient,

packages/react-query-devtools/src/__tests__/utils.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { MatcherFunction } from '@testing-library/dom/types/matches'
22
import { render } from '@testing-library/react'
33
import * as React from 'react'
4-
import { ReactQueryDevtools } from '..'
4+
import { ReactQueryDevtools } from '../devtools'
55

66
import {
77
QueryClient,

packages/react-query-devtools/src/devtools.tsx

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as React from 'react'
22
import { useSyncExternalStore } from 'use-sync-external-store/shim'
33
import {
4-
Query,
54
useQueryClient,
65
onlineManager,
76
notifyManager,
@@ -12,7 +11,7 @@ import {
1211
} from '@tanstack/react-query'
1312
import { rankItem, compareItems } from '@tanstack/match-sorter-utils'
1413
import useLocalStorage from './useLocalStorage'
15-
import { useIsMounted } from './utils'
14+
import { sortFns, useIsMounted } from './utils'
1615

1716
import {
1817
Panel,
@@ -29,7 +28,7 @@ import { getQueryStatusLabel, getQueryStatusColor } from './utils'
2928
import Explorer from './Explorer'
3029
import Logo from './Logo'
3130

32-
interface DevtoolsOptions extends ContextOptions {
31+
export interface DevtoolsOptions extends ContextOptions {
3332
/**
3433
* Set this true if you want the dev tools to default to being open
3534
*/
@@ -375,27 +374,6 @@ export function ReactQueryDevtools({
375374
)
376375
}
377376

378-
const getStatusRank = (q: Query) =>
379-
q.state.fetchStatus !== 'idle'
380-
? 0
381-
: !q.getObserversCount()
382-
? 3
383-
: q.isStale()
384-
? 2
385-
: 1
386-
387-
export const sortFns: Record<string, (a: Query, b: Query) => number> = {
388-
'Status > Last Updated': (a, b) =>
389-
getStatusRank(a) === getStatusRank(b)
390-
? (sortFns['Last Updated']?.(a, b) as number)
391-
: getStatusRank(a) > getStatusRank(b)
392-
? 1
393-
: -1,
394-
'Query Hash': (a, b) => (a.queryHash > b.queryHash ? 1 : -1),
395-
'Last Updated': (a, b) =>
396-
a.state.dataUpdatedAt < b.state.dataUpdatedAt ? 1 : -1,
397-
}
398-
399377
const useSubscribeToQueryCache = <T,>(
400378
queryCache: QueryCache,
401379
getSnapshot: () => T,
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
1-
export * from './devtools'
1+
import * as devtools from './devtools'
2+
3+
export const ReactQueryDevtools: typeof devtools['ReactQueryDevtools'] =
4+
process.env.NODE_ENV !== 'development'
5+
? function () {
6+
return null
7+
}
8+
: devtools.ReactQueryDevtools
9+
10+
export const ReactQueryDevtoolsPanel: typeof devtools['ReactQueryDevtoolsPanel'] =
11+
process.env.NODE_ENV !== 'development'
12+
? (function () {
13+
return null
14+
} as any)
15+
: devtools.ReactQueryDevtoolsPanel

packages/react-query-devtools/src/utils.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,24 @@ export const displayValue = (value: unknown) => {
124124

125125
return JSON.stringify(newValue, name)
126126
}
127+
128+
const getStatusRank = (q: Query) =>
129+
q.state.fetchStatus !== 'idle'
130+
? 0
131+
: !q.getObserversCount()
132+
? 3
133+
: q.isStale()
134+
? 2
135+
: 1
136+
137+
export const sortFns: Record<string, (a: Query, b: Query) => number> = {
138+
'Status > Last Updated': (a, b) =>
139+
getStatusRank(a) === getStatusRank(b)
140+
? (sortFns['Last Updated']?.(a, b) as number)
141+
: getStatusRank(a) > getStatusRank(b)
142+
? 1
143+
: -1,
144+
'Query Hash': (a, b) => (a.queryHash > b.queryHash ? 1 : -1),
145+
'Last Updated': (a, b) =>
146+
a.state.dataUpdatedAt < b.state.dataUpdatedAt ? 1 : -1,
147+
}

0 commit comments

Comments
 (0)