From ab2d670a222eb0379272182c902073eda58fd444 Mon Sep 17 00:00:00 2001 From: Joe Pea Date: Sat, 31 Aug 2024 20:32:21 -0700 Subject: [PATCH 1/3] export constants from dom-expressions server.js --- packages/dom-expressions/src/server.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/dom-expressions/src/server.js b/packages/dom-expressions/src/server.js index df4086a2..7a2a0742 100644 --- a/packages/dom-expressions/src/server.js +++ b/packages/dom-expressions/src/server.js @@ -3,6 +3,17 @@ import { sharedConfig, root } from "rxcore"; import { createSerializer, getLocalHeaderScript } from "./serializer"; export { createComponent } from "rxcore"; +export { + Properties, + ChildProperties, + getPropAlias, + Aliases, + DOMElements, + SVGElements, + SVGNamespace, + DelegatedEvents +} from "./constants.js"; + // Based on https://github.com/WebReflection/domtagger/blob/master/esm/sanitizer.js const VOID_ELEMENTS = /^(?:area|base|br|col|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr)$/i; From 523c89b34c2c415776c7a6625f07ee854a120844 Mon Sep 17 00:00:00 2001 From: Joe Pea Date: Sat, 31 Aug 2024 23:08:17 -0700 Subject: [PATCH 2/3] export functions from dom-expressions server.js to match client.js exports, and make client-only functions throw on the serverside --- packages/dom-expressions/src/client.d.ts | 5 +- packages/dom-expressions/src/client.js | 2 +- packages/dom-expressions/src/server.d.ts | 85 ++++++++++++++++++++++++ packages/dom-expressions/src/server.js | 33 ++++++++- 4 files changed, 121 insertions(+), 4 deletions(-) diff --git a/packages/dom-expressions/src/client.d.ts b/packages/dom-expressions/src/client.d.ts index 9a075095..85dd1763 100644 --- a/packages/dom-expressions/src/client.d.ts +++ b/packages/dom-expressions/src/client.d.ts @@ -57,7 +57,7 @@ export function dynamicProperty(props: unknown, key: string): unknown; export function hydrate( fn: () => JSX.Element, node: MountableElement, - options?: { renderId?: string, owner?: unknown } + options?: { renderId?: string; owner?: unknown } ): () => void; export function getHydrationKey(): string; export function getNextElement(template?: HTMLTemplateElement): Element; @@ -74,4 +74,5 @@ export interface RequestEvent { request: Request; } export declare const RequestContext: unique symbol; -export function getRequestEvent(): RequestEvent | undefined; \ No newline at end of file +export function getRequestEvent(): RequestEvent | undefined; +export function runHydrationEvents(): void; diff --git a/packages/dom-expressions/src/client.js b/packages/dom-expressions/src/client.js index 457add6b..a2323e3e 100644 --- a/packages/dom-expressions/src/client.js +++ b/packages/dom-expressions/src/client.js @@ -466,7 +466,7 @@ function insertExpression(parent, value, current, marker, unwrapArray) { if (marker === undefined) return (current = [...parent.childNodes]); let node = array[0]; if (node.parentNode !== parent) return current; - const nodes = [node] + const nodes = [node]; while ((node = node.nextSibling) !== marker) nodes.push(node); return (current = nodes); } diff --git a/packages/dom-expressions/src/server.d.ts b/packages/dom-expressions/src/server.d.ts index 9a11aaa0..5bb67c20 100644 --- a/packages/dom-expressions/src/server.d.ts +++ b/packages/dom-expressions/src/server.d.ts @@ -1,3 +1,12 @@ +export const Aliases: Record; +export const Properties: Set; +export const ChildProperties: Set; +export const DelegatedEvents: Set; +export const DOMElements: Set; +export const SVGElements: Set; +export const SVGNamespace: Record; +export function getPropAlias(prop: string, tagName: string): string | undefined; + export function renderToString( fn: () => T, options?: { @@ -87,3 +96,79 @@ export function pipeToNodeWritable( onCompleteAll?: () => void; } ): void; + +export function untrack(fn: () => T): T; + +// client-only APIs + +/** @deprecated not supported on the server side */ +export function classList( + node: Element, + value: { [k: string]: boolean }, + prev?: { [k: string]: boolean } +): { [k: string]: boolean }; + +/** @deprecated not supported on the server side */ +export function style( + node: Element, + value: { [k: string]: string }, + prev?: { [k: string]: string } +): void; + +/** @deprecated not supported on the server side */ +export function insert( + parent: MountableElement, + accessor: (() => T) | T, + marker?: Node | null, + init?: JSX.Element +): JSX.Element; + +/** @deprecated not supported on the server side */ +export function spread( + node: Element, + accessor: (() => T) | T, + isSVG?: Boolean, + skipChildren?: Boolean +): void; + +/** @deprecated not supported on the server side */ +export function delegateEvents(eventNames: string[], d?: Document): void; +/** @deprecated not supported on the server side */ +export function dynamicProperty(props: unknown, key: string): unknown; +/** @deprecated not supported on the server side */ +export function setAttribute(node: Element, name: string, value: string): void; +/** @deprecated not supported on the server side */ +export function setAttributeNS(node: Element, namespace: string, name: string, value: string): void; + +/** @deprecated not supported on the server side */ +export function addEventListener( + node: Element, + name: string, + handler: () => void, + delegate: boolean +): void; + +/** @deprecated not supported on the server side */ +export function render(code: () => JSX.Element, element: MountableElement): () => void; +/** @deprecated not supported on the server side */ +export function template(html: string, isCE?: boolean, isSVG?: boolean): () => Element; +/** @deprecated not supported on the server side */ +export function setProperty(node: Element, name: string, value: any): void; +/** @deprecated not supported on the server side */ +export function className(node: Element, value: string): void; +/** @deprecated not supported on the server side */ +export function assign(node: Element, props: any, isSVG?: Boolean, skipChildren?: Boolean): void; + +/** @deprecated not supported on the server side */ +export function hydrate( + fn: () => JSX.Element, + node: MountableElement, + options?: { renderId?: string; owner?: unknown } +): () => void; + +/** @deprecated not supported on the server side */ +export function getNextElement(template?: HTMLTemplateElement): Element; +/** @deprecated not supported on the server side */ +export function getNextMatch(start: Node, elementName: string): Element; +/** @deprecated not supported on the server side */ +export function getNextMarker(start: Node): [Node, Array]; diff --git a/packages/dom-expressions/src/server.js b/packages/dom-expressions/src/server.js index 7a2a0742..1e9833d0 100644 --- a/packages/dom-expressions/src/server.js +++ b/packages/dom-expressions/src/server.js @@ -1,7 +1,8 @@ import { Aliases, BooleanAttributes, ChildProperties } from "./constants"; import { sharedConfig, root } from "rxcore"; import { createSerializer, getLocalHeaderScript } from "./serializer"; -export { createComponent } from "rxcore"; + +export { getOwner, createComponent, effect, memo, untrack } from "rxcore"; export { Properties, @@ -685,3 +686,33 @@ export function ssrSpread(props, isSVG, skipChildren) { } return result; } + +// client-only APIs + +export { + notSup as classList, + notSup as style, + notSup as insert, + notSup as spread, + notSup as delegateEvents, + notSup as dynamicProperty, + notSup as setAttribute, + notSup as setAttributeNS, + notSup as addEventListener, + notSup as render, + notSup as template, + notSup as setProperty, + notSup as className, + notSup as assign, + notSup as hydrate, + notSup as getNextElement, + notSup as getNextMatch, + notSup as getNextMarker, + notSup as runHydrationEvents +}; + +function notSup() { + throw new Error( + "Client-only API called on the server side. Run client-only code in onMount, or conditionally run client-only component with ." + ); +} From bb6fb1c84deff322304cc5b549515a6d8e23675e Mon Sep 17 00:00:00 2001 From: Joe Pea Date: Sun, 1 Sep 2024 14:23:10 -0700 Subject: [PATCH 3/3] add missing imports, add missing types --- packages/dom-expressions/src/server.d.ts | 3 +++ packages/dom-expressions/src/server.js | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/dom-expressions/src/server.d.ts b/packages/dom-expressions/src/server.d.ts index 5bb67c20..6d39b01a 100644 --- a/packages/dom-expressions/src/server.d.ts +++ b/packages/dom-expressions/src/server.d.ts @@ -1,3 +1,4 @@ +import { JSX } from "./jsx.js"; export const Aliases: Record; export const Properties: Set; export const ChildProperties: Set; @@ -7,6 +8,8 @@ export const SVGElements: Set; export const SVGNamespace: Record; export function getPropAlias(prop: string, tagName: string): string | undefined; +type MountableElement = Element | Document | ShadowRoot | DocumentFragment | Node; + export function renderToString( fn: () => T, options?: { diff --git a/packages/dom-expressions/src/server.js b/packages/dom-expressions/src/server.js index 1e9833d0..251f91f3 100644 --- a/packages/dom-expressions/src/server.js +++ b/packages/dom-expressions/src/server.js @@ -1,4 +1,4 @@ -import { Aliases, BooleanAttributes, ChildProperties } from "./constants"; +import { Aliases, BooleanAttributes, ChildProperties, Properties } from "./constants"; import { sharedConfig, root } from "rxcore"; import { createSerializer, getLocalHeaderScript } from "./serializer";