mirror of
https://github.com/zoriya/v10.git
synced 2026-08-16 02:45:09 +00:00
fix(packages): add server-only bundles (#1349)
This commit is contained in:
@@ -2,8 +2,8 @@ type DefinableElement = CustomElementConstructor & { tagName: string };
|
||||
|
||||
/** Define a custom element only if not already registered. */
|
||||
export function safeDefine(element: DefinableElement): void {
|
||||
const registry = globalThis.customElements;
|
||||
if (!registry || registry.get(element.tagName)) return;
|
||||
if (!__BROWSER__) return;
|
||||
if (customElements.get(element.tagName)) return;
|
||||
|
||||
registry.define(element.tagName, element);
|
||||
customElements.define(element.tagName, element);
|
||||
}
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { safeDefine } from '../safe-define';
|
||||
|
||||
describe('safeDefine', () => {
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
it('registers a custom element', () => {
|
||||
class TestElement extends HTMLElement {
|
||||
static tagName = 'test-sd-register';
|
||||
@@ -37,14 +33,4 @@ describe('safeDefine', () => {
|
||||
safeDefine(Replacement);
|
||||
expect(customElements.get('test-sd-no-replace')).toBe(Original);
|
||||
});
|
||||
|
||||
it('does nothing when customElements is unavailable', () => {
|
||||
vi.stubGlobal('customElements', undefined);
|
||||
|
||||
class TestElement extends HTMLElement {
|
||||
static tagName = 'test-sd-ssr';
|
||||
}
|
||||
|
||||
expect(() => safeDefine(TestElement)).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
describe('SSR-safe define imports', () => {
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
vi.resetModules();
|
||||
});
|
||||
|
||||
it('imports video skin without browser-only globals', async () => {
|
||||
vi.stubGlobal('customElements', undefined);
|
||||
vi.stubGlobal('CSSStyleSheet', undefined);
|
||||
|
||||
await expect(import('../video/skin')).resolves.toBeDefined();
|
||||
});
|
||||
|
||||
it('imports simple-hls-video without customElements', async () => {
|
||||
vi.stubGlobal('customElements', undefined);
|
||||
|
||||
await expect(import('../media/simple-hls-video')).resolves.toBeDefined();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user