Skip to content

Commit

Permalink
build: baby steps migrating to ts.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjpillsbury committed May 31, 2024
1 parent 4358d66 commit 11ce908
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 49 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,18 @@ const documentShim = {
dispatchEvent(event /* eslint-disable-line no-unused-vars */) {
return false;
},
};
} as unknown as typeof globalThis['document'];

const globalThisShim = {
ResizeObserver,
document: documentShim,
Node: Node,
HTMLElement: class HTMLElement extends Node {},
HTMLElement: class HTMLElement extends Node {
innerHTML: string = '';
get content() {
return new DocumentFragment();
}
},
DocumentFragment: class DocumentFragment extends EventTarget {},
customElements: {
get: function () {},
Expand Down Expand Up @@ -79,59 +84,15 @@ const globalThisShim = {
media,
};
},
};
} as unknown as typeof globalThis;

export const isServer =
typeof window === 'undefined' || typeof window.customElements === 'undefined';

const isShimmed = Object.keys(globalThisShim).every((key) => key in globalThis);

/**
* @type { globalThis & {
* WebKitPlaybackTargetAvailabilityEvent?,
* chrome?,
* DocumentFragment?,
* getComputedStyle,
* CastableVideoElement?
* } |
* {Node,
* HTMLElement,
* customElements,
* CustomEvent,
* getComputedStyle,
* addEventListener?,
* removeEventListener?,
* localStorage?,
* WebKitPlaybackTargetAvailabilityEvent?,
* window?,
* document?,
* chrome?,
* DocumentFragment?,
* ResizeObserver?,
* CastableVideoElement?,
* navigator?,
* matchMedia,
* } }
* */
export const GlobalThis = isServer && !isShimmed ? globalThisShim : globalThis;

/**
* @type { document & { webkitExitFullscreen? } |
* {createElement,
* createElementNS?,
* activeElement?,
* fullscreenElement?,
* webkitExitFullscreen?,
* getElementById?,
* pictureInPictureElement?,
* exitPictureInPicture?,
* pictureInPictureEnabled?,
* requestPictureInPicture?,
* addEventListener?,
* removeEventListener?,
* } }
*/
export const Document =
export const GlobalThis: typeof globalThis = isServer && !isShimmed ? globalThisShim : globalThis;
export const Document: typeof globalThis['document'] =
isServer && !isShimmed ? documentShim : globalThis.document;

export { GlobalThis as globalThis, Document as document };

0 comments on commit 11ce908

Please sign in to comment.