From 8e006bb8a3066021a297a11b605e3e1022416b52 Mon Sep 17 00:00:00 2001 From: sno Date: Tue, 2 Dec 2025 12:43:20 +0900 Subject: [PATCH] [bugfix] Add vite-define shim for Playwright i18n collection (#6906) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary Fixes the `ReferenceError: __DISTRIBUTION__ is not defined` error when running i18n collection tests. ## Problem PR #6879 added conditional menu commands based on distribution (hiding memory unload commands in cloud). This introduced a dependency on `isCloud` which uses the `__DISTRIBUTION__` Vite define variable in `coreMenuCommands.ts`. When Playwright's test runner imports this file during i18n collection, it fails because Vite define variables are only replaced during Vite's build/dev process, not during Playwright's TypeScript compilation. ## Solution Created a simple shim (`scripts/vite-define-shim.ts`) that: 1. Defines all Vite define variables as global constants with default values 2. Provides a minimal `window` shim for Node environment 3. Is imported at the top of `collect-i18n-general.ts` before any code that uses these variables This approach is simpler than: - Creating a custom Babel plugin (attempted in this PR, see commit history) - Using `ctViteConfig` (only works for component testing, not regular Playwright tests) - Post-build regex replacement (fragile and error-prone) ## Test Plan Run `pnpm collect-i18n` and verify: - ✅ No more `ReferenceError: __DISTRIBUTION__ is not defined` - ✅ No more `ReferenceError: window is not defined` - ⏱️ Tests may timeout if dev server is not running on port 5173, but that's a separate issue ## Related - Fixes issue introduced by PR #6879 - Related to Notion task: https://www.notion.so/comfy-org/Implement-Babel-plugin-for-Vite-define-replacements-in-Playwright-2b56d73d365081d5bb63e02712912b17 🤖 Generated with [Claude Code](https://claude.com/claude-code) ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-6906-bugfix-Add-vite-define-shim-for-Playwright-i18n-collection-2b66d73d36508182b4d6d69b88ae2771) by [Unito](https://www.unito.io) --------- Co-authored-by: Claude Co-authored-by: github-actions Co-authored-by: Alexander Brown Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- scripts/collect-i18n-general.ts | 3 +++ scripts/vite-define-shim.ts | 46 +++++++++++++++++++++++++++++++++ src/locales/en/main.json | 2 +- 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 scripts/vite-define-shim.ts diff --git a/scripts/collect-i18n-general.ts b/scripts/collect-i18n-general.ts index 1bbcb2060..5165e89be 100644 --- a/scripts/collect-i18n-general.ts +++ b/scripts/collect-i18n-general.ts @@ -1,5 +1,8 @@ import * as fs from 'fs' +// Import Vite define shim to make __DISTRIBUTION__ and other define variables available +import './vite-define-shim' + import { DESKTOP_DIALOGS } from '../apps/desktop-ui/src/constants/desktopDialogs' import { comfyPageFixture as test } from '../browser_tests/fixtures/ComfyPage' import { diff --git a/scripts/vite-define-shim.ts b/scripts/vite-define-shim.ts new file mode 100644 index 000000000..e12b126f3 --- /dev/null +++ b/scripts/vite-define-shim.ts @@ -0,0 +1,46 @@ +/** + * Shim for Vite define variables to make them available during Playwright test execution + * This file should be imported before any code that uses Vite define variables + */ + +// Define global constants that Vite would normally replace at build time +declare global { + const __COMFYUI_FRONTEND_VERSION__: string + const __SENTRY_ENABLED__: boolean + const __SENTRY_DSN__: string + const __ALGOLIA_APP_ID__: string + const __ALGOLIA_API_KEY__: string + const __USE_PROD_CONFIG__: boolean + const __DISTRIBUTION__: 'desktop' | 'localhost' | 'cloud' +} + +type GlobalWithDefines = typeof globalThis & { + __COMFYUI_FRONTEND_VERSION__: string + __SENTRY_ENABLED__: boolean + __SENTRY_DSN__: string + __ALGOLIA_APP_ID__: string + __ALGOLIA_API_KEY__: string + __USE_PROD_CONFIG__: boolean + __DISTRIBUTION__: 'desktop' | 'localhost' | 'cloud' + window?: Record +} + +const globalWithDefines = globalThis as GlobalWithDefines + +// Set default values for Playwright test environment +globalWithDefines.__COMFYUI_FRONTEND_VERSION__ = + process.env.npm_package_version || '1.0.0' +globalWithDefines.__SENTRY_ENABLED__ = false +globalWithDefines.__SENTRY_DSN__ = '' +globalWithDefines.__ALGOLIA_APP_ID__ = '' +globalWithDefines.__ALGOLIA_API_KEY__ = '' +globalWithDefines.__USE_PROD_CONFIG__ = false +globalWithDefines.__DISTRIBUTION__ = 'localhost' + +// Provide a minimal window shim for Node environment +// This is needed for code that checks window existence during imports +if (typeof window === 'undefined') { + globalWithDefines.window = {} +} + +export {} diff --git a/src/locales/en/main.json b/src/locales/en/main.json index 1205517f4..1d2e14365 100644 --- a/src/locales/en/main.json +++ b/src/locales/en/main.json @@ -2254,4 +2254,4 @@ "replacementInstruction": "Install these nodes to run this workflow, or replace them with installed alternatives. Missing nodes are highlighted in red on the canvas." } } -} +} \ No newline at end of file