refactor: replace runtime isElectron() with build-time isDesktop constant (#8710)

## Summary

Replace all runtime `isElectron()` function calls with the build-time
`isDesktop` constant from `@/platform/distribution/types`, enabling
dead-code elimination in non-desktop builds.

## Changes

- **What**: Migrate 30 files from runtime `isElectron()` detection
(checking `window.electronAPI`) to the compile-time `isDesktop` constant
(driven by `__DISTRIBUTION__` Vite define). Remove `isElectron` from
`envUtil.ts`. Update `isNativeWindow()` to use `isDesktop`. Guard
`electronAPI()` calls behind `isDesktop` checks in stores. Update 7 test
files to use `vi.hoisted` + getter mock pattern for per-test `isDesktop`
toggling. Add `DISTRIBUTION=desktop` to `dev:electron` script.

## Review Focus

- The `electronDownloadStore.ts` now guards the top-level
`electronAPI()` call behind `isDesktop` to prevent crashes on
non-desktop builds.
- Test mocking pattern uses `vi.hoisted` with a getter to allow per-test
toggling of the `isDesktop` value.
- Pre-existing issues not addressed: `as ElectronAPI` cast in
`envUtil.ts`, `:class="[]"` in `BaseViewTemplate.vue`,
`@ts-expect-error` in `ModelLibrarySidebarTab.vue`.
- This subsumes PR #8627 and renders PR #6122 and PR #7374 obsolete.

┆Issue is synchronized with this [Notion
page](https://www.notion.so/PR-8710-refactor-replace-runtime-isElectron-with-build-time-isDesktop-constant-3006d73d365081c08037f0e61c2f6c77)
by [Unito](https://www.unito.io)
This commit is contained in:
Christian Byrne
2026-02-07 19:47:05 -08:00
committed by GitHub
parent b7fef1c744
commit 6c8473e4e4
30 changed files with 165 additions and 142 deletions

View File

@@ -2,9 +2,9 @@ import { defineStore } from 'pinia'
import { computed } from 'vue'
import { useExternalLink } from '@/composables/useExternalLink'
import { isCloud } from '@/platform/distribution/types'
import { isCloud, isDesktop } from '@/platform/distribution/types'
import type { AboutPageBadge } from '@/types/comfy'
import { electronAPI, isElectron } from '@/utils/envUtil'
import { electronAPI } from '@/utils/envUtil'
import { formatCommitHash } from '@/utils/formatUtil'
import { useExtensionStore } from './extensionStore'
@@ -24,7 +24,7 @@ export const useAboutPanelStore = defineStore('aboutPanel', () => {
// so the python server's API doesn't have the version info.
{
label: `ComfyUI ${
isElectron()
isDesktop
? 'v' + electronAPI().getComfyUIVersion()
: formatCommitHash(coreVersion.value)
}`,

View File

@@ -3,7 +3,8 @@ import type { DownloadState } from '@comfyorg/comfyui-electron-types'
import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
import { electronAPI, isElectron } from '@/utils/envUtil'
import { isDesktop } from '@/platform/distribution/types'
import { electronAPI } from '@/utils/envUtil'
export interface ElectronDownload extends Pick<
DownloadState,
@@ -17,35 +18,34 @@ export interface ElectronDownload extends Pick<
/** Electron downloads store handler */
export const useElectronDownloadStore = defineStore('downloads', () => {
const downloads = ref<ElectronDownload[]>([])
const { DownloadManager } = electronAPI()
const DownloadManager = isDesktop ? electronAPI().DownloadManager : undefined
const findByUrl = (url: string) =>
downloads.value.find((download) => url === download.url)
const initialize = async () => {
if (isElectron()) {
const allDownloads = await DownloadManager.getAllDownloads()
if (!isDesktop || !DownloadManager) return
for (const download of allDownloads) {
downloads.value.push(download)
const allDownloads = await DownloadManager.getAllDownloads()
for (const download of allDownloads) {
downloads.value.push(download)
}
DownloadManager.onDownloadProgress((data) => {
if (!findByUrl(data.url)) {
downloads.value.push(data)
}
// ToDO: replace with ElectronDownload type
DownloadManager.onDownloadProgress((data) => {
if (!findByUrl(data.url)) {
downloads.value.push(data)
}
const download = findByUrl(data.url)
const download = findByUrl(data.url)
if (download) {
download.progress = data.progress
download.status = data.status
download.filename = data.filename
download.savePath = data.savePath
}
})
}
if (download) {
download.progress = data.progress
download.status = data.status
download.filename = data.filename
download.savePath = data.savePath
}
})
}
void initialize()
@@ -58,10 +58,10 @@ export const useElectronDownloadStore = defineStore('downloads', () => {
url: string
savePath: string
filename: string
}) => DownloadManager.startDownload(url, savePath, filename)
const pause = (url: string) => DownloadManager.pauseDownload(url)
const resume = (url: string) => DownloadManager.resumeDownload(url)
const cancel = (url: string) => DownloadManager.cancelDownload(url)
}) => DownloadManager!.startDownload(url, savePath, filename)
const pause = (url: string) => DownloadManager!.pauseDownload(url)
const resume = (url: string) => DownloadManager!.resumeDownload(url)
const cancel = (url: string) => DownloadManager!.cancelDownload(url)
return {
downloads,

View File

@@ -5,7 +5,8 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'
import type { SystemStats } from '@/schemas/apiSchema'
import { api } from '@/scripts/api'
import { useSystemStatsStore } from '@/stores/systemStatsStore'
import { isElectron } from '@/utils/envUtil'
const mockData = vi.hoisted(() => ({ isDesktop: false }))
// Mock the API
vi.mock('@/scripts/api', () => ({
@@ -14,13 +15,13 @@ vi.mock('@/scripts/api', () => ({
}
}))
// Mock the envUtil
vi.mock('@/utils/envUtil', () => ({
isElectron: vi.fn()
vi.mock('@/platform/distribution/types', () => ({
get isDesktop() {
return mockData.isDesktop
},
isCloud: false
}))
vi.mock('@/platform/distribution/types', () => ({ isCloud: false }))
describe('useSystemStatsStore', () => {
let store: ReturnType<typeof useSystemStatsStore>
@@ -161,9 +162,9 @@ describe('useSystemStatsStore', () => {
expect(store.getFormFactor()).toBe('other')
})
describe('desktop environment (Electron)', () => {
describe('desktop environment', () => {
beforeEach(() => {
vi.mocked(isElectron).mockReturnValue(true)
mockData.isDesktop = true
})
it('should return "desktop-windows" for Windows desktop', () => {
@@ -239,9 +240,9 @@ describe('useSystemStatsStore', () => {
})
})
describe('git environment (non-Electron)', () => {
describe('git environment (non-desktop)', () => {
beforeEach(() => {
vi.mocked(isElectron).mockReturnValue(false)
mockData.isDesktop = false
})
it('should return "git-windows" for Windows git', () => {
@@ -319,7 +320,7 @@ describe('useSystemStatsStore', () => {
describe('case insensitive OS detection', () => {
beforeEach(() => {
vi.mocked(isElectron).mockReturnValue(false)
mockData.isDesktop = false
})
it('should handle uppercase OS names', () => {

View File

@@ -1,10 +1,9 @@
import { useAsyncState } from '@vueuse/core'
import { defineStore } from 'pinia'
import { isCloud } from '@/platform/distribution/types'
import { isCloud, isDesktop } from '@/platform/distribution/types'
import type { SystemStats } from '@/schemas/apiSchema'
import { api } from '@/scripts/api'
import { isElectron } from '@/utils/envUtil'
export const useSystemStatsStore = defineStore('systemStats', () => {
const fetchSystemStatsData = async () => {
@@ -40,7 +39,6 @@ export const useSystemStatsStore = defineStore('systemStats', () => {
}
const os = systemStats.value.system.os.toLowerCase()
const isDesktop = isElectron()
if (isDesktop) {
if (os.includes('windows')) {

View File

@@ -48,8 +48,12 @@ vi.mock('@/stores/commandStore', () => ({
})
}))
vi.mock('@/utils/envUtil', () => ({
isElectron: () => false
const mockData = vi.hoisted(() => ({ isDesktop: false }))
vi.mock('@/platform/distribution/types', () => ({
get isDesktop() {
return mockData.isDesktop
}
}))
describe('useBottomPanelStore', () => {

View File

@@ -3,10 +3,10 @@ import { computed, ref } from 'vue'
import { useShortcutsTab } from '@/composables/bottomPanelTabs/useShortcutsTab'
import { isDesktop } from '@/platform/distribution/types'
import { useCommandStore } from '@/stores/commandStore'
import type { ComfyExtension } from '@/types/comfy'
import type { BottomPanelExtension } from '@/types/extensionTypes'
import { isElectron } from '@/utils/envUtil'
type PanelType = 'terminal' | 'shortcuts'
@@ -138,7 +138,7 @@ export const useBottomPanelStore = defineStore('bottomPanel', () => {
const { useLogsTerminalTab, useCommandTerminalTab } =
await import('@/composables/bottomPanelTabs/useTerminalTabs')
registerBottomPanelTab(useLogsTerminalTab())
if (isElectron()) {
if (isDesktop) {
registerBottomPanelTab(useCommandTerminalTab())
}
} catch (error) {