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(' 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('')) + ``}`; + } + 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]) => ``) + .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()): void { + if (!value || typeof value !== 'object' || seen.has(value)) { + return; + } + seen.add(value); + if (value instanceof Html) { + for (const id of value.components) { + components.add(id); + } + } else if (Array.isArray(value)) { + for (const item of value) { + collect(item, components, seen); + } + } else { + for (const item of Object.values(value)) { + collect(item, components, seen); + } + } +} +// Tagged tuples distinguish trusted compiled HTML from arbitrary application data. +function encode(value: unknown): Encoded { + if (value instanceof Html) { + return ['html', value.value]; + } + if (value === undefined) { + return ['undefined']; + } + if (Array.isArray(value)) { + return ['array', value.map(encode)]; + } + if ( + value !== null && + typeof value === 'object' && + Object.getPrototypeOf(value) === Object.prototype + ) { + return ['object', Object.entries(value).map(([key, item]) => [key, encode(item)])]; + } + if ( + value === null || + typeof value === 'string' || + typeof value === 'boolean' || + typeof value === 'number' + ) { + return ['scalar', value]; + } + throw new Error( + 'Root view props must be serializable. Pass callbacks from a dynamic view parent.', + ); +} +let depth = 0; +export function component( + id: string, + props: unknown, + initialize: Initialize, + dynamic: boolean, +): Html { + const root = depth === 0; + if (dynamic) { + depth++; + } + let html: string; + const components = new Set(); + try { + const content = initialize((value) => value)(); + collect(content, components); + if (dynamic) { + components.add(id); + collect(props, components); + } + html = render(content); + } finally { + if (dynamic) { + depth--; + } + } + return new Html( + dynamic + ? `<${id}${root ? ` data-vv-props="${escapeHtml(JSON.stringify(encode(props)))}"` : ''}>${html}` + : html, + components, + ); +} +export function template(_id: string, html: string, bindings: Array): Html { + const children = new Map(); + const components = new Set(); + const attrs = new Map>(); + function set(key: number, name: string, value: unknown) { + if (!attrs.has(key)) { + attrs.set(key, new Map()); + } + attrs.get(key)!.set(name, value); + } + for (const [kind, key, name, value] of bindings) { + if (kind === 'child') { + children.set(key, value); + collect(value, components); + } else if (kind === 'attr') { + set(key, name, value); + } else if (kind === 'spread') { + for (const [name, item] of Object.entries((value as object) ?? {})) { + if (!/^on/i.test(name)) { + set(key, name, item); + } + } + } + } + return new Html( + html + .replace(/ data-vv-a="(\d+)"/g, (_, key) => + [...(attrs.get(+key) ?? [])].map(([name, value]) => attribute(name, value)).join(''), + ) + .replace( + /<\/slot>|/g, + (_, slot, comment) => render(children.get(Number(slot ?? comment))), + ), + components, + ); +} +export function register(_id: string, _factory: (props: unknown) => unknown): void {} diff --git a/packages/view/src/shared.ts b/packages/view/src/shared.ts new file mode 100644 index 000000000..085a8220e --- /dev/null +++ b/packages/view/src/shared.ts @@ -0,0 +1,44 @@ +export type Binding = readonly [ + kind: 'child' | 'attr' | 'event' | 'spread', + key: number, + name: string, + value: unknown, +]; +export type Change = (value: T) => T; +export type Initialize = (change: Change) => () => unknown; +export const voidTags = /* @__PURE__ */ new Set( + 'area base br col embed hr img input link meta param source track wbr'.split(' '), +); +export function escapeHtml(value: unknown): string { + return String(value).replace( + /[&<>"']/g, + (char) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[char]!, + ); +} +export const booleanAttributes = new Set( + 'allowfullscreen async autofocus autoplay checked controls default defer disabled formnovalidate hidden inert ismap itemscope loop multiple muted nomodule novalidate open playsinline readonly required reversed selected'.split( + ' ', + ), +); +export function attribute(name: string, value: unknown): string { + if (!/^[a-zA-Z_:][\w:.-]*$/.test(name) || /^on/i.test(name) || name.startsWith('data-vv-')) { + throw new Error(`Unsafe view attribute: ${name}`); + } + if (value == null || (booleanAttributes.has(name) && !value)) { + return ''; + } + if (booleanAttributes.has(name)) { + return ` ${name}`; + } + if (typeof value === 'function' || typeof value === 'object') { + throw new Error(`View attribute ${name} must be a scalar`); + } + return ` ${name}="${escapeHtml(value)}"`; +} + +export type Encoded = + | ['html', string] + | ['undefined'] + | ['array', Array] + | ['object', Array<[string, Encoded]>] + | ['scalar', string | number | boolean | null]; diff --git a/packages/view/test/integration/view.test.ts b/packages/view/test/integration/view.test.ts new file mode 100644 index 000000000..3ce3a85a9 --- /dev/null +++ b/packages/view/test/integration/view.test.ts @@ -0,0 +1,208 @@ +import { execSync, spawn } from 'node:child_process'; +import { readFileSync, writeFileSync } from 'node:fs'; +import { resolve } from 'node:path'; +import type { Browser } from 'playwright'; +import { afterAll, beforeAll, describe, expect, it } from 'vite-plus/test'; +import { + killChild, + spawnPlaygroundServer, + startServer, +} from '../../../../test/integration-helpers.ts'; +import { launchPagesBrowser } from '../../../../test/pages-browser.ts'; + +const root = resolve(import.meta.dirname, '../../../..'); +for (const mode of ['dev', 'preview'] as const) { + describe(`view ${mode}`, () => { + let server: Awaited>; + let browser: Browser; + beforeAll(async () => { + if (mode === 'preview') { + execSync('vp run --filter ./playground/view build', { + cwd: root, + stdio: 'pipe', + timeout: 60_000, + }); + } + server = await startServer( + (port) => + mode === 'dev' + ? spawnPlaygroundServer(root, 'view', 'dev', port) + : spawn(process.execPath, ['dist/ssr/index.js'], { + cwd: resolve(root, 'playground/view'), + env: { ...process.env, PORT: String(port) }, + stdio: ['ignore', 'pipe', 'pipe'], + }), + 30_000, + ); + browser = await launchPagesBrowser(); + }, 60_000); + afterAll(async () => { + await browser?.close(); + if (server) { + await killChild(server.child); + } + }); + if (mode === 'dev') { + it('rebuilds the template and custom-element graph after editing a static component', async () => { + const file = resolve(root, 'playground/view/components/Label.tsx'); + const original = readFileSync(file, 'utf8'); + const page = await browser.newPage(); + try { + await page.goto(server.base); + await page.waitForSelector('vv-button[data-vv-ready]'); + expect(await page.locator('vv-label').count()).toBe(0); + writeFileSync( + file, + original.replace('', ' {}}>'), + ); + await page.waitForSelector('vv-label[data-vv-ready]', { timeout: 10_000 }); + } finally { + writeFileSync(file, original); + await expect + .poll(async () => { + const response = await fetch(server.base); + return response.ok && !(await response.text()).includes(' { + const html = await (await fetch(server.base)).text(); + expect(html).toContain('Press me'); + expect(html).toContain('Count: 0'); + expect(html).toMatch(/