Skip to content

Commit adcf45c

Browse files
fix: make the import meta conditional to work outside of vite (#305)
* fix: make the import meta conditional to work outside of vite * ci: apply automated fixes * changeset * fix: unmounting of devtools * fix: unmounting of devtools * ci: apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 6457d8a commit adcf45c

File tree

7 files changed

+38
-12
lines changed

7 files changed

+38
-12
lines changed

.changeset/fine-pugs-press.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/devtools-utils': patch
3+
---
4+
5+
fix issue with unmounting of devtools

.changeset/gold-beers-shine.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/devtools': patch
3+
---
4+
5+
fix issue with popup window

packages/devtools-utils/src/react/panel.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,16 @@ export function createReactPanel<
3232
const devToolRef = useRef<HTMLDivElement>(null)
3333
const devtools = useRef<TCoreDevtoolsClass | null>(null)
3434
useEffect(() => {
35-
if (devtools.current) return
36-
37-
devtools.current = new CoreClass()
35+
const instance = new CoreClass()
36+
devtools.current = instance
3837

3938
if (devToolRef.current) {
40-
devtools.current.mount(devToolRef.current, props?.theme ?? 'dark')
39+
instance.mount(devToolRef.current, props?.theme ?? 'dark')
4140
}
4241

4342
return () => {
44-
if (devToolRef.current) {
45-
devtools.current?.unmount()
46-
}
43+
instance.unmount()
44+
devtools.current = null
4745
}
4846
}, [props?.theme])
4947

packages/devtools-utils/src/solid/class.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ import type { JSX } from 'solid-js'
1414
export function constructCoreClass(Component: () => JSX.Element) {
1515
class DevtoolsCore {
1616
#isMounted = false
17+
#isMounting = false
18+
#mountCb: (() => void) | null = null
1719
#dispose?: () => void
1820
#Component: any
1921
#ThemeProvider: any
2022

2123
constructor() {}
2224

2325
async mount<T extends HTMLElement>(el: T, theme: 'light' | 'dark') {
26+
this.#isMounting = true
2427
const { lazy } = await import('solid-js')
2528
const { render, Portal } = await import('solid-js/web')
2629
if (this.#isMounted) {
@@ -49,13 +52,25 @@ export function constructCoreClass(Component: () => JSX.Element) {
4952
)
5053
}, mountTo)
5154
this.#isMounted = true
55+
this.#isMounting = false
5256
this.#dispose = dispose
57+
if (this.#mountCb) {
58+
this.#mountCb()
59+
this.#mountCb = null
60+
}
5361
}
5462

5563
unmount() {
56-
if (!this.#isMounted) {
64+
if (!this.#isMounted && !this.#isMounting) {
5765
throw new Error('Devtools is not mounted')
5866
}
67+
if (this.#isMounting) {
68+
this.#mountCb = () => {
69+
this.#dispose?.()
70+
this.#isMounted = false
71+
}
72+
return
73+
}
5974
this.#dispose?.()
6075
this.#isMounted = false
6176
}

packages/devtools/src/components/source-inspector.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export const SourceInspector = () => {
8080
e.stopPropagation()
8181

8282
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
83-
const baseUrl = new URL(import.meta.env?.BASE_URL ?? '/', location.origin)
83+
const baseUrl = new URL(import.meta?.env?.BASE_URL ?? '/', location.origin)
8484
const url = new URL(
8585
`__tsd/open-source?source=${encodeURIComponent(
8686
highlightState.dataSource,

packages/devtools/src/context/pip-context.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ export const PiPProvider = (props: PiPProviderProps) => {
5454
'Failed to open popup. Please allow popups for this site to view the devtools in picture-in-picture mode.',
5555
)
5656
}
57-
58-
import.meta.hot?.on('vite:beforeUpdate', () => {
57+
// can be run outside of vite so we ignore the rule
58+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
59+
import.meta?.hot?.on('vite:beforeUpdate', () => {
5960
localStorage.setItem('pip_open', 'false')
6061
closePipWindow()
6162
})

packages/devtools/src/core.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ export class TanStackDevtoolsCore {
6363

6464
mount<T extends HTMLElement>(el: T) {
6565
// tsup-preset-solid statically replaces this variable during build, which eliminates this code from server bundle
66-
if (import.meta.env.SSR) return
66+
// can be run outside of vite so we ignore the rule
67+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
68+
if (import.meta?.env?.SSR) return
6769

6870
if (this.#isMounted) {
6971
throw new Error('Devtools is already mounted')

0 commit comments

Comments
 (0)