Files
ComfyUI_frontend/scripts/setup-i18n-globals.mjs
snomiao 308213913f Feat: Add Babel plugin for Vite define replacements in Playwright
Implements a solution to handle Vite define replacements during Playwright's
Babel compilation for i18n collection tests. This resolves ReferenceErrors
caused by unprocessed compile-time constants like __DISTRIBUTION__.

Changes:
- Add babel-plugin-vite-define.cjs to replace Vite define constants
- Add babel-plugin-inject-globals.cjs to inject browser globals setup
- Add setup-i18n-globals.mjs for JSDOM-based browser environment
- Update playwright.i18n.config.ts with Babel plugin configuration
- Install babel-plugin-module-resolver for @ alias support

The implementation follows the approach from PR #5515 but is adapted for
the current codebase structure. The Babel plugins run during Playwright's
test compilation to ensure all Vite define constants are replaced with
their actual values before execution.

Fixes #10981

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-25 05:20:51 +00:00

62 lines
1.6 KiB
JavaScript

/**
* Setup browser globals for i18n collection in Node.js environment
*
* This file is imported at the top of i18n collection test files to provide
* browser globals that are referenced in the codebase but not available in Node.js
*/
import { JSDOM } from 'jsdom'
// Create a minimal JSDOM instance
const dom = new JSDOM('<!DOCTYPE html><html><body></body></html>', {
url: 'http://localhost:5173',
pretendToBeVisual: true,
resources: 'usable'
})
// Set up global window and document
global.window = dom.window
global.document = dom.window.document
// Use defineProperty for read-only globals
Object.defineProperty(global, 'navigator', {
value: dom.window.navigator,
writable: true,
configurable: true
})
// Set up other common browser globals
global.HTMLElement = dom.window.HTMLElement
global.Element = dom.window.Element
global.Node = dom.window.Node
global.NodeList = dom.window.NodeList
global.MutationObserver = dom.window.MutationObserver
global.ResizeObserver = dom.window.ResizeObserver || class ResizeObserver {
observe() {}
unobserve() {}
disconnect() {}
}
global.IntersectionObserver = dom.window.IntersectionObserver || class IntersectionObserver {
observe() {}
unobserve() {}
disconnect() {}
}
// Set up basic localStorage and sessionStorage
global.localStorage = {
getItem: () => null,
setItem: () => {},
removeItem: () => {},
clear: () => {}
}
global.sessionStorage = {
getItem: () => null,
setItem: () => {},
removeItem: () => {},
clear: () => {}
}
// Mock requestAnimationFrame
global.requestAnimationFrame = (callback) => setTimeout(callback, 0)
global.cancelAnimationFrame = (id) => clearTimeout(id)