coralite
v1.5.0
Published
Isomorphic Web Component framework for interactive SPAs and dynamic sites
Maintainers
Readme
🪸 Coralite
Build for the Web, With the Web
Coralite is a static-first Web Component framework that compiles HTML modules into static HTML at build time with zero client JavaScript by default. When interactivity or dynamic data is required, Coralite progressively adds server-side rendering (SSR), fine-grained client-side hydration, and single-page application (SPA) routing using standard native Web Components.
Key Features
- Zero-JS Static Components: Components without client lifecycle code emit pure static HTML and scoped CSS at build time with zero browser JavaScript overhead. Their module still ships for imperative hydration, but no
client()runs. - Native Web Components via Named Exports: Author components in
.htmlfiles using standard ES module exports (export const,export function) rather than boilerplate class syntax. - Light DOM Scoped CSS: Scoped styling using compiler-generated instance identifiers and CSS
@layerwithout requiring Shadow DOM boundaries. - Isomorphic Server Execution: Fetch data or compute state at build/SSR time in
server(). Initial state serializes to the client while server code is stripped from the browser bundle. - Opt-Out Hydration: Mark components with
no-hydrationto render markup statically and bypass client runtime hydration entirely. - Declarative Reactive Styling (
style): Define reactive inline styles and CSS Custom Properties (--*) directly in component declarations with zero-cost SSR emission. - Reactive Slot Transformations (
slots): Intercept and project slotted children isomorphically across SSR and client updates. - Declarative Templates & Pure Getters: Templates read flat tokens (
{{ token }}) while UI transformations live in pure getter functions, including client-only async getters. - Signal-Backed Reactivity: Mutate state in
client()controllers or derive it through signals. Batched DOM updates run without a virtual DOM, whileobserve()andupdateCompletehandle side effects and render synchronization. - Built-in Plugins:
refs, metadata, static assets, testing, virtualized lists, and an opt-in SPA router ship with the framework. - Global Store: A reactive
storedeclared incoralite.config.jsshares application-wide state across components and survives SPA navigation. - Async Race-Condition Protection: Built-in version locks ensure stale asynchronous promise resolutions never overwrite newer state.
Quick Start
The easiest way to start a new Coralite project is to use the official scaffolding tool. This sets up your directory structure, installs dependencies, and configures ready-to-run npm scripts automatically.
npm create coralite@latest my-coralite-app
cd my-coralite-app
npm run devKey Features
- Live Reload - Automatic browser refresh on HTML, template, and asset changes.
- Hot CSS Updates - Instant CSS injection without page refresh via Server-Sent Events.
- Sass/SCSS Support - Compile Sass files with source maps and auto-prefixing.
- File Watching - Monitors all source directories for changes.
- Production Optimization - Clean builds with optimized output.
- Plugin Integration - Full support for Coralite plugins.
Learn more about the scaffolding process in the Quick Start guide and the CLI reference.
AI-Assisted Development (llms.txt)
Coralite includes an authoritative llms.txt specification in the root of the published npm package (node_modules/coralite/llms.txt), alongside llms-full.txt.
When using modern AI coding assistants (Claude Code, Cursor, GitHub Copilot, Antigravity, Jules), the model automatically discovers Coralite's specific architectural invariants (dumb templates, serialization boundaries, two-phase plugin context, signal-backed state, imperative component tracking), ensuring accurate, hallucination-free code generation.
Code Quality & Static Auditing
Coralite includes built-in static analysis and self-healing tools to audit and fix your code across Components, Plugins, and Pages:
# Run multi-domain workspace audit
npx coralite check
# Preview self-healing AST auto-fixes without writing to disk
npx coralite fix --dry-run
# Apply deterministic AST auto-fixes in-place
npx coralite fixAuditing Commands
coralite check: Audits template syntax, unreferenced getters/server props, encapsulation rules, and unknown custom elements.coralite fix: Automatically resolves AST anti-patterns, lifts expressions to getters, rewrites client dynamic imports, and fixes plugin definitions.coralite validate-components: Statically audits component templates, refs, CSS selectors, and script blocks.coralite validate-plugins: Validates plugin contracts, lifecycle hooks, and isomorphic boundary compliance.coralite validate-pages: Validates page HTML against component schemas and encapsulation rules.coralite diagnostics: Lists every diagnostic code with severity, auto-fix availability and guidance. Supports--family,--severity,--fixable,--searchand--format jsonfor tooling and CI. Also available asDIAGNOSTIC_CATALOGfrom the package root.
Manual Installation & CLI
If you are adding Coralite to an existing project or building from scratch manually, install the core package:
npm install coraliteThe coralite binary resolves coralite.config.js from the current directory upward, so commands need no path arguments in a standard project:
| Command | Purpose |
| :--- | :--- |
| coralite dev | Development server with incremental rebuilds |
| coralite build | Production build into dist/ |
| coralite test | Deterministic build served for E2E tests |
| coralite check | Unified validation across components, pages, and plugins |
| coralite fix | Apply safe AST auto-fixes (--dry-run to preview) |
| coralite diagnostics | List every diagnostic code and its metadata |
| coralite init-agent | Scaffold AGENTS.md and AI IDE config aliases |
| coralite validate-* | Scope validation to components, pages, or plugins |
dev, build, and test share --clean, --verbose, --no-incremental, and --assets. See the CLI reference for filters, type-check flags, exit codes, and the diagnostic catalogue.
Configuration (defineConfig)
Coralite uses a coralite.config.js file at the root of your project. We provide a defineConfig helper to give you full IDE autocomplete and type safety.
import { defineConfig } from 'coralite'
import myCustomPlugin from './plugins/my-plugin.js'
export default defineConfig({
pages: './src/pages',
components: './src/components',
output: './dist',
plugins: [
myCustomPlugin()
]
})Building Components (Named Exports)
Coralite components are single-file HTML modules containing a <template>, an optional <style>, and a <script type="module">. Behavior is defined through top-level named exports (export function, export const).
Notice how the HTML template remains strictly declarative, reading flat properties from signal-backed state, while all complex logic is pushed into the
gettersblock.
src/components/user-card.html
<template id="user-card">
<h2 ref="title">{{ formatName }}</h2>
<p>{{ userMeta }}</p>
<slot></slot>
<p class="stats">Logins: {{ loginCount }}</p>
<p class="warning" hidden="{{ hideWarning }}">High Activity User</p>
</template>
<style>
/* Preferred: Style the root custom element container */
:host {
display: block;
border: 1px solid #eaeaea;
padding: 1.5rem;
border-radius: 8px;
}
/* Internal elements are automatically scoped (:host not required) */
h2 { color: coral; }
.warning { color: red; font-weight: bold; }
</style>
<script type="module">
// ATTRIBUTES: Public API, coerced from HTML (String, Number, Boolean)
export const attributes = {
userId: { type: Number, default: 0 },
role: { type: String, default: 'Guest' },
theme: { type: String, default: 'light' }
}
// SERVER: Async server-side initialization (Stripped from client bundle!)
export async function server ({ state }) {
const { userService } = await import('./services.js')
const user = await userService.getById(state.userId)
return {
firstName: user.firstName,
lastName: user.lastName,
loginCount: user.loginCount
}
}
// GETTERS: Pure, sync derived state mapping to the Dumb Template
export const getters = {
formatName: ({ state }) => `${state.firstName} ${state.lastName}`.trim(),
userMeta: ({ state }) => `Role: ${state.role} | ID: ${state.userId}`,
hideWarning: ({ state }) => state.loginCount < 50
}
// STYLE: Declarative reactive inline CSS and custom properties on host
export const style = {
'--accent-color': ({ state }) => state.theme === 'dark' ? '#38bdf8' : '#0284c7'
}
// SLOTS: Transform or build projected slot content
export const slots = {
default: (nodes) => nodes
}
// CLIENT: Client-side controller and side-effects
export function client ({ state, refs, signal }) {
const titleEl = refs('title')
// Bind events with the 'signal' for automatic garbage collection
titleEl.addEventListener('click', () => {
// Direct mutations schedule DOM updates in the next microtask
state.loginCount++
}, { signal })
}
</script>Computed Slots & Isomorphic DOM Compatibility
Computed slots (slots: { [name]: (nodes, context) => ... }) run during both Server-Side Rendering (SSR) and client runtime state updates:
- First-Class DOM Compatibility on Server: Coralite equips server-side AST nodes with a full DOM-emulation layer. Properties such as
node.textContent,node.nodeType,node.childNodes,node.getAttribute(),node.hasAttribute(),node.tagName, andnode.classListare fully supported on the server. - Write Standard DOM Code: Because of this compatibility layer, you do not need to branch on server-specific AST properties (like
.dataor.type). Writing standard DOM operations likenodes.map(n => n.textContent).join('')or checkingnode.nodeType === 1runs 100% isomorphically across both Node.js SSR and the browser. - Hydration Parity: Server-rendered computed slots are marked with
data-coralite-slot-computedduring SSR. During initial client hydration, Coralite preserves the server-rendered markup without redundant re-evaluation—preventing double transformations on non-idempotent slot functions (e.g.X!->X!!) and eliminating Cumulative Layout Shifts (CLS). Live projected node references are stored inslotEl._originalNodesso subsequent client state mutations re-evaluate against the live projected nodes directly, ensuring owner component bindings remain attached and reactive. - Environment Boundaries: Slot transformers should avoid browser-only layout APIs (
getBoundingClientRect(),offsetWidth) or DOM event listeners (addEventListener()), as these require an active browser rendering engine. Return HTML strings, DOM nodes, orundefined(to preserve original slotted content).
Extending the Engine (definePlugin)
Coralite plugins use definePlugin and separate build-time (server) from browser-time (client) work. Context is declared in one of two mutually exclusive ways:
- Ubiquitous
context: Injected into component and pageserver,getters,client,slots, andstyle. Use it for pure helpers that must run on both sides, such as formatters, translators, or read-only clients. - Environment-specific
server.context/client.context: Injected only intoserver()orclient()respectively. Useserver.contextfor build-only clients (database handles, filesystem readers, API keys) andclient.contextfor browser-only helpers (DOM utilities, third-party widgets).
Both forms are curried as (pluginContext) => (instanceContext) => ({ ... }):
- Phase 1: Global Setup (
pluginContext): Runs once during registration. Use it to initialize shared resources (e.g. database pools, in-memory caches, global event buses). Values assigned topluginContextare shared across the server build or client runtime. - Phase 2: Instance Resolver (
instanceContext): Runs for each component instance. Receives instance metadata (instanceId,componentId,page,root) and allows lazy cross-plugin resolution. The returned object is injected under the plugin'snamenamespace, and core keys (state,route,refs,signal,emit,observe,root) always win.
Mixing a ubiquitous context with server.context or client.context is a validation error.
import { definePlugin } from 'coralite'
export default function telemetryPlugin(options = {}) {
return definePlugin({
name: 'telemetry', // Namespaces the context in components: context.telemetry
server: {
// Phase 1 (pluginContext): Runs ONCE on server startup
// Phase 2 (instanceContext): Runs per component to inject instance-scoped APIs
context: (pluginContext) => {
const buildMetrics = new Map()
pluginContext.metrics = buildMetrics // Shared globally across server plugins
return (instanceContext) => ({
trackServerEvent: (name, data = {}) => {
buildMetrics.set(`${instanceContext.componentId}:${instanceContext.instanceId}`, {
event: name,
route: instanceContext.page?.url,
timestamp: Date.now(),
...data
})
}
})
}
},
client: {
// Phase 1 (pluginContext): Initializes shared client resources (e.g. centralized EventBus)
// Phase 2 (instanceContext): Injects instance-scoped tracking tagged with component metadata
context: (pluginContext) => {
const sharedBus = new EventTarget()
pluginContext.bus = sharedBus // Shared across all component instances in browser
return (instanceContext) => ({
emitEvent: (type, detail = {}) => {
sharedBus.dispatchEvent(new CustomEvent(type, {
detail: {
...detail,
originId: instanceContext.instanceId,
originTag: instanceContext.componentId
}
}))
},
onEvent: (type, handler) => {
sharedBus.addEventListener(type, handler)
}
})
}
}
})
}Consuming Plugin Context in Components
Components access the injected APIs directly via destructuring in server() and client():
<script type="module">
// Sourced from server Phase 2 resolver
export async function server ({ telemetry, state }) {
telemetry.trackServerEvent('user_rendered', { userId: state.userId })
return {}
}
// Sourced from client Phase 2 resolver
export function client ({ telemetry, state, signal }) {
telemetry.onEvent('cart:updated', (e) => {
console.log('Cart updated from component', e.detail.originTag)
})
telemetry.emitEvent('item_viewed', { itemId: state.userId })
}
</script>Imperative Custom Elements and Shared Helpers
Shared DOM factories belong in a plugin's client.context / client.init or in component/page client() code. Coralite AST-scans those client functions, rewrites document.createElement, innerHTML, outerHTML, and insertAdjacentHTML to its on-demand component loaders, and records literal custom-element tags in page/component dependency metadata.
import { definePlugin } from 'coralite'
export default definePlugin({
name: 'eventCards',
client: {
context: () => (instanceContext) => ({
createCard (event) {
const card = document.createElement('event-card')
card.setAttribute('event-id', event.id)
card.setAttribute('label', event.title)
return card
}
})
}
})Use a literal tag when the component should be tracked during the build. Dynamic tag expressions also lazy-load at runtime, but the concrete component may not be recorded in page metadata.
Do not move custom-element creation into a dynamically imported helper module. Those modules are bundled but not scanned. Keep helpers pure or use window.__coralite__.dom.createElement(tag) / await window.__coralite__.dom.loadComponent(tag) when DOM creation is unavoidable. dependencies controls plugin registration order, not custom-element tracking. Phase 1 of client.context runs before the loader is ready; create elements in phase 2 or client.init.
See the Imperative Components Reference for creation paths, scan boundaries, runtime APIs, upgrade timing, ownership, styling, testing, and troubleshooting.
Contributing
We welcome community contributions! Coralite is managed as a monorepo.
1. Prerequisites
- Node.js: >=22.22.2 (ESM native)
- pnpm: v12+
2. Fork & Clone
git clone [email protected]:tjdav/coralite.git
cd coralite3. Install Dependencies
Run the installation from the root directory. pnpm will automatically link the local packages together:
pnpm install4. Run Tests & Benchmarks
Before submitting a Pull Request, ensure your changes pass all tests and haven't caused performance regressions:
cd packages/coralite
# Run the unit test suite
pnpm run test:unit
# Run the deterministic E2E test suite
pnpm run test:e2e
# Run the fast internal regression benchmarks
pnpm run bench:internalFor canonical external harness runs, use pnpm run bench:external from the repository root. See benchmarks/README.md.
Documentation
For a deep dive into advanced features, imperative slot rendering, end-to-end testing strategies, and full API references, check out the official documentation.
License
This project is licensed under the Mozilla Public License 2.0 - see the LICENSE file for details.
