@basenative/runtime

中文文档
3.0.0发布于 2026/10/31Apache-2.0TypeScriptES Module零依赖构建来源已验证

Signal-based web runtime over native HTML — zero build step, zero production dependencies

原始使用文档
文档目录14 节
  1. Install
  2. Quick Start
  3. API
  4. Signals
  5. Hydration
  6. Browser Features
  7. Diagnostics & DevTools
  8. Debug Mode
  9. Error Boundaries
  10. Plugins
  11. Lazy Hydration
  12. Web Vitals
  13. Shared Utilities (subpath exports)
  14. License

@basenative/runtime

Signal-based reactive runtime for native HTML — zero build step, zero production dependencies

Part of the BaseNative ecosystem — a signal-based web runtime over native HTML.

Install

npm install @basenative/runtime

Quick Start

import { signal, computed, effect, hydrate } from '@basenative/runtime';

// Create reactive state
const count = signal(0);
const doubled = computed(() => count() * 2);

// React to changes
effect(() => {
  console.log(`count: ${count()}, doubled: ${doubled()}`);
});

count.set(5); // logs: count: 5, doubled: 10

// Hydrate server-rendered HTML with signal bindings
hydrate(document.body, { count });

API

Signals

  • signal(initial) — Creates a readable/writable reactive value. Call it to read (count()), use .set(value) to write, .peek() to read without tracking.
  • computed(fn) — Creates a derived signal that re-evaluates when its dependencies change. Lazy with automatic dependency tracking.
  • effect(fn) — Runs fn immediately and re-runs whenever any signal read inside changes. Returns a dispose function to stop tracking.
  • batch(fn) — Groups signal writes inside fn into a single effect flush instead of one flush per write. Nests; only the outermost call flushes.
  • registerPlugin(plugin) — Registers a global { onSignalWrite(signal, prev, next) } hook that observes every signal write. Returns an unregister function. Distinct from the definePlugin/createPluginRegistry lifecycle system below.

Hydration

  • hydrate(root, ctx) — Attaches signal reactivity to server-rendered HTML. Reads <!--bn:*--> markers emitted by @basenative/server.

Browser Features

  • browserFeatures — Signal containing detected browser capability flags.
  • detectBrowserFeatures() — Runs feature detection and updates browserFeatures.
  • supportsFeature(name) — Returns true/false for a named capability.

Diagnostics & DevTools

  • emitDiagnostic(diagnostic) — Emit a structured runtime diagnostic.
  • reportHydrationMismatch(info) — Report a server/client HTML mismatch.
  • enableDevtools() / disableDevtools() / isDevtoolsEnabled() — Toggle the BaseNative devtools hooks (live signal/effect/hydration state on globalThis.__BASENATIVE_DEVTOOLS__; creation registers automatically while enabled).
  • trackSignal(signal) / trackEffect(effect) / recordHydration(info) — DevTools instrumentation hooks.

Debug Mode

  • enableDebug(options) / disableDebug() / isDebugEnabled() — Toggle verbose console logging for signal/effect activity. Zero overhead when off.
  • getDebugStats() — Returns a snapshot of counters (signalsCreated, effectsCreated, signalWrites, signalReads, effectRuns).
  • debugSignal(signal, name) / debugEffect(fn, name) — Wrap a signal or effect so its activity is logged.
  • debugTime(label, fn) — Times and logs a synchronous call; still runs fn and returns its result when debug mode is off.
  • debugAssert(condition, message) — Throws when condition is false and debug mode is on; a full no-op when off.
  • debugDeps(signal, name) — Logs a signal's current value; a no-op when debug mode is off.

Error Boundaries

  • createErrorBoundary(options) — Creates a reusable boundary ({ onError, fallback }) with try(fn) / getError() / hasError() / getFallback() / reset(). Catches rendering errors and exposes a fallback.
  • renderWithBoundary(renderFn, options) — One-shot version for a single render call; same { onError, fallback } options, no reusable boundary object. Falls back to an HTML comment when no fallback is given.

Plugins

  • definePlugin(plugin) — Register a runtime plugin with lifecycle hooks.
  • createPluginRegistry() — Creates an isolated plugin registry.

Lazy Hydration

  • lazyHydrate(el, fn) — Hydrates an element on demand.
  • hydrateOnIdle(el, fn) — Hydrates when the browser is idle via requestIdleCallback.
  • hydrateOnInteraction(el, fn) — Hydrates on first user interaction with the element.
  • hydrateOnMedia(fn, query) — Hydrates when a CSS media query matches. Note: fn comes first, unlike the other hydrateOn* helpers.
  • createLazyHydrator(options) — Creates a lazy hydration controller with shared options.

Web Vitals

  • createVitalsReporter(options) — Creates a reporter that sends Core Web Vitals to an endpoint.
  • observeLCP(cb) / observeFID(cb) / observeCLS(cb) / observeFCP(cb) / observeTTFB(cb) / observeINP(cb) — Observe individual Web Vital metrics. Each returns a cleanup function, or null when PerformanceObserver is unavailable (e.g. during SSR).

Shared Utilities (subpath exports)

  • raw(value) — Marks a string as trusted markup, exempt from HTML-escaping. Also available from the package root.
  • @basenative/runtime/shared/escape — isRaw, unwrapRaw, escapeText, escapeAttr, isUrlAttribute, sanitizeUrl, findInterpolations. The output-escaping primitives shared with @basenative/server.
  • @basenative/runtime/shared/expression — compileExpression, evaluateExpression, clearExpressionCache, isScopeSlot, SCOPE_SLOT. The CSP-safe expression evaluator shared with @basenative/server; see docs/api/runtime.md for the full API and its security constraints.

License

Apache-2.0

版本与发布记录

@basenative/runtime 共发布 11 个版本,首次发布于 2026/03/20;过去 12 个月发布了 11 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.0.0latest2026/10/312 天前
2.4.02026/10/268 天前
2.3.22026/10/2212 天前
2.3.12026/10/2212 天前
2.3.02026/10/2212 天前
2.2.02026/10/2113 天前

关于 @basenative/runtime 的常见问题

@basenative/runtime 是什么?

基于信号的web运行时在本地HTML -零构建步骤,零生产依赖

如何安装 @basenative/runtime?

在项目目录运行 npm install @basenative/runtime,或使用 pnpm add / yarn add / bun add。

@basenative/runtime 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@basenative/runtime 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@basenative/runtime 可以商用吗?

@basenative/runtime 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@basenative/runtime 还在维护吗?

最新版本 3.0.0 发布于 2026/10/31(2 天前);至今共发布 11 个版本,其中过去 12 个月发布了 11 个;首次发布于 2026/03/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。