From b2388b1c09752dccf3f6bf91c6bedff3a4153d75 Mon Sep 17 00:00:00 2001
From: Sasha Koss
Date: Sat, 14 Nov 2026 19:47:58 +0800
Subject: [PATCH 1/3] chore: first @void/view PoC
---
TODO.md | 4 +
packages/view/package.json | 57 ++
packages/view/src/client.ts | 209 +++++++
packages/view/src/compiler.ts | 622 ++++++++++++++++++++
packages/view/src/index.ts | 15 +
packages/view/src/inline.ts | 322 ++++++++++
packages/view/src/jsx-runtime.ts | 21 +
packages/view/src/plugin.ts | 349 +++++++++++
packages/view/src/references.ts | 207 +++++++
packages/view/src/resources.ts | 102 ++++
packages/view/src/server.ts | 144 +++++
packages/view/src/shared.ts | 44 ++
packages/view/test/integration/view.test.ts | 208 +++++++
packages/view/test/unit/compiler.test.ts | 167 ++++++
packages/view/test/unit/resources.test.ts | 96 +++
packages/view/tsconfig.build.json | 1 +
packages/view/tsconfig.json | 13 +
packages/view/vite.config.ts | 17 +
playground/view/components/Button.tsx | 29 +
playground/view/components/Counter.tsx | 45 ++
playground/view/components/Details.tsx | 46 ++
playground/view/components/Label.tsx | 4 +
playground/view/components/Reveal.tsx | 21 +
playground/view/components/Test.tsx | 13 +
playground/view/components/greeting.ts | 3 +
playground/view/components/index.ts | 1 +
playground/view/package.json | 21 +
playground/view/pages/about.tsx | 9 +
playground/view/pages/button.tsx | 13 +
playground/view/pages/conditional.server.ts | 4 +
playground/view/pages/conditional.tsx | 10 +
playground/view/pages/index.tsx | 34 ++
playground/view/pages/style.css | 52 ++
playground/view/tsconfig.json | 15 +
playground/view/vite.config.ts | 4 +
playground/view/void.config.ts | 2 +
pnpm-lock.yaml | 53 ++
vite.config.ts | 5 +
38 files changed, 2982 insertions(+)
create mode 100644 TODO.md
create mode 100644 packages/view/package.json
create mode 100644 packages/view/src/client.ts
create mode 100644 packages/view/src/compiler.ts
create mode 100644 packages/view/src/index.ts
create mode 100644 packages/view/src/inline.ts
create mode 100644 packages/view/src/jsx-runtime.ts
create mode 100644 packages/view/src/plugin.ts
create mode 100644 packages/view/src/references.ts
create mode 100644 packages/view/src/resources.ts
create mode 100644 packages/view/src/server.ts
create mode 100644 packages/view/src/shared.ts
create mode 100644 packages/view/test/integration/view.test.ts
create mode 100644 packages/view/test/unit/compiler.test.ts
create mode 100644 packages/view/test/unit/resources.test.ts
create mode 100644 packages/view/tsconfig.build.json
create mode 100644 packages/view/tsconfig.json
create mode 100644 packages/view/vite.config.ts
create mode 100644 playground/view/components/Button.tsx
create mode 100644 playground/view/components/Counter.tsx
create mode 100644 playground/view/components/Details.tsx
create mode 100644 playground/view/components/Label.tsx
create mode 100644 playground/view/components/Reveal.tsx
create mode 100644 playground/view/components/Test.tsx
create mode 100644 playground/view/components/greeting.ts
create mode 100644 playground/view/components/index.ts
create mode 100644 playground/view/package.json
create mode 100644 playground/view/pages/about.tsx
create mode 100644 playground/view/pages/button.tsx
create mode 100644 playground/view/pages/conditional.server.ts
create mode 100644 playground/view/pages/conditional.tsx
create mode 100644 playground/view/pages/index.tsx
create mode 100644 playground/view/pages/style.css
create mode 100644 playground/view/tsconfig.json
create mode 100644 playground/view/vite.config.ts
create mode 100644 playground/view/void.config.ts
diff --git a/TODO.md b/TODO.md
new file mode 100644
index 000000000..898b8cfca
--- /dev/null
+++ b/TODO.md
@@ -0,0 +1,4 @@
+# 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.
diff --git a/packages/view/package.json b/packages/view/package.json
new file mode 100644
index 000000000..c38361370
--- /dev/null
+++ b/packages/view/package.json
@@ -0,0 +1,57 @@
+{
+ "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",
+ "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:*"
+ }
+}
diff --git a/packages/view/src/client.ts b/packages/view/src/client.ts
new file mode 100644
index 000000000..b1bf67fba
--- /dev/null
+++ b/packages/view/src/client.ts
@@ -0,0 +1,209 @@
+import type { ViewResult } from './index.ts';
+import {
+ attribute,
+ booleanAttributes,
+ type Binding,
+ type Initialize,
+ type Encoded,
+} from './shared.ts';
+interface Host extends HTMLElement {
+ 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,
+): 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 queued = false;
+ const draw = initialize((value) => {
+ try {
+ return value;
+ } finally {
+ if (!queued) {
+ queued = true;
+ queueMicrotask(() => {
+ queued = false;
+ if (host.isConnected) {
+ host.refresh?.();
+ }
+ });
+ }
+ }
+ });
+ 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): 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;
+ const slots = new Map(
+ 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>();
+ 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;
+ });
+}
diff --git a/packages/view/src/compiler.ts b/packages/view/src/compiler.ts
new file mode 100644
index 000000000..0fa73c8f5
--- /dev/null
+++ b/packages/view/src/compiler.ts
@@ -0,0 +1,622 @@
+import { createHash } from 'node:crypto';
+import { decodeHTML } from 'entities';
+import MagicString, { type SourceMap } from 'magic-string';
+import { parseSync } from 'oxc-parser';
+import { escapeHtml, voidTags } from './shared.ts';
+
+// Oxc's ESTree nodes have heterogeneous fields; all access is guarded by node kind.
+// eslint-disable-next-line @typescript-eslint/no-explicit-any -- A structural ESTree cursor; fields are selected by node kind below.
+export type AstNode = { type: string; start: number; end: number; [key: string]: any };
+type Node = AstNode;
+export interface ComponentInfo {
+ name: string;
+ id: string;
+ node: Node;
+ dynamic: boolean;
+ dependencies: Set;
+ callbacks: Set;
+ templates: Map;
+ defaultStatementStart?: number;
+ callbackArguments: Array<{ tag: string; name: string; value?: string; known: boolean }>;
+ callbackProps: Set;
+ propBindings: Map;
+}
+export interface ModuleInfo {
+ file: string;
+ source: string;
+ program: Node;
+ components: Array;
+ imports: Map;
+ exports: Map;
+ functions: Set;
+ stars: Array;
+}
+function children(node: Node): Array {
+ return Object.values(node)
+ .flatMap((value) => (Array.isArray(value) ? value : [value]))
+ .filter((value) => value && typeof value === 'object' && typeof value.type === 'string');
+}
+function walk(node: Node, visit: (node: Node) => boolean | void): void {
+ if (visit(node) === false) {
+ return;
+ }
+ for (const child of children(node)) {
+ walk(child, visit);
+ }
+}
+export function bindingNames(node: Node): Array {
+ if (node.type === 'Identifier') {
+ return [node.name];
+ }
+ if (node.type === 'MemberExpression') {
+ return bindingNames(node.object);
+ }
+ if (node.type === 'AssignmentPattern') {
+ return bindingNames(node.left);
+ }
+ if (node.type === 'RestElement') {
+ return bindingNames(node.argument);
+ }
+ if (node.type === 'ArrayPattern') {
+ return node.elements.flatMap((item: Node | null) => (item ? bindingNames(item) : []));
+ }
+ if (node.type === 'ObjectPattern') {
+ return node.properties.flatMap((item: Node) =>
+ bindingNames(item.type === 'RestElement' ? item : item.value),
+ );
+ }
+ return [];
+}
+function tagName(node: Node): string {
+ if (node.type === 'JSXIdentifier') {
+ return node.name;
+ }
+ if (node.type === 'JSXMemberExpression') {
+ return `${tagName(node.object)}.${tagName(node.property)}`;
+ }
+ throw new Error('@void/view: JSX namespaces are not supported');
+}
+export function analyze(source: string, file: string, identity = file): ModuleInfo {
+ const result = parseSync(file, source);
+ if (result.errors.length) {
+ throw new Error(`${file}: ${result.errors.map((error) => error.message).join('\n')}`);
+ }
+ const program = result.program as unknown as Node;
+ const info: ModuleInfo = {
+ file,
+ source,
+ program,
+ components: [],
+ imports: new Map(),
+ exports: new Map(),
+ functions: new Set(),
+ stars: [],
+ };
+ for (const statement of program.body) {
+ const declaration = statement.declaration ?? statement;
+ if (declaration.type === 'FunctionDeclaration' && declaration.id) {
+ info.functions.add(declaration.id.name);
+ }
+ if (declaration.type === 'VariableDeclaration') {
+ for (const item of declaration.declarations) {
+ if (['ArrowFunctionExpression', 'FunctionExpression'].includes(item.init?.type)) {
+ info.functions.add(item.id.name);
+ }
+ }
+ }
+ }
+ const add = (node: Node, name: string) => {
+ let jsx = false;
+ walk(node.body, (child) => {
+ if (child.type === 'JSXElement' || child.type === 'JSXFragment') {
+ jsx = true;
+ }
+ });
+ if (!jsx && !/^[A-Z]/.test(name)) {
+ return;
+ }
+ if (node.async || node.generator) {
+ throw new Error(`${file}: view components must be synchronous`);
+ }
+ if (node.params.length > 1) {
+ throw new Error(`${file}: view components accept a single props argument`);
+ }
+ const id = `vv-${name.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase()}-${createHash(
+ 'sha256',
+ )
+ .update(identity + ':' + name)
+ .digest('hex')
+ .slice(0, 8)}`;
+ const component: ComponentInfo = {
+ name,
+ id,
+ node,
+ dynamic: false,
+ dependencies: new Set(),
+ callbacks: new Set(),
+ templates: new Map(),
+ callbackArguments: [],
+ callbackProps: new Set(),
+ propBindings: new Map(),
+ };
+ const param =
+ node.params[0]?.type === 'AssignmentPattern' ? node.params[0].left : node.params[0];
+ if (param?.type === 'ObjectPattern') {
+ for (const property of param.properties) {
+ if (property.type === 'Property') {
+ for (const binding of bindingNames(property.value)) {
+ component.propBindings.set(binding, property.key.name ?? property.key.value);
+ }
+ }
+ }
+ }
+ const functions = new Set(info.functions);
+ walk(node.body, (child) => {
+ if (child.type === 'FunctionDeclaration' && child.id) {
+ functions.add(child.id.name);
+ }
+ if (
+ child.type === 'VariableDeclarator' &&
+ ['ArrowFunctionExpression', 'FunctionExpression'].includes(child.init?.type)
+ ) {
+ functions.add(child.id.name);
+ }
+ });
+ walk(node.body, (child) => {
+ if (child.type !== 'JSXOpeningElement') {
+ return;
+ }
+ const tag = tagName(child.name);
+ const native = child.name.type === 'JSXIdentifier' && /^[a-z]/.test(tag);
+ if (!native) {
+ component.dependencies.add(tag);
+ }
+ for (const attr of child.attributes) {
+ if (attr.type === 'JSXSpreadAttribute') {
+ component.dynamic = true;
+ if (!native) {
+ component.callbacks.add(tag);
+ component.callbackArguments.push({ tag, name: '*', known: true });
+ }
+ continue;
+ }
+ const name = tagName(attr.name);
+ const expression = attr.value?.expression;
+ const callback =
+ /^on/i.test(name) ||
+ ['ArrowFunctionExpression', 'FunctionExpression'].includes(expression?.type) ||
+ (expression?.type === 'Identifier' && functions.has(expression.name));
+ if (!native) {
+ let value = expression?.type === 'Identifier' ? expression.name : undefined;
+ if (
+ expression?.type === 'MemberExpression' &&
+ expression.object.type === 'Identifier' &&
+ expression.object.name === param?.name &&
+ !expression.computed
+ ) {
+ value = source.slice(expression.start, expression.end);
+ component.propBindings.set(value, expression.property.name);
+ }
+ component.callbackArguments.push({ tag, name, value, known: callback });
+ }
+ if (callback) {
+ component.dynamic = true;
+ if (!native) {
+ component.callbacks.add(tag);
+ }
+ }
+ }
+ });
+ info.components.push(component);
+ };
+ for (const statement of program.body) {
+ if (statement.type === 'ImportDeclaration' && statement.importKind !== 'type') {
+ for (const spec of statement.specifiers) {
+ if (spec.importKind !== 'type') {
+ info.imports.set(spec.local.name, {
+ source: statement.source.value,
+ imported:
+ spec.type === 'ImportDefaultSpecifier' ? 'default' : (spec.imported?.name ?? '*'),
+ });
+ }
+ }
+ }
+ const declaration = statement.declaration ?? statement;
+ if (declaration.type === 'FunctionDeclaration') {
+ const name = declaration.id?.name ?? 'DefaultView';
+ add(declaration, name);
+ if (statement.type.startsWith('Export')) {
+ info.exports.set(statement.type === 'ExportDefaultDeclaration' ? 'default' : name, name);
+ }
+ } else if (declaration.type === 'VariableDeclaration') {
+ for (const item of declaration.declarations) {
+ if (
+ ['ArrowFunctionExpression', 'FunctionExpression'].includes(item.init?.type) &&
+ item.id.type === 'Identifier'
+ ) {
+ add(item.init, item.id.name);
+ }
+ if (statement.type === 'ExportNamedDeclaration') {
+ info.exports.set(item.id.name, item.id.name);
+ }
+ }
+ } else if (statement.type === 'ExportDefaultDeclaration' && declaration.type === 'Identifier') {
+ info.exports.set('default', declaration.name);
+ } else if (
+ statement.type === 'ExportDefaultDeclaration' &&
+ declaration.type === 'ArrowFunctionExpression'
+ ) {
+ add(declaration, 'DefaultView');
+ info.components.at(-1)!.defaultStatementStart = statement.start;
+ info.exports.set('default', 'DefaultView');
+ }
+ if (statement.type === 'ExportNamedDeclaration') {
+ for (const spec of statement.specifiers ?? []) {
+ info.exports.set(spec.exported.name, spec.local.name);
+ if (statement.source && statement.exportKind !== 'type') {
+ info.imports.set(spec.local.name, {
+ source: statement.source.value,
+ imported: spec.local.name,
+ });
+ }
+ }
+ }
+ if (statement.type === 'ExportAllDeclaration' && statement.exportKind !== 'type') {
+ info.stars.push(statement.source.value);
+ }
+ }
+ return info;
+}
+function jsxText(value: string): string {
+ const lines = decodeHTML(value).replace(/\r/g, '').split('\n');
+ return lines
+ .map((line, index) => {
+ let text = line.replace(/\t/g, ' ');
+ if (index > 0) {
+ text = text.trimStart();
+ }
+ if (index < lines.length - 1) {
+ text = text.trimEnd();
+ }
+ return text;
+ })
+ .filter(Boolean)
+ .join(' ');
+}
+export function compile(
+ info: ModuleInfo,
+ client: boolean,
+): { code: string; map: SourceMap | null } {
+ const output = new MagicString(info.source);
+ const source = info.source;
+ const imports = `import { component as __vv_component, template as __vv_template, register as __vv_register } from '@void/view/${client ? 'client' : 'server'}';\n`;
+ if (!info.components.length) {
+ return { code: source, map: null };
+ }
+ if (/\b__vv_\w*/.test(source)) {
+ throw new Error(`${info.file}: the __vv_ prefix is reserved by @void/view`);
+ }
+ for (const component of info.components) {
+ component.templates.clear();
+ const fn = component.node;
+ const state = new Set();
+ for (const statement of fn.body.body ?? []) {
+ if (statement.type === 'VariableDeclaration' && statement.kind === 'let') {
+ for (const item of statement.declarations) {
+ for (const name of bindingNames(item.id)) {
+ state.add(name);
+ }
+ }
+ }
+ }
+ let templateIndex = 0;
+ function expr(node: Node, shadow = new Set(), rendering = true): string {
+ if (node.type === 'JSXElement' || node.type === 'JSXFragment') {
+ return jsx(node, shadow);
+ }
+ const edit = new MagicString(source.slice(node.start, node.end));
+ function patch(current: Node, hidden: Set, inRender: boolean) {
+ if (current.type === 'JSXElement' || current.type === 'JSXFragment') {
+ edit.overwrite(
+ current.start - node.start,
+ current.end - node.start,
+ jsx(current, hidden),
+ );
+ return;
+ }
+ let scoped = hidden;
+ if (
+ ['ArrowFunctionExpression', 'FunctionExpression', 'FunctionDeclaration'].includes(
+ current.type,
+ )
+ ) {
+ inRender = false;
+ }
+ if (
+ [
+ 'ArrowFunctionExpression',
+ 'FunctionExpression',
+ 'FunctionDeclaration',
+ 'BlockStatement',
+ 'CatchClause',
+ 'ForStatement',
+ 'ForOfStatement',
+ 'ForInStatement',
+ ].includes(current.type)
+ ) {
+ scoped = new Set(hidden);
+ const bindings: Array = [
+ ...(current.params ?? []),
+ ...(current.param ? [current.param] : []),
+ ];
+ const loop = current.init ?? current.left;
+ if (loop?.type === 'VariableDeclaration') {
+ bindings.push(...loop.declarations.map((item: Node) => item.id));
+ }
+ if (current !== fn.body) {
+ for (const statement of current.body?.body ??
+ (Array.isArray(current.body) ? current.body : [])) {
+ if (statement.type === 'VariableDeclaration') {
+ bindings.push(...statement.declarations.map((item: Node) => item.id));
+ }
+ }
+ }
+ for (const binding of bindings) {
+ for (const name of bindingNames(binding)) {
+ scoped.add(name);
+ }
+ }
+ }
+ if (current.type === 'AssignmentExpression' || current.type === 'UpdateExpression') {
+ const target = current.left ?? current.argument;
+ const changed = bindingNames(target).some((name) => state.has(name) && !scoped.has(name));
+ if (changed) {
+ if (inRender) {
+ throw new Error(
+ `${info.file}: update view state inside callbacks, not while rendering`,
+ );
+ }
+ // Recurse into operands before wrapping, retaining expression result and evaluation order.
+ for (const child of children(current)) {
+ patch(child, scoped, inRender);
+ }
+ edit.prependLeft(current.start - node.start, '__vv_change(');
+ edit.appendRight(current.end - node.start, ')');
+ return;
+ }
+ }
+ for (const child of children(current)) {
+ patch(child, scoped, inRender);
+ }
+ }
+ patch(node, shadow, rendering);
+ return edit.toString();
+ }
+ function jsx(node: Node, shadow: Set): string {
+ const id = component.id + (templateIndex++ ? `-${templateIndex - 1}` : '');
+ let key = 0;
+ const bindings: Array = [];
+ function slot(value: string, name = ''): string {
+ const index = key++;
+ bindings.push(`['child',${index},'',${value}]`);
+ return name ? `` : ``;
+ }
+ function child(n: Node, parent = ''): string {
+ if (n.type === 'JSXText') {
+ return escapeHtml(jsxText(n.value));
+ }
+ if (n.type === 'JSXExpressionContainer') {
+ if (n.expression.type === 'JSXEmptyExpression') {
+ return '';
+ }
+ return slot(
+ expr(n.expression, shadow),
+ n.expression.type === 'Identifier' &&
+ n.expression.name === 'children' &&
+ ![
+ 'table',
+ 'thead',
+ 'tbody',
+ 'tfoot',
+ 'tr',
+ 'colgroup',
+ 'select',
+ 'optgroup',
+ ].includes(parent)
+ ? 'children'
+ : '',
+ );
+ }
+ if (n.type === 'JSXSpreadChild') {
+ return slot(expr(n.expression, shadow));
+ }
+ return element(n);
+ }
+ function element(n: Node): string {
+ if (n.type === 'JSXFragment') {
+ return n.children.map((item: Node) => child(item)).join('');
+ }
+ const opening = n.openingElement;
+ const tag = tagName(opening.name);
+ if (opening.name.type !== 'JSXIdentifier' || !/^[a-z]/.test(tag)) {
+ const props = opening.attributes.map((attr: Node) => {
+ if (attr.type === 'JSXSpreadAttribute') {
+ return `...(${expr(attr.argument, shadow)})`;
+ }
+ const name = tagName(attr.name);
+ const value = !attr.value
+ ? 'true'
+ : attr.value.type === 'Literal'
+ ? JSON.stringify(decodeHTML(attr.value.value))
+ : expr(attr.value.expression, shadow);
+ return `${JSON.stringify(name)}:${value}`;
+ });
+ if (n.children.length) {
+ const values = n.children.flatMap((c: Node) => {
+ if (c.type === 'JSXText') {
+ const text = jsxText(c.value);
+ return text ? [JSON.stringify(text)] : [];
+ }
+ if (c.type === 'JSXExpressionContainer') {
+ return c.expression.type === 'JSXEmptyExpression'
+ ? []
+ : [expr(c.expression, shadow)];
+ }
+ return [expr(c, shadow)];
+ });
+ props.push(`children:${values.length === 1 ? values[0] : `[${values.join(',')}]`}`);
+ }
+ return slot(`${tag}({${props.join(',')}})`);
+ }
+ let rawText: string | undefined;
+ if (['script', 'style', 'textarea', 'title'].includes(tag)) {
+ const text: string = n.children
+ .map((child: Node) => {
+ if (child.type === 'JSXText') {
+ return decodeHTML(child.value);
+ }
+ const value = child.expression;
+ if (value?.type === 'JSXEmptyExpression') {
+ return '';
+ }
+ if (value?.type === 'Literal' && typeof value.value === 'string') {
+ return value.value;
+ }
+ if (value?.type === 'TemplateLiteral' && value.expressions.length === 0) {
+ return value.quasis[0].value.cooked;
+ }
+ throw new Error(
+ info.file +
+ ': use attributes/props rather than dynamic children inside <' +
+ tag +
+ '>',
+ );
+ })
+ .join('');
+ rawText =
+ tag === 'title' || tag === 'textarea'
+ ? escapeHtml(text)
+ : text.replace(new RegExp('' + tag, 'gi'), '<\\/' + tag);
+ }
+ let attrs = '';
+ const hasSpread = opening.attributes.some(
+ (attr: Node) => attr.type === 'JSXSpreadAttribute',
+ );
+ let marker: number | undefined;
+ const getMarker = () => {
+ if (marker === undefined) {
+ marker = key++;
+ attrs += ` data-vv-a="${marker}"`;
+ }
+ return marker;
+ };
+ for (const attr of opening.attributes) {
+ if (attr.type === 'JSXSpreadAttribute') {
+ bindings.push(`['spread',${getMarker()},'',${expr(attr.argument, shadow)}]`);
+ continue;
+ }
+ const name = tagName(attr.name);
+ if (name.startsWith('data-vv-') || name === 'dangerouslySetInnerHTML') {
+ throw new Error(`${info.file}: unsupported view attribute ${name}`);
+ }
+ if (/^on/i.test(name)) {
+ if (attr.value?.type !== 'JSXExpressionContainer') {
+ throw new Error(`${info.file}: ${name} must be an event callback`);
+ }
+ bindings.push(
+ `['event',${getMarker()},${JSON.stringify(name.slice(2).toLowerCase())},${expr(attr.value.expression, shadow)}]`,
+ );
+ } else if (!attr.value) {
+ if (hasSpread) {
+ bindings.push(`['attr',${getMarker()},${JSON.stringify(name)},true]`);
+ } else {
+ attrs += ` ${name}`;
+ }
+ } else if (attr.value.type === 'Literal') {
+ if (hasSpread) {
+ bindings.push(
+ `['attr',${getMarker()},${JSON.stringify(name)},${JSON.stringify(decodeHTML(attr.value.value))}]`,
+ );
+ } else {
+ attrs += ` ${name}="${escapeHtml(decodeHTML(attr.value.value))}"`;
+ }
+ } else {
+ bindings.push(
+ `['attr',${getMarker()},${JSON.stringify(name)},${expr(attr.value.expression, shadow)}]`,
+ );
+ }
+ }
+ return `<${tag}${attrs}>${voidTags.has(tag) ? '' : (rawText ?? n.children.map((item: Node) => child(item, tag)).join('')) + `${tag}>`}`;
+ }
+ const html = element(node);
+ component.templates.set(id, html);
+ return `__vv_template(${JSON.stringify(id)},${client ? 'null' : JSON.stringify(html)},[${bindings.join(',')}])`;
+ }
+ const parameter = fn.params[0];
+ const binding = parameter?.type === 'AssignmentPattern' ? parameter.left : parameter;
+ const params = binding ? source.slice(binding.start, binding.end) : '';
+ const fallback =
+ parameter?.type === 'AssignmentPattern'
+ ? source.slice(parameter.right.start, parameter.right.end)
+ : '{}';
+ const setup = params ? `const ${params} = __vv_props;` : '';
+ let body: string;
+ if (fn.body.type !== 'BlockStatement') {
+ body = `return () => ${expr(fn.body)};`;
+ } else {
+ const statements = fn.body.body as Array;
+ const last = statements.at(-1);
+ if (last?.type !== 'ReturnStatement' || !last.argument) {
+ throw new Error(`${info.file}: ${component.name} needs a final return expression`);
+ }
+ // Initialization runs once. Rendering executes only the final return expression.
+ for (const statement of statements.slice(0, -1)) {
+ walk(statement, (n) => {
+ if (
+ ['FunctionDeclaration', 'FunctionExpression', 'ArrowFunctionExpression'].includes(
+ n.type,
+ )
+ ) {
+ return false;
+ }
+ if (n.type === 'ReturnStatement') {
+ throw new Error(
+ `${info.file}: use conditional JSX in the final return instead of early component returns`,
+ );
+ }
+ });
+ }
+ body =
+ statements
+ .slice(0, -1)
+ .map((statement) => expr(statement, new Set(), false))
+ .join('\n') + `\nreturn () => (${expr(last.argument)});`;
+ }
+ // Replace parameters too: destructuring belongs inside the persistent instance closure.
+ if (fn.type === 'ArrowFunctionExpression') {
+ if (component.defaultStatementStart !== undefined) {
+ output.overwrite(component.defaultStatementStart, fn.start, `const ${component.name} = `);
+ output.append(`\nexport default ${component.name};\n`);
+ }
+ output.overwrite(
+ fn.start,
+ fn.end,
+ `(__vv_props = ${fallback}) => __vv_component(${JSON.stringify(component.id)},__vv_props,(__vv_change)=>{${setup}\n${body}},${component.dynamic})`,
+ );
+ } else {
+ output.overwrite(
+ fn.start,
+ fn.end,
+ `function ${component.name}(__vv_props = ${fallback}) { return __vv_component(${JSON.stringify(component.id)},__vv_props,(__vv_change)=>{${setup}\n${body}},${component.dynamic}); }`,
+ );
+ }
+ if (component.dynamic) {
+ output.append(`\n__vv_register(${JSON.stringify(component.id)},${component.name});\n`);
+ }
+ }
+ output.prepend(imports);
+ return {
+ code: output.toString(),
+ map: output.generateMap({ hires: true, source: info.file, includeContent: true }),
+ };
+}
diff --git a/packages/view/src/index.ts b/packages/view/src/index.ts
new file mode 100644
index 000000000..8cf34c513
--- /dev/null
+++ b/packages/view/src/index.ts
@@ -0,0 +1,15 @@
+export type ViewChild =
+ | string
+ | number
+ | boolean
+ | null
+ | undefined
+ | ViewResult
+ | Array;
+/** Opaque compiled HTML on the server, DOM on the client. */
+export interface ViewResult {
+ readonly __view: true;
+}
+export interface BaseProps {
+ children?: ViewChild;
+}
diff --git a/packages/view/src/inline.ts b/packages/view/src/inline.ts
new file mode 100644
index 000000000..d16264481
--- /dev/null
+++ b/packages/view/src/inline.ts
@@ -0,0 +1,322 @@
+import { readFileSync } from 'node:fs';
+import MagicString from 'magic-string';
+import { parseSync } from 'oxc-parser';
+import { transformWithOxc } from 'vite';
+import { bindingNames, compile, type AstNode, type ModuleInfo } from './compiler.ts';
+import { references } from './references.ts';
+import type { ClientUnit, ViewResources } from './resources.ts';
+
+interface SourceModule {
+ id: number;
+ file: string;
+ code: string;
+ declarations: Map }>;
+ imports: Map;
+ exports: Map;
+ stars: Array;
+ effects: Array<{ node: AstNode; code: string; order: number; names: Array }>;
+ sideEffects: Array;
+}
+/** Compile source fragments once. SSR later selects and concatenates only the required declarations. */
+export async function prepareClient(
+ views: Map,
+ eligible: Set,
+ resolve: (id: string, importer: string) => Promise,
+): Promise> {
+ if (!eligible.size) {
+ return { units: {}, componentUnits: {}, register: '' };
+ }
+ const modules = new Map();
+ const units: Record = {};
+ const componentUnits: Record = {};
+ async function load(file: string): Promise {
+ const cached = modules.get(file);
+ if (cached) {
+ return cached;
+ }
+ const view = views.get(file);
+ const input = view ? compile(view, true).code : readFileSync(file, 'utf8');
+ const transformed = await transformWithOxc(input, file, { target: 'esnext', sourcemap: false });
+ const code = transformed.code;
+ const ast = parseSync(file + '.js', code).program as unknown as AstNode;
+ const mod: SourceModule = {
+ id: modules.size,
+ file,
+ code,
+ declarations: new Map(),
+ imports: new Map(),
+ exports: new Map(),
+ stars: [],
+ effects: [],
+ sideEffects: [],
+ };
+ modules.set(file, mod);
+ function declaration(node: AstNode, order: number) {
+ if (node.type === 'VariableDeclaration') {
+ for (const [index, item] of node.declarations.entries()) {
+ const names = bindingNames(item.id);
+ const entry = {
+ node: { ...node, declarations: [item], start: item.start, end: item.end },
+ code: node.kind + ' ' + code.slice(item.start, item.end) + ';',
+ order: order + index / (node.declarations.length + 1),
+ names,
+ };
+ for (const name of names) {
+ mod.declarations.set(name, entry);
+ }
+ }
+ } else if (
+ (node.type === 'FunctionDeclaration' || node.type === 'ClassDeclaration') &&
+ node.id
+ ) {
+ mod.declarations.set(node.id.name, {
+ node,
+ code: code.slice(node.start, node.end),
+ order,
+ names: [node.id.name],
+ });
+ } else {
+ mod.effects.push({ node, code: code.slice(node.start, node.end), order, names: [] });
+ }
+ }
+ for (const [order, statement] of ast.body.entries()) {
+ if (statement.type === 'ImportDeclaration') {
+ if (
+ !statement.specifiers.length &&
+ !/\.(css|scss|sass|less)(?:\?|$)/.test(statement.source.value)
+ ) {
+ mod.sideEffects.push(statement.source.value);
+ }
+ for (const spec of statement.specifiers) {
+ mod.imports.set(spec.local.name, {
+ source: statement.source.value,
+ imported:
+ spec.type === 'ImportDefaultSpecifier'
+ ? 'default'
+ : spec.type === 'ImportNamespaceSpecifier'
+ ? '*'
+ : (spec.imported.name ?? spec.imported.value),
+ });
+ }
+ } else if (statement.type === 'ExportNamedDeclaration') {
+ if (statement.declaration) {
+ declaration(statement.declaration, order);
+ const names =
+ statement.declaration.type === 'VariableDeclaration'
+ ? statement.declaration.declarations.flatMap((item: AstNode) => bindingNames(item.id))
+ : [statement.declaration.id.name];
+ for (const name of names) {
+ mod.exports.set(name, name);
+ }
+ }
+ for (const spec of statement.specifiers) {
+ mod.exports.set(
+ spec.exported.name ?? spec.exported.value,
+ statement.source
+ ? { source: statement.source.value, imported: spec.local.name }
+ : spec.local.name,
+ );
+ }
+ } else if (statement.type === 'ExportAllDeclaration') {
+ mod.stars.push(statement.source.value);
+ } else if (statement.type === 'ExportDefaultDeclaration') {
+ const value = statement.declaration;
+ if (value.type === 'Identifier') {
+ mod.exports.set('default', value.name);
+ } else if (value.id && ['FunctionDeclaration', 'ClassDeclaration'].includes(value.type)) {
+ declaration(value, order);
+ mod.exports.set('default', value.id.name);
+ } else {
+ const name = '__vv_default';
+ mod.exports.set('default', name);
+ mod.declarations.set(name, {
+ node: value,
+ code: `const ${name} = ${code.slice(value.start, value.end)};`,
+ order,
+ names: [name],
+ });
+ }
+ } else if (
+ statement.type === 'ExpressionStatement' &&
+ statement.expression.type === 'CallExpression' &&
+ statement.expression.callee.name === '__vv_register'
+ ) {
+ // Registration is emitted by SSR only for the selected dynamic components.
+ } else {
+ declaration(statement, order);
+ }
+ }
+ return mod;
+ }
+ async function imported(source: string, mod: SourceModule) {
+ const id = await resolve(source, mod.file);
+ if (!id || id.startsWith('\0') || id.startsWith('node:')) {
+ throw new Error(`${mod.file}: cannot inline client import ${source}`);
+ }
+ return load(id.split('?')[0]!);
+ }
+ async function exported(
+ mod: SourceModule,
+ name: string,
+ seen = new Set(),
+ ): Promise<{ mod: SourceModule; name: string } | undefined> {
+ const key = `${mod.id}:${name}`;
+ if (seen.has(key)) {
+ return;
+ }
+ seen.add(key);
+ const entry = mod.exports.get(name);
+ if (typeof entry === 'string') {
+ if (mod.declarations.has(entry)) {
+ return { mod, name: entry };
+ }
+ const importedEntry = mod.imports.get(entry);
+ if (importedEntry) {
+ return exported(await imported(importedEntry.source, mod), importedEntry.imported, seen);
+ }
+ } else if (entry) {
+ return exported(await imported(entry.source, mod), entry.imported, seen);
+ }
+ for (const source of mod.stars) {
+ const found = await exported(await imported(source, mod), name, seen);
+ if (found) {
+ return found;
+ }
+ }
+ }
+ function symbol(mod: SourceModule, name: string): string {
+ return '__vv_m' + mod.id + '_' + name;
+ }
+ async function unit(mod: SourceModule, name: string): Promise {
+ const declaration = name.startsWith('#')
+ ? mod.effects[Number(name.slice(1))]
+ : mod.declarations.get(name);
+ if (!declaration) {
+ throw new Error(`${mod.file}: cannot inline client binding ${name}; use ESM exports`);
+ }
+ const id = `${mod.id}:${declaration.names[0] ?? name}`;
+ if (units[id]) {
+ return id;
+ }
+ const record: ClientUnit = {
+ module: mod.id,
+ order: declaration.order,
+ names: declaration.names.map((name) => symbol(mod, name)),
+ code: '',
+ dependencies: [],
+ };
+ units[id] = record;
+ const component = views.get(mod.file)?.components.find((component) => component.name === name);
+ if (component) {
+ componentUnits[component.id] = id;
+ }
+ const node = declaration.node;
+ const edits = new MagicString(mod.code.slice(node.start, node.end));
+ function replace(target: AstNode, text: string) {
+ edits.overwrite(target.start - node.start, target.end - node.start, text);
+ }
+ function binding(target: AstNode | null, shorthand = false) {
+ if (!target) {
+ return;
+ }
+ if (target.type === 'Identifier') {
+ replace(target, (shorthand ? target.name + ':' : '') + symbol(mod, target.name));
+ } else if (target.type === 'AssignmentPattern') {
+ binding(target.left, shorthand);
+ } else if (target.type === 'RestElement') {
+ binding(target.argument);
+ } else if (target.type === 'ArrayPattern') {
+ for (const item of target.elements) {
+ binding(item);
+ }
+ } else if (target.type === 'ObjectPattern') {
+ for (const property of target.properties) {
+ binding(
+ property.type === 'RestElement' ? property.argument : property.value,
+ property.shorthand,
+ );
+ }
+ }
+ }
+ if (node.type === 'VariableDeclaration') {
+ for (const item of node.declarations) {
+ binding(item.id);
+ }
+ } else if (node.type === 'FunctionDeclaration' || node.type === 'ClassDeclaration') {
+ binding(node.id);
+ }
+ for (const ref of references(node)) {
+ let target: { mod: SourceModule; name: string } | undefined;
+ let namespace = false;
+ if (mod.declarations.has(ref.name)) {
+ target = { mod, name: ref.name };
+ } else {
+ const entry = mod.imports.get(ref.name);
+ if (!entry) {
+ continue;
+ }
+ namespace = entry.imported === '*';
+ if (namespace && !ref.member) {
+ throw new Error(mod.file + ': use static namespace members in inline client code');
+ }
+ target = await exported(
+ await imported(entry.source, mod),
+ namespace ? ref.member! : entry.imported,
+ );
+ if (!target) {
+ throw new Error(mod.file + ': cannot resolve client import ' + ref.name);
+ }
+ }
+ record.dependencies.push(await unit(target.mod, target.name));
+ let replacement = symbol(target.mod, target.name);
+ if (!namespace && ref.member) {
+ replacement += (ref.node.optional ? '?.' : '.') + ref.member;
+ }
+ if (ref.shorthand) {
+ replacement = ref.name + ':' + replacement;
+ }
+ replace(ref.node, replacement);
+ }
+ const rewritten = edits.toString();
+ record.code =
+ node.type === 'VariableDeclaration'
+ ? node.kind + ' ' + rewritten + ';'
+ : declaration.names.length && !node.id
+ ? 'const ' + record.names[0] + '=' + rewritten + ';'
+ : rewritten;
+ // Preserve explicit module side effects, but never retain unused component declarations.
+ for (let index = 0; index < mod.effects.length; index++) {
+ const effect = '#' + index;
+ if (effect !== name) {
+ record.dependencies.push(await unit(mod, effect));
+ }
+ }
+ for (const source of mod.sideEffects) {
+ const target = await imported(source, mod);
+ for (let index = 0; index < target.effects.length; index++) {
+ record.dependencies.push(await unit(target, '#' + index));
+ }
+ }
+ return id;
+ }
+ for (const view of views.values()) {
+ for (const component of view.components) {
+ if (eligible.has(component.id)) {
+ componentUnits[component.id] = await unit(await load(view.file), component.name);
+ }
+ }
+ }
+ const runtimeFile = await resolve(
+ '@void/view/client',
+ [...views.keys()][0] ?? process.cwd() + '/index.ts',
+ );
+ if (!runtimeFile) {
+ throw new Error('Cannot resolve @void/view/client');
+ }
+ const runtime = await load(runtimeFile);
+ const register = await exported(runtime, 'register');
+ if (!register) {
+ throw new Error('Missing view client registration');
+ }
+ return { units, componentUnits, register: await unit(register.mod, register.name) };
+}
diff --git a/packages/view/src/jsx-runtime.ts b/packages/view/src/jsx-runtime.ts
new file mode 100644
index 000000000..828a6dc39
--- /dev/null
+++ b/packages/view/src/jsx-runtime.ts
@@ -0,0 +1,21 @@
+import type { ViewChild, ViewResult } from './index.ts';
+export namespace JSX {
+ export type Element = ViewResult;
+ export interface ElementChildrenAttribute {
+ children: unknown;
+ }
+ type Events = {
+ [K in keyof GlobalEventHandlersEventMap as `on${K}`]?: (
+ event: GlobalEventHandlersEventMap[K],
+ ) => unknown;
+ };
+ type Attributes = Events & {
+ children?: ViewChild;
+ class?: string;
+ style?: string;
+ [attribute: string]: unknown;
+ };
+ export type IntrinsicElements = { [K in keyof HTMLElementTagNameMap]: Attributes } & {
+ [K in keyof SVGElementTagNameMap]: Attributes;
+ };
+}
diff --git a/packages/view/src/plugin.ts b/packages/view/src/plugin.ts
new file mode 100644
index 000000000..ac1e4c6ef
--- /dev/null
+++ b/packages/view/src/plugin.ts
@@ -0,0 +1,349 @@
+import { readFileSync, readdirSync } from 'node:fs';
+import { resolve, relative } from 'pathslash';
+import type { Plugin } from 'vite';
+import { createPagesPlugin, generateComponentManifest, resolvePagesDir } from 'void/pages';
+import { analyze, compile, type ComponentInfo, type ModuleInfo } from './compiler.ts';
+import { prepareClient } from './inline.ts';
+import type { ViewResources } from './resources.ts';
+
+export interface ViewOptions {
+ root?: string;
+}
+/** Compile TSX views and render Void Pages as documents with optional custom elements. */
+export function voidView(options: ViewOptions = {}): Array {
+ let root = resolve(options.root ?? process.cwd());
+ let modules = new Map();
+ let graphFiles = new Set();
+ let resources: ViewResources;
+ let cssImports: Array = [];
+ const ignored = new Set(['node_modules', '.git', '.void', 'dist', '.output', '.wrangler']);
+ function scan(directory: string, files: Array = []): Array {
+ for (const entry of readdirSync(directory, { withFileTypes: true })) {
+ if (ignored.has(entry.name) || entry.name.startsWith('.')) {
+ continue;
+ }
+ const path = resolve(directory, entry.name);
+ if (entry.isDirectory()) {
+ scan(path, files);
+ } else if (/\.[jt]sx$/.test(path)) {
+ files.push(path);
+ }
+ }
+ return files;
+ }
+ async function refresh(
+ resolveImport: (id: string, importer: string) => Promise,
+ ) {
+ const next = new Map(
+ scan(root).map((file) => [
+ file,
+ analyze(readFileSync(file, 'utf8'), file, relative(root, file).replaceAll('\\', '/')),
+ ]),
+ );
+ // Readable names for the usual case; disambiguate same-named components by source identity.
+ const counts = new Map();
+ for (const info of next.values()) {
+ for (const component of info.components) {
+ const name = component.id.replace(/-[a-f0-9]{8}$/, '');
+ counts.set(name, (counts.get(name) ?? 0) + 1);
+ }
+ }
+ for (const info of next.values()) {
+ for (const component of info.components) {
+ const name = component.id.replace(/-[a-f0-9]{8}$/, '');
+ if (counts.get(name) === 1) {
+ component.id = name;
+ }
+ }
+ }
+ const metadata = new Map(next);
+ async function imported(source: string, importer: string): Promise {
+ const id = (await resolveImport(source, importer))?.split('?')[0];
+ if (!id || !/\.[cm]?[jt]sx?$/.test(id)) {
+ return;
+ }
+ let info = metadata.get(id);
+ if (!info) {
+ info = analyze(readFileSync(id, 'utf8'), id, relative(root, id));
+ metadata.set(id, info);
+ if (/\.[jt]sx$/.test(id)) {
+ next.set(id, info);
+ }
+ }
+ return info;
+ }
+ type Symbol = { module: ModuleInfo; local: string };
+ async function symbol(
+ info: ModuleInfo,
+ name: string,
+ exported = false,
+ seen = new Set(),
+ ): Promise {
+ const key = info.file + ':' + name + ':' + exported;
+ if (seen.has(key)) {
+ return;
+ }
+ seen.add(key);
+ if (exported) {
+ const local = info.exports.get(name);
+ if (local) {
+ return symbol(info, local, false, seen);
+ }
+ for (const source of info.stars) {
+ const target = await imported(source, info.file);
+ const found = target && (await symbol(target, name, true, seen));
+ if (found) {
+ return found;
+ }
+ }
+ return;
+ }
+ if (
+ info.functions.has(name) ||
+ info.components.some((component) => component.name === name)
+ ) {
+ return { module: info, local: name };
+ }
+ const [base, member] = name.split('.');
+ const entry = info.imports.get(base!);
+ if (!entry) {
+ return;
+ }
+ const target = await imported(entry.source, info.file);
+ return target
+ ? symbol(target, entry.imported === '*' ? member! : entry.imported, true, seen)
+ : undefined;
+ }
+ const targets = new Map>();
+ const calls: Array<{
+ parent: ComponentInfo;
+ target: ComponentInfo;
+ prop: string;
+ binding?: string;
+ known: boolean;
+ }> = [];
+ for (const info of next.values()) {
+ for (const component of info.components) {
+ const dependencies: Array = [];
+ const named = new Map();
+ for (const name of component.dependencies) {
+ const resolved = await symbol(info, name);
+ const target = resolved?.module.components.find(
+ (component) => component.name === resolved.local,
+ );
+ if (!target || !next.has(resolved!.module.file)) {
+ throw new Error(info.file + ': cannot resolve TSX view component ' + name);
+ }
+ dependencies.push(target);
+ named.set(name, target);
+ }
+ for (const argument of component.callbackArguments) {
+ const resolved = argument.value && (await symbol(info, argument.value));
+ calls.push({
+ parent: component,
+ target: named.get(argument.tag)!,
+ prop: argument.name,
+ binding: argument.value && component.propBindings.get(argument.value),
+ known: argument.known || !!(resolved && resolved.module.functions.has(resolved.local)),
+ });
+ }
+ targets.set(component, dependencies);
+ }
+ }
+ // Callback props propagate through forwarding components, regardless of source-file order.
+ let changed = true;
+ while (changed) {
+ changed = false;
+ for (const call of calls) {
+ if (
+ !call.known &&
+ !(
+ call.binding &&
+ (call.parent.callbackProps.has(call.binding) || call.parent.callbackProps.has('*'))
+ )
+ ) {
+ continue;
+ }
+ call.parent.dynamic = call.target.dynamic = true;
+ if (!call.target.callbackProps.has(call.prop)) {
+ call.target.callbackProps.add(call.prop);
+ changed = true;
+ }
+ }
+ }
+ const needed = new Set();
+ function include(component: ComponentInfo) {
+ if (needed.has(component)) {
+ return;
+ }
+ needed.add(component);
+ for (const target of targets.get(component) ?? []) {
+ include(target);
+ }
+ }
+ for (const info of next.values()) {
+ for (const component of info.components) {
+ if (component.dynamic) {
+ include(component);
+ }
+ }
+ }
+ for (const info of next.values()) {
+ compile(info, false);
+ }
+ resources = {
+ components: Object.fromEntries(
+ [...next.values()].flatMap((info) =>
+ info.components.map((component) => [
+ component.id,
+ {
+ dynamic: component.dynamic,
+ dependencies: (targets.get(component) ?? []).map((target) => target.id),
+ templates: [...component.templates],
+ },
+ ]),
+ ),
+ ),
+ ...(await prepareClient(
+ next,
+ new Set([...needed].map((component) => component.id)),
+ resolveImport,
+ )),
+ };
+ // Include component calls reached through ordinary JS helpers as well as JSX tags.
+ const sourceComponents = new Map(
+ Object.entries(resources.componentUnits).map(([component, unit]) => [unit, component]),
+ );
+ for (const [id, entry] of Object.entries(resources.componentUnits)) {
+ const dependencies = new Set(resources.components[id]!.dependencies);
+ const visited = new Set();
+ function visit(unit: string) {
+ if (visited.has(unit)) {
+ return;
+ }
+ visited.add(unit);
+ const component = sourceComponents.get(unit);
+ if (component && component !== id) {
+ dependencies.add(component);
+ return;
+ }
+ for (const dependency of resources.units[unit]!.dependencies) {
+ visit(dependency);
+ }
+ }
+ visit(entry);
+ resources.components[id]!.dependencies = [...dependencies];
+ }
+ cssImports = [];
+ for (const info of next.values()) {
+ for (const statement of info.program.body) {
+ if (
+ statement.type === 'ImportDeclaration' &&
+ /\.(css|scss|sass|less)(?:\?|$)/.test(statement.source.value)
+ ) {
+ const css = await resolveImport(statement.source.value, info.file);
+ if (css) {
+ cssImports.push(css);
+ }
+ }
+ }
+ }
+ cssImports = [...new Set(cssImports)];
+ modules = next;
+ graphFiles = new Set(metadata.keys());
+ }
+ const compiler: Plugin = {
+ name: 'void:view:compiler',
+ enforce: 'pre',
+ config() {
+ return { optimizeDeps: { exclude: ['@void/view'] }, ssr: { noExternal: ['@void/view'] } };
+ },
+ configResolved(resolved) {
+ root = resolve(options.root ?? resolved.root);
+ },
+ async buildStart() {
+ await refresh(async (id, importer) => (await this.resolve(id, importer))?.id);
+ for (const path of graphFiles) {
+ this.addWatchFile(path);
+ }
+ },
+ configureServer(server) {
+ const restart = (file: string) => {
+ const path = relative(root, file);
+ if (
+ !server.httpServer?.listening ||
+ path.startsWith('..') ||
+ !/\.[jt]sx$/.test(path) ||
+ path.split('/').some((part) => ignored.has(part) || part.startsWith('.'))
+ ) {
+ return;
+ }
+ void server.restart();
+ };
+ server.watcher.on('add', restart);
+ server.watcher.on('unlink', restart);
+ },
+ transform(code, id, options) {
+ const info = modules.get(id.split('?')[0]!);
+ if (!info) {
+ return;
+ }
+ // A fresh graph is built by buildStart and on source edits.
+ if (code !== info.source) {
+ throw new Error(`@void/view: source changed during compilation: ${id}`);
+ }
+ return compile(info, !options?.ssr);
+ },
+ async handleHotUpdate(context) {
+ if (!graphFiles.has(context.file)) {
+ return;
+ }
+ await refresh(
+ async (id, importer) =>
+ (await context.server.environments.client.pluginContainer.resolveId(id, importer))?.id,
+ );
+ for (const environment of Object.values(context.server.environments)) {
+ environment.moduleGraph.invalidateAll();
+ }
+ context.server.ws.send({ type: 'full-reload' });
+ return [];
+ },
+ };
+ const pages = createPagesPlugin(resolvePagesDir(root), {
+ name: 'void:view:pages',
+ root,
+ config() {
+ return {
+ build: {
+ manifest: true,
+ rollupOptions: { input: { 'pages-client': 'virtual:void-pages-client' } },
+ },
+ };
+ },
+ generateClient() {
+ return cssImports.map((file) => `import ${JSON.stringify(file)};`).join('\n');
+ },
+ generateRenderer(pageScan) {
+ return `
+import { render, renderResources } from '@void/view/server';
+import { renderHeadToString, renderHtmlAttrs, renderBodyAttrs } from 'void/pages-head';
+const resources = ${JSON.stringify(resources)};
+${generateComponentManifest(pageScan, resolvePagesDir(root))}
+export async function renderPage(page, assets) {
+ const Page = (await components[page.component]()).default;
+ let content = Page(page.props ?? {});
+ const layouts = layoutTree[page.component] ?? [];
+ for (let i = layouts.length - 1; i >= 0; i--) {
+ const Layout = (await components[layouts[i]]()).default;
+ content = Layout({ children: content });
+ }
+ return '' +
+ (page.head ? renderHeadToString(page.head) : '') + assets.css + assets.preloads +
+ '' + render(content) +
+ (page.documentOnly ? '' : renderResources(content.components, resources)) + '';
+}
+`;
+ },
+ });
+ return [compiler, pages];
+}
diff --git a/packages/view/src/references.ts b/packages/view/src/references.ts
new file mode 100644
index 000000000..7b7bad7a8
--- /dev/null
+++ b/packages/view/src/references.ts
@@ -0,0 +1,207 @@
+import type { AstNode } from './compiler.ts';
+
+export interface Reference {
+ node: AstNode;
+ name: string;
+ member?: string;
+ shorthand?: boolean;
+}
+function names(node: AstNode | null | undefined): Array {
+ if (!node) {
+ return [];
+ }
+ if (node.type === 'Identifier') {
+ return [node.name];
+ }
+ if (node.type === 'RestElement') {
+ return names(node.argument);
+ }
+ if (node.type === 'AssignmentPattern') {
+ return names(node.left);
+ }
+ if (node.type === 'ArrayPattern') {
+ return node.elements.flatMap(names);
+ }
+ if (node.type === 'ObjectPattern') {
+ return node.properties.flatMap((property: AstNode) =>
+ names(property.type === 'RestElement' ? property.argument : property.value),
+ );
+ }
+ return [];
+}
+function nodes(node: AstNode): Array {
+ return Object.values(node)
+ .flatMap((value) => (Array.isArray(value) ? value : [value]))
+ .filter((value) => value && typeof value === 'object' && typeof value.type === 'string');
+}
+/** Free references, retaining ranges for rewriting imported bindings without touching shadowed locals. */
+export function references(node: AstNode): Array {
+ const result: Array = [];
+ const root = node;
+ function pattern(node: AstNode | null, scope: Set) {
+ if (!node) {
+ return;
+ }
+ if (node.type === 'AssignmentPattern') {
+ pattern(node.left, scope);
+ visit(node.right, scope);
+ } else if (node.type === 'ObjectPattern') {
+ for (const property of node.properties) {
+ if (property.computed) {
+ visit(property.key, scope);
+ }
+ pattern(property.type === 'RestElement' ? property.argument : property.value, scope);
+ }
+ } else if (node.type === 'ArrayPattern') {
+ for (const item of node.elements) {
+ pattern(item, scope);
+ }
+ } else if (node.type === 'RestElement') {
+ pattern(node.argument, scope);
+ }
+ }
+ function visit(node: AstNode | null | undefined, scope: Set, shorthand = false) {
+ if (!node) {
+ return;
+ }
+ if (node.type === 'Identifier') {
+ if (!scope.has(node.name)) {
+ result.push({ node, name: node.name, shorthand });
+ }
+ return;
+ }
+ if (
+ ['FunctionDeclaration', 'FunctionExpression', 'ArrowFunctionExpression'].includes(node.type)
+ ) {
+ const inner = new Set([
+ ...scope,
+ ...(node === root && node.type === 'FunctionDeclaration' ? [] : names(node.id)),
+ ...node.params.flatMap(names),
+ ]);
+ // var is function-scoped even when declared inside a nested block.
+ function vars(current: AstNode) {
+ if (
+ current !== node &&
+ ['FunctionDeclaration', 'FunctionExpression', 'ArrowFunctionExpression'].includes(
+ current.type,
+ )
+ ) {
+ return;
+ }
+ if (current.type === 'VariableDeclaration' && current.kind === 'var') {
+ for (const item of current.declarations) {
+ for (const name of names(item.id)) {
+ inner.add(name);
+ }
+ }
+ }
+ for (const child of nodes(current)) {
+ vars(child);
+ }
+ }
+ vars(node);
+ for (const param of node.params) {
+ pattern(param, inner);
+ }
+ visit(node.body, inner);
+ return;
+ }
+ if (node.type === 'BlockStatement' || node.type === 'Program') {
+ const inner = new Set(scope);
+ for (const statement of node.body) {
+ if (statement.type === 'VariableDeclaration') {
+ for (const item of statement.declarations) {
+ for (const name of names(item.id)) {
+ inner.add(name);
+ }
+ }
+ }
+ if (statement.type === 'FunctionDeclaration' || statement.type === 'ClassDeclaration') {
+ for (const name of names(statement.id)) {
+ inner.add(name);
+ }
+ }
+ }
+ for (const statement of node.body) {
+ visit(statement, inner);
+ }
+ return;
+ }
+ if (node.type === 'VariableDeclaration') {
+ for (const item of node.declarations) {
+ pattern(item.id, scope);
+ visit(item.init, scope);
+ }
+ return;
+ }
+ if (
+ node.type === 'MemberExpression' &&
+ !node.computed &&
+ node.object.type === 'Identifier' &&
+ !scope.has(node.object.name)
+ ) {
+ result.push({ node, name: node.object.name, member: node.property.name });
+ return;
+ }
+ if (node.type === 'MemberExpression') {
+ visit(node.object, scope);
+ if (node.computed) {
+ visit(node.property, scope);
+ }
+ return;
+ }
+ if (
+ node.type === 'Property' ||
+ node.type === 'MethodDefinition' ||
+ node.type === 'PropertyDefinition'
+ ) {
+ if (node.computed) {
+ visit(node.key, scope);
+ }
+ visit(node.value, scope, node.shorthand);
+ return;
+ }
+ if (node.type === 'ClassDeclaration' || node.type === 'ClassExpression') {
+ visit(node.superClass, scope);
+ visit(
+ node.body,
+ new Set([
+ ...scope,
+ ...(node === root && node.type === 'ClassDeclaration' ? [] : names(node.id)),
+ ]),
+ );
+ return;
+ }
+ if (node.type === 'CatchClause') {
+ visit(node.body, new Set([...scope, ...names(node.param)]));
+ return;
+ }
+ if (['ForStatement', 'ForInStatement', 'ForOfStatement'].includes(node.type)) {
+ const binding = node.init ?? node.left;
+ const inner = new Set(scope);
+ if (binding?.type === 'VariableDeclaration') {
+ for (const item of binding.declarations) {
+ for (const name of names(item.id)) {
+ inner.add(name);
+ }
+ }
+ }
+ for (const child of nodes(node)) {
+ visit(child, inner);
+ }
+ return;
+ }
+ if (['BreakStatement', 'ContinueStatement', 'MetaProperty'].includes(node.type)) {
+ return;
+ }
+ if (node.type === 'LabeledStatement') {
+ visit(node.body, scope);
+ return;
+ }
+ for (const child of nodes(node)) {
+ visit(child, scope);
+ }
+ }
+ visit(node, new Set());
+ return result;
+}
diff --git a/packages/view/src/resources.ts b/packages/view/src/resources.ts
new file mode 100644
index 000000000..fb9706a78
--- /dev/null
+++ b/packages/view/src/resources.ts
@@ -0,0 +1,102 @@
+import { escapeHtml } from './shared.ts';
+
+export interface ClientUnit {
+ module: number;
+ order: number;
+ names: Array;
+ code: string;
+ dependencies: Array;
+}
+export interface ViewResources {
+ components: Record<
+ string,
+ { dynamic: boolean; dependencies: Array; templates: Array<[string, string]> }
+ >;
+ units: Record;
+ componentUnits: Record;
+ register: string;
+}
+/** Worker-safe assembly of request-specific resources. No module registry or request state is shared. */
+export function renderResources(roots: Iterable, resources: ViewResources): string {
+ const selected = new Set();
+ function include(id: string) {
+ if (selected.has(id)) {
+ return;
+ }
+ const component = resources.components[id];
+ if (!component) {
+ throw new Error(`Missing view resources: ${id}`);
+ }
+ selected.add(id);
+ for (const dependency of component.dependencies) {
+ include(dependency);
+ }
+ }
+ for (const root of roots) {
+ include(root);
+ }
+ if (!selected.size) {
+ return '';
+ }
+ const wanted = new Set();
+ const modules = new Map>();
+ function use(id: string) {
+ if (wanted.has(id)) {
+ return;
+ }
+ const unit = resources.units[id];
+ if (!unit) {
+ throw new Error(`Missing view client source: ${id}`);
+ }
+ wanted.add(id);
+ if (!modules.has(unit.module)) {
+ modules.set(unit.module, new Map());
+ }
+ modules.get(unit.module)!.set(id, unit);
+ for (const dependency of unit.dependencies) {
+ use(dependency);
+ }
+ }
+ for (const id of selected) {
+ use(resources.componentUnits[id]!);
+ }
+ use(resources.register);
+ let code = '';
+ // Preserve module initialization order; function declarations remain naturally hoisted.
+ const initialized = new Set();
+ function initialize(id: number) {
+ if (initialized.has(id)) {
+ return;
+ }
+ initialized.add(id);
+ for (const unit of modules.get(id)!.values()) {
+ for (const dependency of unit.dependencies) {
+ const target = resources.units[dependency]!.module;
+ if (target !== id) {
+ initialize(target);
+ }
+ }
+ }
+ code +=
+ [...modules.get(id)!.values()]
+ .sort((a, b) => a.order - b.order)
+ .map((unit) => unit.code)
+ .join('\n') + '\n';
+ }
+ for (const id of modules.keys()) {
+ initialize(id);
+ }
+ const registration = resources.units[resources.register]!;
+ for (const id of selected) {
+ if (resources.components[id]!.dynamic) {
+ const unit = resources.units[resources.componentUnits[id]!]!;
+ code += `${registration.names[0]}(${JSON.stringify(id)},${unit.names[0]});`;
+ }
+ }
+ const templates = [...selected]
+ .flatMap((id) => resources.components[id]!.templates)
+ .map(([id, html]) => `${html}`)
+ .join('');
+ // Escape the HTML raw-text terminator even when it appears inside a JS string or comment.
+ return `${templates}`;
+}
diff --git a/packages/view/src/server.ts b/packages/view/src/server.ts
new file mode 100644
index 000000000..c5561daff
--- /dev/null
+++ b/packages/view/src/server.ts
@@ -0,0 +1,144 @@
+export { renderResources } from './resources.ts';
+import type { ViewResult } from './index.ts';
+import { attribute, escapeHtml, type Binding, type Initialize, type Encoded } from './shared.ts';
+export class Html implements ViewResult {
+ readonly __view = true;
+ constructor(
+ readonly value: string,
+ readonly components: ReadonlySet = new Set(),
+ ) {}
+}
+export function render(value: unknown): string {
+ if (value == null || typeof value === 'boolean') {
+ return '';
+ }
+ if (value instanceof Html) {
+ return value.value;
+ }
+ if (Array.isArray(value)) {
+ return value.map(render).join('');
+ }
+ if (typeof value === 'string' || typeof value === 'number') {
+ return escapeHtml(value);
+ }
+ throw new Error('Views can render only compiled HTML, scalars, and arrays');
+}
+function collect(value: unknown, components: Set, seen = new Set
+ );
+}
diff --git a/playground/view/components/greeting.ts b/playground/view/components/greeting.ts
new file mode 100644
index 000000000..1c2804db4
--- /dev/null
+++ b/playground/view/components/greeting.ts
@@ -0,0 +1,3 @@
+export function greeting() {
+ return 'Imported callback';
+}
diff --git a/playground/view/components/index.ts b/playground/view/components/index.ts
new file mode 100644
index 000000000..ad7fcd510
--- /dev/null
+++ b/playground/view/components/index.ts
@@ -0,0 +1 @@
+export { Details } from './Details.tsx';
diff --git a/playground/view/package.json b/playground/view/package.json
new file mode 100644
index 000000000..1d6751c78
--- /dev/null
+++ b/playground/view/package.json
@@ -0,0 +1,21 @@
+{
+ "name": "playground-view",
+ "private": true,
+ "type": "module",
+ "scripts": {
+ "build": "vp build",
+ "dev": "vp dev",
+ "preview": "node dist/ssr/index.js",
+ "typecheck": "tsc --noEmit"
+ },
+ "dependencies": {
+ "@hono/node-server": "catalog:",
+ "@void/view": "workspace:*",
+ "void": "workspace:*"
+ },
+ "devDependencies": {
+ "typescript": "catalog:",
+ "vite": "catalog:",
+ "vite-plus": "catalog:"
+ }
+}
diff --git a/playground/view/pages/about.tsx b/playground/view/pages/about.tsx
new file mode 100644
index 000000000..a73bef852
--- /dev/null
+++ b/playground/view/pages/about.tsx
@@ -0,0 +1,9 @@
+export default function About() {
+ return (
+
+ A static page
+ This page ships ordinary HTML, with no View templates or component JavaScript.
+ Back to the playground
+
+ );
+}
diff --git a/playground/view/pages/button.tsx b/playground/view/pages/button.tsx
new file mode 100644
index 000000000..56e97694f
--- /dev/null
+++ b/playground/view/pages/button.tsx
@@ -0,0 +1,13 @@
+import { Button } from '../components/Button.tsx';
+
+export default function ButtonPage() {
+ return (
+
+ Just one button
+
+
+ All examples
+
+
+ );
+}
diff --git a/playground/view/pages/conditional.server.ts b/playground/view/pages/conditional.server.ts
new file mode 100644
index 000000000..1b503e4b7
--- /dev/null
+++ b/playground/view/pages/conditional.server.ts
@@ -0,0 +1,4 @@
+import { defineHandler } from 'void';
+export const loader = defineHandler((context) => ({
+ enabled: context.req.query('enabled') === '1',
+}));
diff --git a/playground/view/pages/conditional.tsx b/playground/view/pages/conditional.tsx
new file mode 100644
index 000000000..c02acf0ba
--- /dev/null
+++ b/playground/view/pages/conditional.tsx
@@ -0,0 +1,10 @@
+import { Reveal } from '../components/Reveal.tsx';
+export default function Conditional({ enabled }: { enabled: boolean }) {
+ return (
+
+ Server-selected interaction
+ {enabled ? : No interactive component was rendered.
}
+ Enable the interactive example
+
+ );
+}
diff --git a/playground/view/pages/index.tsx b/playground/view/pages/index.tsx
new file mode 100644
index 000000000..e2b3f3885
--- /dev/null
+++ b/playground/view/pages/index.tsx
@@ -0,0 +1,34 @@
+import { Button } from '../components/Button.tsx';
+import { Counter } from '../components/Counter.tsx';
+import * as Examples from '../components/index.ts';
+import './style.css';
+function Intro() {
+ return (
+
+ Server-rendered HTML. Small custom elements only where interaction is needed.
+
+ );
+}
+export default function Home() {
+ return (
+
+ Void View
+
+
+ The Button example
+
+
+
+
+
+
+
+ Visit a static page
+ {' · '}
+ Just one button
+ {' · '}
+ Server-selected interaction
+
+
+ );
+}
diff --git a/playground/view/pages/style.css b/playground/view/pages/style.css
new file mode 100644
index 000000000..15cedd65d
--- /dev/null
+++ b/playground/view/pages/style.css
@@ -0,0 +1,52 @@
+:root {
+ font:
+ 17px/1.6 system-ui,
+ sans-serif;
+ color: #24352c;
+ background: #f2f5ef;
+}
+body {
+ margin: 0;
+}
+main {
+ max-width: 850px;
+ padding: 48px 24px;
+ margin: auto;
+}
+h1 {
+ font-size: 48px;
+ letter-spacing: -2px;
+ margin-bottom: 8px;
+}
+h2 {
+ font-size: 21px;
+}
+.intro {
+ color: #546558;
+}
+.card {
+ background: white;
+ border: 1px solid #d6dfd1;
+ border-radius: 12px;
+ padding: 24px;
+ margin: 24px 0;
+}
+button {
+ padding: 10px 16px;
+ margin: 4px;
+ border: 1px solid #809b83;
+ border-radius: 6px;
+ background: #e9f3e4;
+ color: inherit;
+ cursor: pointer;
+ font: inherit;
+}
+.label {
+ font-weight: bold;
+}
+a {
+ color: #315d38;
+}
+.milestone {
+ color: #31794b;
+}
diff --git a/playground/view/tsconfig.json b/playground/view/tsconfig.json
new file mode 100644
index 000000000..48cd1b4d5
--- /dev/null
+++ b/playground/view/tsconfig.json
@@ -0,0 +1,15 @@
+{
+ "compilerOptions": {
+ "target": "ES2022",
+ "lib": ["ES2022", "DOM"],
+ "module": "NodeNext",
+ "moduleResolution": "NodeNext",
+ "strict": true,
+ "skipLibCheck": true,
+ "jsx": "preserve",
+ "jsxImportSource": "@void/view",
+ "allowImportingTsExtensions": true,
+ "types": ["vite/client"]
+ },
+ "include": ["pages", "components"]
+}
diff --git a/playground/view/vite.config.ts b/playground/view/vite.config.ts
new file mode 100644
index 000000000..996f93f03
--- /dev/null
+++ b/playground/view/vite.config.ts
@@ -0,0 +1,4 @@
+import { voidView } from '@void/view/plugin';
+import { defineConfig } from 'vite-plus';
+import { voidPlugin } from 'void';
+export default defineConfig({ plugins: [voidPlugin(), voidView()] });
diff --git a/playground/view/void.config.ts b/playground/view/void.config.ts
new file mode 100644
index 000000000..dee001747
--- /dev/null
+++ b/playground/view/void.config.ts
@@ -0,0 +1,2 @@
+import { defineConfig } from 'void/config';
+export default defineConfig({ target: 'node', head: { title: 'Void View playground' } });
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 0e2591bd2..fd516c93f 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -754,6 +754,37 @@ importers:
specifier: workspace:*
version: link:../void
+ packages/view:
+ dependencies:
+ entities:
+ specifier: ^9.0.0
+ version: 9.0.0
+ magic-string:
+ specifier: 'catalog:'
+ version: 1.4.5
+ oxc-parser:
+ specifier: ^0.156.0
+ version: 0.156.0
+ pathslash:
+ specifier: 'catalog:'
+ version: 0.1.0
+ devDependencies:
+ '@types/node':
+ specifier: 'catalog:'
+ version: 26.6.6
+ typescript:
+ specifier: 'catalog:'
+ version: 7.0.2
+ vite:
+ specifier: 'catalog:'
+ version: '@voidzero-dev/vite-plus-core@1.6.0(@types/node@26.6.6)(esbuild@0.28.2)(jiti@2.7.0)(sass-embedded@1.104.1)(sass@1.104.1)(terser@5.51.2)(tsx@4.23.15)(typescript@7.0.2)(yaml@2.9.1)'
+ vite-plus:
+ specifier: 'catalog:'
+ version: 1.6.0(@astrojs/compiler-rs@0.5.3)(@types/node@26.6.6)(@vitest/ui@5.1.0)(esbuild@0.28.2)(happy-dom@21.0.4)(jiti@2.7.0)(playwright@1.65.0)(sass-embedded@1.104.1)(sass@1.104.1)(svelte@5.57.2)(terser@5.51.2)(tsx@4.23.15)(typescript@7.0.2)(yaml@2.9.1)
+ void:
+ specifier: workspace:*
+ version: link:../void
+
packages/void:
dependencies:
'@cloudflare/build-output-utils':
@@ -1966,6 +1997,28 @@ importers:
specifier: workspace:*
version: link:../../packages/void
+ playground/view:
+ dependencies:
+ '@hono/node-server':
+ specifier: 'catalog:'
+ version: 2.1.6(hono@4.13.18)
+ '@void/view':
+ specifier: workspace:*
+ version: link:../../packages/view
+ void:
+ specifier: workspace:*
+ version: link:../../packages/void
+ devDependencies:
+ typescript:
+ specifier: 'catalog:'
+ version: 7.0.2
+ vite:
+ specifier: 'catalog:'
+ version: '@voidzero-dev/vite-plus-core@1.6.0(@types/node@26.6.6)(esbuild@0.28.2)(jiti@2.7.0)(sass-embedded@1.104.1)(sass@1.104.1)(terser@5.51.2)(tsx@4.23.15)(typescript@7.0.2)(yaml@2.9.1)'
+ vite-plus:
+ specifier: 'catalog:'
+ version: 1.6.0(@astrojs/compiler-rs@0.5.3)(@types/node@26.6.6)(@vitest/ui@5.1.0)(esbuild@0.28.2)(happy-dom@21.0.4)(jiti@2.7.0)(playwright@1.65.0)(sass-embedded@1.104.1)(sass@1.104.1)(svelte@5.57.2)(terser@5.51.2)(tsx@4.23.15)(typescript@7.0.2)(yaml@2.9.1)
+
playground/vinext-app:
dependencies:
'@vitejs/plugin-rsc':
diff --git a/vite.config.ts b/vite.config.ts
index a43b193a5..a3bd9595f 100644
--- a/vite.config.ts
+++ b/vite.config.ts
@@ -160,6 +160,10 @@ export default defineConfig({
'.worktrees/**',
],
overrides: [
+ {
+ files: ['playground/view/**/*.tsx'],
+ rules: { 'react/jsx-key': 'off' }, // View replaces HTML; it has no keyed virtual DOM.
+ },
{
files: ['**/*.ts', '**/*.tsx', '**/*.mts', '**/*.cts'],
rules: {
@@ -572,6 +576,7 @@ export default defineConfig({
'packages/vue/test/integration/**/*.test.ts',
'packages/svelte/test/integration/**/*.test.ts',
'packages/solid/test/integration/**/*.test.ts',
+ 'packages/view/test/integration/**/*.test.ts',
],
maxWorkers,
},
From 12833b9d33347d37ed29e57816fe08a7fb5a2580 Mon Sep 17 00:00:00 2001
From: Sasha Koss
Date: Sat, 14 Nov 2026 21:01:57 +0800
Subject: [PATCH 2/3] chore: add ; }',
'/Styled.tsx',
);
compile(info, false);
+ const scope = info.components[0]!.scope;
expect([...info.components[0]!.templates.values()]).toEqual([
- '',
+ ``,
]);
});
it.each([
diff --git a/packages/view/test/unit/styles.test.ts b/packages/view/test/unit/styles.test.ts
new file mode 100644
index 000000000..7e5cf8674
--- /dev/null
+++ b/packages/view/test/unit/styles.test.ts
@@ -0,0 +1,57 @@
+import { describe, expect, it } from 'vite-plus/test';
+import { analyze, compile } from '../../src/compiler.ts';
+import { scopeStyles } from '../../src/styles.ts';
+
+const scope = 'data-vv-cid-test';
+describe('component styles', () => {
+ it('scopes selector lists, compounds, combinators, and pseudo-elements', () => {
+ const css = scopeStyles('h2, .box > h2:hover::before { color: red }', scope, 'component.css');
+ expect(css).toContain(`h2[${scope}]`);
+ expect(css).toContain(`.box[${scope}]>h2:hover[${scope}]:before`);
+ expect(css).not.toContain(`:before[${scope}]`);
+ });
+ it('treats both root forms as global and keeps adjacent rules local', () => {
+ const css = scopeStyles(
+ ':root { .nested { color: red } } :root .flat { color: blue } h2 { color: green }',
+ scope,
+ 'component.css',
+ );
+ expect(css).toContain(':root .nested{');
+ expect(css).toContain(':root .flat{');
+ expect(css).not.toContain(`.nested[${scope}]`);
+ expect(css).not.toContain(`.flat[${scope}]`);
+ expect(css).toContain(`h2[${scope}]`);
+ });
+ it('handles nesting and media rules without modifying keyframe selectors', () => {
+ const css = scopeStyles(
+ '@media (min-width: 1px) { .box { & > h2 { color: red } } :root { .global { color: blue } } } @keyframes fade { from { opacity: 0 } to { opacity: 1 } }',
+ scope,
+ 'component.css',
+ );
+ expect(css).toContain(`.box[${scope}]>h2[${scope}]`);
+ expect(css).toContain(':root .global{');
+ expect(css).toContain('@keyframes fade{');
+ expect(css.slice(css.indexOf('@keyframes'))).not.toContain(scope);
+ });
+ it('uses stable, distinct scopes and the same templates on server and client', () => {
+ const source =
+ 'export function Panel(){ return <>Title
>; }';
+ const a = analyze(source, '/one.tsx');
+ const b = analyze(source, '/two.tsx');
+ expect(a.components[0]!.scope).not.toBe(b.components[0]!.scope);
+ compile(a, false);
+ const html = [...a.components[0]!.templates.values()];
+ expect(html.join('')).toContain(``);
+ const { code } = compile(a, true);
+ expect([...a.components[0]!.templates.values()]).toEqual(html);
+ expect(code).not.toContain('color:red');
+ expect(analyze(source, '/one.tsx').components[0]!.scope).toBe(a.components[0]!.scope);
+ });
+ it('leaves components without style blocks unchanged and rejects malformed CSS', () => {
+ const plain = analyze('export function Plain(){ return Plain
; }', '/plain.tsx');
+ compile(plain, false);
+ expect(plain.components[0]!.scope).toBeUndefined();
+ expect([...plain.components[0]!.templates.values()]).toEqual(['Plain
']);
+ expect(() => scopeStyles('h2[ { color: red }', scope, 'broken.css')).toThrow();
+ });
+});
diff --git a/packages/view/vite.config.ts b/packages/view/vite.config.ts
index a3a4703f0..360983251 100644
--- a/packages/view/vite.config.ts
+++ b/packages/view/vite.config.ts
@@ -12,6 +12,16 @@ export default defineConfig({
format: 'esm',
dts: true,
clean: true,
- deps: { neverBundle: ['vite', 'void', 'void/*', 'oxc-parser', 'magic-string', 'entities'] },
+ deps: {
+ neverBundle: [
+ 'vite',
+ 'void',
+ 'void/*',
+ 'oxc-parser',
+ 'magic-string',
+ 'entities',
+ 'lightningcss',
+ ],
+ },
},
});
diff --git a/playground/view/components/Counter.tsx b/playground/view/components/Counter.tsx
index b48c8dada..beb9d3f9c 100644
--- a/playground/view/components/Counter.tsx
+++ b/playground/view/components/Counter.tsx
@@ -40,6 +40,16 @@ export function Counter({ initial = 0 }: { initial?: number }) {
Item {index + 1}
))}
+
);
}
diff --git a/playground/view/components/Details.tsx b/playground/view/components/Details.tsx
index 4f2fb4ad8..0d0d3cc33 100644
--- a/playground/view/components/Details.tsx
+++ b/playground/view/components/Details.tsx
@@ -41,6 +41,15 @@ export function Details() {
))}
+
);
}
diff --git a/playground/view/components/Label.tsx b/playground/view/components/Label.tsx
index 8ddffb885..447d6c39c 100644
--- a/playground/view/components/Label.tsx
+++ b/playground/view/components/Label.tsx
@@ -1,4 +1,9 @@
import type { BaseProps } from '@void/view';
export function Label({ children }: BaseProps) {
- return {children};
+ return (
+ <>
+ {children}
+
+ >
+ );
}
diff --git a/playground/view/components/ScopedStyles.tsx b/playground/view/components/ScopedStyles.tsx
new file mode 100644
index 000000000..bd6706e2b
--- /dev/null
+++ b/playground/view/components/ScopedStyles.tsx
@@ -0,0 +1,39 @@
+export function StyledChild() {
+ return (
+
+
Child heading
+
+
+ );
+}
+
+export function StyledPanel() {
+ let count = 0;
+ return (
+
+ Local heading: {count}
+
+ {count > 0 ? New heading after rerender
: null}
+
+
+
+ );
+}
diff --git a/playground/view/pages/about.tsx b/playground/view/pages/about.tsx
index a73bef852..ec4d83c5d 100644
--- a/playground/view/pages/about.tsx
+++ b/playground/view/pages/about.tsx
@@ -1,9 +1,9 @@
export default function About() {
return (
-
+ <>
A static page
This page ships ordinary HTML, with no View templates or component JavaScript.
Back to the playground
-
+ >
);
}
diff --git a/playground/view/pages/button.tsx b/playground/view/pages/button.tsx
index 56e97694f..1898afec8 100644
--- a/playground/view/pages/button.tsx
+++ b/playground/view/pages/button.tsx
@@ -2,12 +2,12 @@ import { Button } from '../components/Button.tsx';
export default function ButtonPage() {
return (
-
+ <>
Just one button
All examples
-
+ >
);
}
diff --git a/playground/view/pages/conditional.tsx b/playground/view/pages/conditional.tsx
index c02acf0ba..b42d16d16 100644
--- a/playground/view/pages/conditional.tsx
+++ b/playground/view/pages/conditional.tsx
@@ -1,10 +1,10 @@
import { Reveal } from '../components/Reveal.tsx';
export default function Conditional({ enabled }: { enabled: boolean }) {
return (
-
+ <>
Server-selected interaction
{enabled ? : No interactive component was rendered.
}
Enable the interactive example
-
+ >
);
}
diff --git a/playground/view/pages/index.tsx b/playground/view/pages/index.tsx
index e2b3f3885..de256e68b 100644
--- a/playground/view/pages/index.tsx
+++ b/playground/view/pages/index.tsx
@@ -1,17 +1,19 @@
import { Button } from '../components/Button.tsx';
import { Counter } from '../components/Counter.tsx';
import * as Examples from '../components/index.ts';
-import './style.css';
function Intro() {
return (
-
- Server-rendered HTML. Small custom elements only where interaction is needed.
-
+ <>
+
+ Server-rendered HTML. Small custom elements only where interaction is needed.
+
+
+ >
);
}
export default function Home() {
return (
-
+ <>
Void View
-
+
+ >
);
}
diff --git a/playground/view/pages/layout.tsx b/playground/view/pages/layout.tsx
new file mode 100644
index 000000000..9e2abd5e8
--- /dev/null
+++ b/playground/view/pages/layout.tsx
@@ -0,0 +1,35 @@
+import type { BaseProps } from '@void/view';
+
+export default function Layout({ children }: BaseProps) {
+ return (
+ <>
+ {children}
+
+ >
+ );
+}
diff --git a/playground/view/pages/style.css b/playground/view/pages/style.css
deleted file mode 100644
index 15cedd65d..000000000
--- a/playground/view/pages/style.css
+++ /dev/null
@@ -1,52 +0,0 @@
-:root {
- font:
- 17px/1.6 system-ui,
- sans-serif;
- color: #24352c;
- background: #f2f5ef;
-}
-body {
- margin: 0;
-}
-main {
- max-width: 850px;
- padding: 48px 24px;
- margin: auto;
-}
-h1 {
- font-size: 48px;
- letter-spacing: -2px;
- margin-bottom: 8px;
-}
-h2 {
- font-size: 21px;
-}
-.intro {
- color: #546558;
-}
-.card {
- background: white;
- border: 1px solid #d6dfd1;
- border-radius: 12px;
- padding: 24px;
- margin: 24px 0;
-}
-button {
- padding: 10px 16px;
- margin: 4px;
- border: 1px solid #809b83;
- border-radius: 6px;
- background: #e9f3e4;
- color: inherit;
- cursor: pointer;
- font: inherit;
-}
-.label {
- font-weight: bold;
-}
-a {
- color: #315d38;
-}
-.milestone {
- color: #31794b;
-}
diff --git a/playground/view/pages/styles-static.tsx b/playground/view/pages/styles-static.tsx
new file mode 100644
index 000000000..abb87d7a6
--- /dev/null
+++ b/playground/view/pages/styles-static.tsx
@@ -0,0 +1,10 @@
+import { StyledChild } from '../components/ScopedStyles.tsx';
+export default function StaticStyles() {
+ return (
+ <>
+ Static scoped CSS
+
+ Interactive styles example
+ >
+ );
+}
diff --git a/playground/view/pages/styles.tsx b/playground/view/pages/styles.tsx
new file mode 100644
index 000000000..afeeb4310
--- /dev/null
+++ b/playground/view/pages/styles.tsx
@@ -0,0 +1,16 @@
+import { StyledPanel } from '../components/ScopedStyles.tsx';
+export default function Styles() {
+ return (
+ <>
+ Component-scoped CSS
+ Unstyled outside heading
+
+ Global through a nested :root block
+ Global through a :root selector
+ Static styled component, without View JavaScript
+
+ All examples
+
+ >
+ );
+}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index fd516c93f..834009bad 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -759,6 +759,9 @@ importers:
entities:
specifier: ^9.0.0
version: 9.0.0
+ lightningcss:
+ specifier: ^1.33.0
+ version: 1.33.0
magic-string:
specifier: 'catalog:'
version: 1.4.5
diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml
index f92b77f1b..68922422d 100644
--- a/pnpm-workspace.yaml
+++ b/pnpm-workspace.yaml
@@ -163,3 +163,13 @@ minimumReleaseAgeExclude:
- mysql2@3.24.6
- vite-plus@1.3.0 || 1.6.0
- vitepress-plugin-group-icons@1.8.2
+
+permissions:
+ '@analogjs/platform':
+ skills: false
+ '@tanstack/react-router':
+ skills: false
+ '@tanstack/react-start':
+ skills: false
+ arktype:
+ skills: false
From 2a6991be3749b42d5de03cec0b51b134622bf2f9 Mon Sep 17 00:00:00 2001
From: Sasha Koss
Date: Mon, 16 Nov 2026 11:56:13 +0800
Subject: [PATCH 3/3] chore: add dynamic styles support to Void View
---
TODO.md | 4 +
packages/view/src/client.ts | 66 +++++++---
packages/view/src/compiler.ts | 78 ++++++++++--
packages/view/src/plugin.ts | 17 ++-
packages/view/src/server.ts | 34 +++++-
packages/view/src/shared.ts | 8 +-
packages/view/src/styles.ts | 30 ++++-
packages/view/test/integration/view.test.ts | 119 ++++++++++++++++++-
packages/view/test/unit/compiler.test.ts | 7 +-
packages/view/test/unit/styles.test.ts | 57 +++++++++
playground/view/components/DynamicStyles.tsx | 34 ++++++
playground/view/pages/styles-dynamic.tsx | 12 ++
playground/view/pages/styles.tsx | 3 +
13 files changed, 430 insertions(+), 39 deletions(-)
create mode 100644 playground/view/components/DynamicStyles.tsx
create mode 100644 playground/view/pages/styles-dynamic.tsx
diff --git a/TODO.md b/TODO.md
index 898b8cfca..1ffe7dc4e 100644
--- a/TODO.md
+++ b/TODO.md
@@ -2,3 +2,7 @@
- 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.
diff --git a/packages/view/src/client.ts b/packages/view/src/client.ts
index b1bf67fba..2d412d871 100644
--- a/packages/view/src/client.ts
+++ b/packages/view/src/client.ts
@@ -1,12 +1,14 @@
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;
mount?: () => void;
refresh?: () => void;
}
@@ -65,6 +67,7 @@ export function component(
_props: unknown,
initialize: Initialize,
dynamic: boolean,
+ styled = false,
): ViewResult {
if (!dynamic) {
return initialize((value) => value)() as ViewResult;
@@ -73,22 +76,44 @@ export function component(
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?.();
- }
- });
+ 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());
@@ -124,13 +149,24 @@ export function register(id: string, factory: (props: unknown) => unknown): void
},
);
}
-export function template(id: string, _html: string | null, bindings: Array): ViewResult {
+export function template(
+ id: string,
+ _html: string | null,
+ bindings: Array,
+ 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(
Array.from(fragment.querySelectorAll('[data-vv-slot]'), (el) => [
Number(el.getAttribute('data-vv-slot')),
diff --git a/packages/view/src/compiler.ts b/packages/view/src/compiler.ts
index a056c7946..e052581b0 100644
--- a/packages/view/src/compiler.ts
+++ b/packages/view/src/compiler.ts
@@ -15,6 +15,8 @@ export interface ComponentInfo {
node: Node;
dynamic: boolean;
scope?: string;
+ dynamicStyles: boolean;
+ styles: Array;
dependencies: Set;
callbacks: Set;
templates: Map;
@@ -110,12 +112,22 @@ export function analyze(source: string, file: string, identity = file): ModuleIn
const add = (node: Node, name: string) => {
let jsx = false;
let styled = false;
+ let dynamicStyles = false;
walk(node.body, (child) => {
if (child.type === 'JSXElement' || child.type === 'JSXFragment') {
jsx = true;
}
if (child.type === 'JSXElement' && child.openingElement.name.name === 'style') {
styled = true;
+ dynamicStyles ||= child.children.some(
+ (item: Node) =>
+ item.type === 'JSXExpressionContainer' &&
+ item.expression.type !== 'JSXEmptyExpression' &&
+ !(item.expression.type === 'Literal' && typeof item.expression.value === 'string') &&
+ !(
+ item.expression.type === 'TemplateLiteral' && item.expression.expressions.length === 0
+ ),
+ );
}
});
if (!jsx && !/^[A-Z]/.test(name)) {
@@ -137,7 +149,9 @@ export function analyze(source: string, file: string, identity = file): ModuleIn
name,
id,
node,
- dynamic: false,
+ dynamic: dynamicStyles,
+ dynamicStyles,
+ styles: [],
scope: styled ? `data-vv-cid-${id.slice(-8)}` : undefined,
dependencies: new Set(),
callbacks: new Set(),
@@ -305,6 +319,8 @@ export function compile(
}
for (const component of info.components) {
component.templates.clear();
+ component.styles = [];
+ let styleIndex = 0;
const fn = component.node;
const state = new Set();
for (const statement of fn.body.body ?? []) {
@@ -475,8 +491,54 @@ export function compile(
}
return slot(`${tag}({${props.join(',')}})`);
}
+ if (tag === 'style') {
+ if (opening.attributes.length) {
+ throw new Error(`${info.file}: component style blocks do not accept attributes`);
+ }
+ const expressions: Array = [];
+ const text = n.children
+ .map((item: Node) => {
+ if (item.type === 'JSXText') {
+ return decodeHTML(item.value);
+ }
+ const value = item.expression;
+ if (value?.type === 'JSXEmptyExpression') {
+ return '';
+ }
+ if (value?.type === 'Literal' && typeof value.value === 'string') {
+ return value.value;
+ }
+ if (value?.type === 'TemplateLiteral') {
+ return value.quasis
+ .map((quasi: Node, index: number) => {
+ let text = quasi.value.cooked;
+ if (index < value.expressions.length) {
+ const marker = `__vv_expr${expressions.length}__`;
+ expressions.push(expr(value.expressions[index], shadow));
+ text += marker;
+ }
+ return text;
+ })
+ .join('');
+ }
+ throw new Error(`${info.file}: use a template literal inside ',
+ )
+ : undefined,
+ rid,
+ )();
collect(content, components);
if (dynamic) {
components.add(id);
@@ -100,12 +118,20 @@ export function component(
}
return new Html(
dynamic
- ? `<${id}${root ? ` data-vv-props="${escapeHtml(JSON.stringify(encode(props)))}"` : ''}>${html}${id}>`
+ ? `<${id}${rid ? ` data-vv-rid-${rid} data-vv-cid-${rid}` : ''}${root ? ` data-vv-props="${escapeHtml(JSON.stringify(encode(props)))}"` : ''}>${html}${id}>`
: html,
components,
);
}
-export function template(_id: string, html: string, bindings: Array): Html {
+export function template(
+ _id: string,
+ html: string,
+ bindings: Array,
+ instance?: string,
+): Html {
+ if (instance) {
+ html = html.replaceAll(' data-vv-i', ' data-vv-cid-' + instance);
+ }
const children = new Map();
const components = new Set();
const attrs = new Map>();
diff --git a/packages/view/src/shared.ts b/packages/view/src/shared.ts
index 085a8220e..540055565 100644
--- a/packages/view/src/shared.ts
+++ b/packages/view/src/shared.ts
@@ -5,7 +5,13 @@ export type Binding = readonly [
value: unknown,
];
export type Change = (value: T) => T;
-export type Initialize = (change: Change) => () => unknown;
+export type Style = (key: number, css: string) => unknown;
+export type Initialize = (change: Change, style?: Style, instance?: string) => () => unknown;
+export function instanceStyles(css: string, id: string): string {
+ return css
+ .replaceAll('data-vv-rid-vvINSTANCE', 'data-vv-rid-' + id)
+ .replaceAll('data-vv-cid-vvINSTANCE', 'data-vv-cid-' + id);
+}
export const voidTags = /* @__PURE__ */ new Set(
'area base br col embed hr img input link meta param source track wbr'.split(' '),
);
diff --git a/packages/view/src/styles.ts b/packages/view/src/styles.ts
index 3155615bd..eafeb1b0b 100644
--- a/packages/view/src/styles.ts
+++ b/packages/view/src/styles.ts
@@ -1,19 +1,37 @@
import { Features, transform, type Selector } from 'lightningcss';
-/** Scope selectors at compile time. A leading :root explicitly opts a selector into global CSS. */
-export function scopeStyles(css: string, attribute: string, filename: string): string {
+/** Scope selectors at compile time. Static :root rules are global; instance rules stay in their host. */
+export function scopeStyles(
+ css: string,
+ attribute: string,
+ filename: string,
+ instance = false,
+): string {
// Flatten nesting first so :root { .global {} } and :root .global {} take the same path.
const flattened = transform({ filename, code: Buffer.from(css), include: Features.Nesting }).code;
return transform({
filename,
code: flattened,
- minify: true,
+ minify: !instance,
visitor: {
Selector(selector) {
if (selector[0]?.type === 'pseudo-class' && selector[0].kind === 'root') {
- return selector;
+ if (!instance) {
+ return selector;
+ }
+ selector = selector.slice(1);
+ if (selector[0]?.type === 'combinator') {
+ selector = selector.slice(1);
+ }
}
- const scoped: Selector = [];
+ const scoped: Selector = instance
+ ? [
+ { type: 'attribute', name: 'data-vv-rid-vvINSTANCE' },
+ ...(selector.length
+ ? [{ type: 'combinator' as const, value: 'descendant' as const }]
+ : []),
+ ]
+ : [];
let compound: Selector = [];
function flush() {
if (!compound.length) {
@@ -22,7 +40,7 @@ export function scopeStyles(css: string, attribute: string, filename: string): s
const pseudo = compound.findIndex((part) => part.type === 'pseudo-element');
compound.splice(pseudo < 0 ? compound.length : pseudo, 0, {
type: 'attribute',
- name: attribute,
+ name: instance ? 'data-vv-cid-vvINSTANCE' : attribute,
});
scoped.push(...compound);
compound = [];
diff --git a/packages/view/test/integration/view.test.ts b/packages/view/test/integration/view.test.ts
index 73e578ef5..abd7a25fd 100644
--- a/packages/view/test/integration/view.test.ts
+++ b/packages/view/test/integration/view.test.ts
@@ -53,9 +53,19 @@ for (const mode of ['dev', 'preview'] as const) {
expect(await page.locator('vv-label').count()).toBe(0);
writeFileSync(
file,
- original.replace('', ' {}}>'),
+ original
+ .replace('', ' {}}>')
+ .replace('font-weight: bold', 'font-weight: normal'),
);
await page.waitForSelector('vv-label[data-vv-ready]', { timeout: 10_000 });
+ await expect
+ .poll(() =>
+ page
+ .locator('.label')
+ .first()
+ .evaluate((node) => getComputedStyle(node).fontWeight),
+ )
+ .toBe('400');
} finally {
writeFileSync(file, original);
await expect
@@ -213,6 +223,113 @@ for (const mode of ['dev', 'preview'] as const) {
await page.close();
}
});
+ it('bundles static CSS and isolates interpolated styles per instance before and after hydration', async () => {
+ const context = await browser.newContext({ javaScriptEnabled: false });
+ const ssr = await context.newPage();
+ try {
+ await ssr.goto(server.base + '/styles-dynamic');
+ const panels = ssr.locator('vv-dynamic-styles');
+ expect(await panels.count()).toBe(2);
+ expect(
+ await panels
+ .nth(0)
+ .locator('h2')
+ .evaluate((node) => getComputedStyle(node).color),
+ ).toBe('rgb(255, 0, 0)');
+ expect(
+ await panels
+ .nth(1)
+ .locator('h2')
+ .evaluate((node) => getComputedStyle(node).color),
+ ).toBe('rgb(0, 0, 255)');
+ expect(
+ await panels
+ .nth(0)
+ .locator('section')
+ .evaluate((node) => getComputedStyle(node).padding),
+ ).toBe('24px');
+ expect(await ssr.locator('vv-swatch style').count()).toBe(2);
+ } finally {
+ await context.close();
+ }
+ const page = await browser.newPage();
+ const errors: Array = [];
+ page.on('pageerror', (error) => errors.push(error.message));
+ try {
+ const response = await page.goto(server.base + '/styles-dynamic');
+ const initial = await response!.text();
+ const initialIds = [
+ ...initial.matchAll(/ match[1]);
+ await page.waitForSelector('vv-dynamic-styles[data-vv-ready]');
+ const panels = page.locator('vv-dynamic-styles');
+ const ids = await panels.evaluateAll((hosts) =>
+ hosts.map((host) =>
+ host.getAttributeNames().find((name) => name.startsWith('data-vv-rid-')),
+ ),
+ );
+ expect(ids).toEqual(initialIds);
+ expect(new Set(ids).size).toBe(2);
+ expect(ids.every(Boolean)).toBe(true);
+ const styles = await panels.nth(0).locator('section > style').elementHandles();
+ expect(styles).toHaveLength(2);
+ await panels.nth(0).getByRole('button').click();
+ await expect
+ .poll(() =>
+ panels
+ .nth(0)
+ .locator('h2')
+ .evaluate((node) => getComputedStyle(node).color),
+ )
+ .toBe('rgb(128, 0, 128)');
+ expect(
+ await panels
+ .nth(0)
+ .locator('h2')
+ .evaluate((node) => getComputedStyle(node).fontSize),
+ ).toBe('24px');
+ expect(
+ await panels
+ .nth(1)
+ .locator('h2')
+ .evaluate((node) => getComputedStyle(node).color),
+ ).toBe('rgb(0, 0, 255)');
+ expect(
+ await panels
+ .nth(0)
+ .locator('.swatch')
+ .evaluate((node) => getComputedStyle(node).color),
+ ).toBe('rgb(0, 128, 0)');
+ expect(await styles[0]!.evaluate((node) => node.isConnected)).toBe(true);
+ expect(await styles[1]!.evaluate((node) => node.isConnected)).toBe(true);
+ expect(
+ await panels
+ .nth(0)
+ .evaluate((host) =>
+ host.getAttributeNames().find((name) => name.startsWith('data-vv-rid-')),
+ ),
+ ).toBe(ids[0]);
+ expect(await page.locator('[data-vv-i]').count()).toBe(0);
+ expect(errors).toEqual([]);
+ await page.goto(server.base + '/styles-static');
+ expect(await page.locator('body style').count()).toBe(0);
+ expect(await page.locator('script[data-void-view], template').count()).toBe(0);
+ if (mode === 'preview') {
+ const css = await page
+ .locator('link[rel="stylesheet"]')
+ .evaluateAll(async (links) =>
+ Promise.all(
+ links.map(async (link) => (await fetch((link as HTMLLinkElement).href)).text()),
+ ),
+ );
+ expect(css.join('')).toContain('data-vv-cid-');
+ expect(css.join('')).not.toContain('data-vv-rid-');
+ expect(css.join('')).not.toContain('__vv_expr');
+ }
+ } finally {
+ await page.close();
+ }
+ });
it('binds events, keeps instance state, invokes callbacks, and updates after await', async () => {
const page = await browser.newPage();
const errors: Array = [];
diff --git a/packages/view/test/unit/compiler.test.ts b/packages/view/test/unit/compiler.test.ts
index 0a7221892..e7f6956fb 100644
--- a/packages/view/test/unit/compiler.test.ts
+++ b/packages/view/test/unit/compiler.test.ts
@@ -4,16 +4,15 @@ import { analyze, compile } from '../../src/compiler.ts';
import { component, Html, render, template } from '../../src/server.ts';
describe('view compiler', () => {
- it('keeps scoped style text in HTML rather than creating a binding', () => {
+ it('extracts scoped static styles out of the HTML template', () => {
const info = analyze(
'export function Styled(){ return ; }',
'/Styled.tsx',
);
compile(info, false);
const scope = info.components[0]!.scope;
- expect([...info.components[0]!.templates.values()]).toEqual([
- ``,
- ]);
+ expect([...info.components[0]!.templates.values()]).toEqual([``]);
+ expect(info.components[0]!.styles).toEqual([`h1[${scope}]{color:red}`]);
});
it.each([
'export default function({ label } = { label: "default" }) { return {label}
; }',
diff --git a/packages/view/test/unit/styles.test.ts b/packages/view/test/unit/styles.test.ts
index 7e5cf8674..9e834e28b 100644
--- a/packages/view/test/unit/styles.test.ts
+++ b/packages/view/test/unit/styles.test.ts
@@ -1,5 +1,6 @@
import { describe, expect, it } from 'vite-plus/test';
import { analyze, compile } from '../../src/compiler.ts';
+import { component, template } from '../../src/server.ts';
import { scopeStyles } from '../../src/styles.ts';
const scope = 'data-vv-cid-test';
@@ -47,6 +48,62 @@ describe('component styles', () => {
expect(code).not.toContain('color:red');
expect(analyze(source, '/one.tsx').components[0]!.scope).toBe(a.components[0]!.scope);
});
+ it('extracts static blocks while marking interpolated styles dynamic without event bindings', () => {
+ const info = analyze(
+ 'export function Color({color}) { return <>Color
>; }',
+ '/Color.tsx',
+ );
+ const owner = info.components[0]!;
+ expect(owner.dynamic).toBe(true);
+ expect(owner.dynamicStyles).toBe(true);
+ const server = compile(info, false).code;
+ const styles = [...owner.styles];
+ expect(styles).toEqual([`h2[${owner.scope}]{margin:0}`]);
+ expect([...owner.templates.values()].join('')).not.toContain('"}',
+ ),
+ ],
+ ['child', 1, '', 'literal data-vv-i'],
+ ],
+ instance,
+ ),
+ true,
+ true,
+ );
+ const a = render().value;
+ const b = render().value;
+ const id = /data-vv-rid-([a-f0-9-]+)/.exec(a)![1]!;
+ expect(b).not.toContain(id);
+ expect(a).toContain(``);
+ expect(a).toContain(`[data-vv-rid-${id}] h2[data-vv-cid-${id}]`);
+ expect(a).toContain('literal data-vv-i');
+ expect(a.match(/<\/style>/g)).toHaveLength(1);
+ expect(a).not.toContain('vvINSTANCE');
+ });
it('leaves components without style blocks unchanged and rejects malformed CSS', () => {
const plain = analyze('export function Plain(){ return Plain
; }', '/plain.tsx');
compile(plain, false);
diff --git a/playground/view/components/DynamicStyles.tsx b/playground/view/components/DynamicStyles.tsx
new file mode 100644
index 000000000..c008d0a88
--- /dev/null
+++ b/playground/view/components/DynamicStyles.tsx
@@ -0,0 +1,34 @@
+export function Swatch({ color }: { color: string }) {
+ return (
+
+ Dynamic styles alone make this a Web Component.
+
+
+ );
+}
+
+export function DynamicStyles({ initial }: { initial: string }) {
+ let color = initial;
+ let size = 20;
+ return (
+
+ Instance color: {color}
+
+
+
+
+
+
+ );
+}
diff --git a/playground/view/pages/styles-dynamic.tsx b/playground/view/pages/styles-dynamic.tsx
new file mode 100644
index 000000000..55f968214
--- /dev/null
+++ b/playground/view/pages/styles-dynamic.tsx
@@ -0,0 +1,12 @@
+import { DynamicStyles } from '../components/DynamicStyles.tsx';
+
+export default function DynamicStylesPage() {
+ return (
+ <>
+ Instance-scoped dynamic styles
+
+
+ Static styles example
+ >
+ );
+}
diff --git a/playground/view/pages/styles.tsx b/playground/view/pages/styles.tsx
index afeeb4310..370537d41 100644
--- a/playground/view/pages/styles.tsx
+++ b/playground/view/pages/styles.tsx
@@ -8,6 +8,9 @@ export default function Styles() {
Global through a nested :root block
Global through a :root selector
Static styled component, without View JavaScript
+
+ Dynamic styles and independent instances
+
All examples