Files
ComfyUI_frontend/scripts/setup-browser-globals.mjs
snomiao c3e2588ae0 fix: resolve collect-i18n babel transformation issues
- Created configurable babel-plugin-inject-globals to inject browser setup
- Added setup-browser-globals.mjs for browser environment mocking
- Moved babel plugin files to scripts directory for better organization
- Removed dependency on import order by using babel transformation
- Made plugin options configurable (filenamePattern, setupFile)
- Updated tsconfig.json to include playwright config and scripts

This fixes the ReferenceError: location is not defined issue that occurred
when running pnpm collect-i18n, ensuring the command works reliably
regardless of import auto-sorting.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-24 04:35:35 +00:00

76 lines
1.9 KiB
JavaScript

// Setup browser-like globals for Node.js environment
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const { Window } = require('happy-dom');
// Set build-time constants
global.__USE_PROD_CONFIG__ = false;
global.__USE_LOCAL_SERVER__ = true;
global.__RUN_TESTS__ = true;
const window = new Window({
url: 'http://localhost:5173',
width: 1024,
height: 768
});
global.window = window;
global.document = window.document;
global.location = window.location;
// Don't set navigator if it's read-only
if (!global.navigator || Object.getOwnPropertyDescriptor(global, 'navigator')?.set) {
global.navigator = window.navigator;
}
global.HTMLElement = window.HTMLElement;
global.Element = window.Element;
global.CustomEvent = window.CustomEvent;
global.requestAnimationFrame = window.requestAnimationFrame;
// Use happy-dom's storage implementations
global.localStorage = window.localStorage;
global.sessionStorage = window.sessionStorage;
// Mock fetch if not available
if (!global.fetch) {
global.fetch = () => Promise.resolve({
ok: true,
json: () => Promise.resolve({}),
text: () => Promise.resolve(''),
blob: () => Promise.resolve(new Blob()),
arrayBuffer: () => Promise.resolve(new ArrayBuffer(0)),
headers: new Map()
});
}
// Mock ResizeObserver
global.ResizeObserver = class ResizeObserver {
observe() {}
unobserve() {}
disconnect() {}
};
// Mock IntersectionObserver
global.IntersectionObserver = class IntersectionObserver {
constructor() {}
observe() {}
unobserve() {}
disconnect() {}
};
// Mock getComputedStyle
global.getComputedStyle = window.getComputedStyle;
// Mock createRange
global.document.createRange = () => ({
setStart: () => {},
setEnd: () => {},
getBoundingClientRect: () => ({
top: 0,
left: 0,
right: 0,
bottom: 0,
width: 0,
height: 0
}),
getClientRects: () => []
});