Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Void View

- Prepare templates for every component in production storage, with a renderer lookup at runtime. Keep page output selective: include only templates reachable from the dynamic components actually rendered on that page, including descendants that may appear on later renders. The prototype keeps the prepared templates in the generated server module.
- Store compiled source for dynamic components and their transitive dynamic children as separate chunks. Evaluate assembling page bundles on demand, preparing bundles during rendering, or another delivery strategy later. The prototype selects compiled source fragments and emits them inline during SSR; a static page needs neither View templates nor View JavaScript.

## View styling TODO

`@void/view` bundles static component styles and renders interpolated styles inside each Web Component instance. Instance styles currently duplicate CSS and incur browser parsing work. Defer deduplication and parsing optimizations for now; investigate React 19's stylesheet handling before committing to a solution.
58 changes: 58 additions & 0 deletions packages/view/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"name": "@void/view",
"version": "1.9.1",
"description": "Compiled HTML and Web Component views for Void.",
"license": "MIT",
"files": [
"dist"
],
"type": "module",
"exports": {
".": {
"types": "./dist/index.d.mts",
"default": "./dist/index.mjs"
},
"./jsx-runtime": {
"types": "./dist/jsx-runtime.d.mts",
"default": "./dist/jsx-runtime.mjs"
},
"./plugin": {
"types": "./dist/plugin.d.mts",
"import": "./dist/plugin.mjs"
},
"./server": {
"types": "./dist/server.d.mts",
"import": "./dist/server.mjs"
},
"./client": {
"types": "./dist/client.d.mts",
"import": "./dist/client.mjs"
}
},
"publishConfig": {
"access": "public"
},
"scripts": {
"build": "vp pack",
"dev": "vp pack --watch",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"entities": "^9.0.0",
"lightningcss": "^1.33.0",
"magic-string": "catalog:",
"oxc-parser": "^0.156.0",
"pathslash": "catalog:"
},
"devDependencies": {
"@types/node": "catalog:",
"typescript": "catalog:",
"vite": "catalog:",
"vite-plus": "catalog:",
"void": "workspace:*"
},
"peerDependencies": {
"vite": "^8.0.0",
"void": "workspace:*"
}
}
245 changes: 245 additions & 0 deletions packages/view/src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
import type { ViewResult } from './index.ts';
import {
attribute,
instanceStyles,
booleanAttributes,
type Binding,
type Initialize,
type Encoded,
} from './shared.ts';
interface Host extends HTMLElement {
styles?: Map<number, HTMLStyleElement>;
mount?: () => void;
refresh?: () => void;
}
let hydrating: Host | undefined;
class Fragment implements ViewResult {
readonly __view = true;
constructor(readonly create: () => DocumentFragment) {}
}
class SerializedHtml {
constructor(readonly html: string) {}
}
function decode(encoded: Encoded): unknown {
const [kind, value] = encoded;
if (kind === 'html') {
return new SerializedHtml(value);
}
if (kind === 'array') {
return value.map(decode);
}
if (kind === 'object') {
return Object.fromEntries(value.map(([key, item]) => [key, decode(item)]));
}
return kind === 'undefined' ? undefined : value;
}
function append(parent: Node, value: unknown): void {
if (value == null || typeof value === 'boolean') {
return;
}
if (Array.isArray(value)) {
for (const item of value) {
append(parent, item);
}
return;
}
if (value instanceof SerializedHtml) {
const template = document.createElement('template');
template.innerHTML = value.html;
parent.appendChild(template.content);
return;
}
if (value instanceof Fragment) {
parent.appendChild(value.create());
return;
}
if (value instanceof Node) {
parent.appendChild(value);
return;
}
if (typeof value !== 'string' && typeof value !== 'number') {
throw new Error('Invalid view child');
}
parent.appendChild(document.createTextNode(String(value)));
}
export function component(
id: string,
_props: unknown,
initialize: Initialize,
dynamic: boolean,
styled = false,
): ViewResult {
if (!dynamic) {
return initialize((value) => value)() as ViewResult;
}
const host = hydrating?.localName === id ? hydrating : (document.createElement(id) as Host);
if (host === hydrating) {
hydrating = undefined;
}
let instance: string | undefined;
let style: import('./shared.ts').Style | undefined;
if (styled) {
const attribute =
host.getAttributeNames().find((name) => name.startsWith('data-vv-rid-')) ??
'data-vv-rid-' + crypto.randomUUID();
host.setAttribute(attribute, '');
const rid = attribute.slice('data-vv-rid-'.length);
instance = rid;
host.setAttribute('data-vv-cid-' + rid, '');
host.styles = new Map();
style = (key, css) => {
const element = host.styles!.get(key) ?? document.createElement('style');
host.styles!.set(key, element);
element.textContent = instanceStyles(css, rid);
return element;
};
}
let queued = false;
const draw = initialize(
(value) => {
try {
return value;
} finally {
if (!queued) {
queued = true;
queueMicrotask(() => {
queued = false;
if (host.isConnected) {
host.refresh?.();
}
});
}
}
},
style,
instance,
);
host.refresh = () => {
const content = document.createDocumentFragment();
append(content, draw());
host.replaceChildren(content);
};
host.mount = () => {
host.refresh!();
host.setAttribute('data-vv-ready', '');
};
host.mount();
return host as unknown as ViewResult;
}
export function register(id: string, factory: (props: unknown) => unknown): void {
if (customElements.get(id)) {
return;
}
customElements.define(
id,
class extends HTMLElement {
connectedCallback() {
const host = this as Host;
if (host.mount || !this.hasAttribute('data-vv-props')) {
return;
}
const previous = hydrating;
hydrating = host;
try {
factory(decode(JSON.parse(this.getAttribute('data-vv-props')!)));
} finally {
hydrating = previous;
}
}
},
);
}
export function template(
id: string,
_html: string | null,
bindings: Array<Binding>,
instance?: string,
): ViewResult {
return new Fragment(() => {
const source = document.getElementById(id);
if (!(source instanceof HTMLTemplateElement)) {
throw new Error(`Missing view template: ${id}`);
}
const fragment = source.content.cloneNode(true) as DocumentFragment;
if (instance) {
for (const element of fragment.querySelectorAll('[data-vv-i]')) {
element.removeAttribute('data-vv-i');
element.setAttribute('data-vv-cid-' + instance, '');
}
}
const slots = new Map<number, ChildNode>(
Array.from(fragment.querySelectorAll('[data-vv-slot]'), (el) => [
Number(el.getAttribute('data-vv-slot')),
el,
]),
);
const comments = document.createTreeWalker(fragment, NodeFilter.SHOW_COMMENT);
while (comments.nextNode()) {
const comment = comments.currentNode as Comment;
if (/^vv:\d+$/.test(comment.data)) {
slots.set(Number(comment.data.slice(3)), comment);
}
}
const elements = new Map(
Array.from(fragment.querySelectorAll('[data-vv-a]'), (el) => [
Number(el.getAttribute('data-vv-a')),
el,
]),
);
function set(el: Element, name: string, value: unknown) {
// Share validation and boolean semantics with SSR.
attribute(name, value);
if (value == null || (booleanAttributes.has(name) && !value)) {
el.removeAttribute(name);
} else {
el.setAttribute(name, booleanAttributes.has(name) ? '' : String(value));
}
if (name === 'value' && 'value' in el) {
(el as HTMLInputElement).value = String(value ?? '');
}
if (name === 'checked' && 'checked' in el) {
(el as HTMLInputElement).checked = !!value;
}
}
const events = new Map<Element, Map<string, unknown>>();
function event(el: Element, name: string, value: unknown) {
if (!events.has(el)) {
events.set(el, new Map());
}
events.get(el)!.set(name, value);
}
for (const [kind, key, name, value] of bindings) {
if (kind === 'child') {
const content = document.createDocumentFragment();
append(content, value);
slots.get(key)!.replaceWith(content);
} else {
const el = elements.get(key)!;
if (kind === 'event') {
event(el, name, value);
} else if (kind === 'attr') {
set(el, name, value);
} else {
for (const [name, item] of Object.entries((value as object) ?? {})) {
if (/^on/i.test(name)) {
event(el, name.slice(2).toLowerCase(), item);
} else {
set(el, name, item);
}
}
}
}
}
for (const [element, handlers] of events) {
for (const [name, handler] of handlers) {
if (handler != null) {
element.addEventListener(name, handler as EventListener);
}
}
}
for (const el of elements.values()) {
el.removeAttribute('data-vv-a');
}
return fragment;
});
}
Loading
Loading