@appcues/snabbdom-virtualize

中文文档
1.1.0发布于 2025/11/22MITCommonJS近 30 天下载 3.3万

Library for turning strings and DOM nodes into virtual DOM nodes compatible with snabbdom.

原始使用文档
文档目录5 节
  1. API
  2. virtualize(nodes, options)
  3. Usage
  4. Project setup
  5. Tests

snabbdom-virtualize Build Status

Library for turning strings and DOM nodes into virtual DOM nodes compatible with Snabbdom.

API

virtualize(nodes, options)

  • nodes: Element|String - Either a DOM Element or a string of HTML to turn into a set of virtual DOM nodes.
  • options: Object - A hash of options to pass into the virtualize call. Available options are currently:
    • context: Document - An alternative DOM document to use (default is window.document).
    • hooks: Object - An object specifying hooks to call during the virtualization process. See the hooks section below.

Usage

Add it to your application with

npm install --save snabbdom-virtualize

Require/import it.

// ES6
import virtualize from 'snabbdom-virtualize';

// Require.
let virtualize = require('snabbdom-virtualize').default;

Pass it a set of DOM nodes or a string representing DOM nodes with one root node.

// Actual DOM nodes
let topNode = document.createElement('div');
let textNode = document.createTextNode('Click ');
let linkNode = document.createElement('a');
linkNode.setAttribute('href', 'http://example.com');
linkNode.textContent = 'here';
topNode.appendChild(textNode);
topNode.appendChild(linkNode);
let vnode = virtualize(topNode);


// String
let vnode = virtualize('<div>Click <a href="http://example.com">here</a></div>');

Specifying a different document

You can specify a different DOM document (other than the default window.document in a browser) by passing a context option into your calls to virtualize. This will allow for usage in a server-side setting, where there is no browser. One option is to use jsdom to create a DOM document that can be used:

const virtualize = require('snabbdom-virtualize').default;
const jsdom = require('jsdom').jsdom;
virtualizeString('<div>Click <a href="http://example.com">here</a></div>', {
  context: jsdom('<html></html>')
});

Using modules à la carte

If you'd prefer to import just the function for virtualizing DOM nodes or just the function for virtualizing HTML strings, you're in luck. Just import snabbdom-virtualize/nodes or snabbdom-virtualize/strings and use in the same way:

// DOM nodes.
import virtualize from 'snabbdom-virtualize/nodes';

let topNode = document.createElement('div');
let textNode = document.createTextNode('Click ');
let linkNode = document.createElement('a');
linkNode.setAttribute('href', 'http://example.com');
linkNode.textContent = 'here';
topNode.appendChild(textNode);
topNode.appendChild(linkNode);
let vnode = virtualize(topNode);


// HTML strings.
import virtualize from 'snabbdom-virtualize/strings';

let vnode = virtualize('<div>Click <a href="http://example.com">here</a></div>');

Hooks

You can register a create hook with any of the virtualize functions. This will be called once for each vnode that was created. It's called after the virtualization process is completed. The function receives one argument - the VNode that was created.

// The function passed as the 'create' hook is called 3 times: once for the
// <div>, once for the <span> and once for the text node inside the <span>.
virtualize("<div><span>Hi!</span></div>", {
    hooks: {
        create: function(vnode) { ... }
    }
});

Hooks allow you to perform some operations on your VNodes after virtualization but before patching with snabbdom.

Project setup

Written in ES6, compiled using Babel. To get started:

npm install
npm run build

This will output compiled files in the lib directory.

Tests

Tests can be run with npm test.

版本与发布记录

@appcues/snabbdom-virtualize 共发布 3 个版本,首次发布于 2021/09/17;过去 12 个月发布了 1 个版本。

版本发布日期距今
1.1.0latest2025/11/2211 个月前
1.0.0-beta.12021/09/175 年前
1.0.0-beta.02021/09/175 年前

关于 @appcues/snabbdom-virtualize 的常见问题

@appcues/snabbdom-virtualize 是什么?

Library for turning strings and DOM nodes into virtual DOM nodes compatible with snabbdom.

如何安装 @appcues/snabbdom-virtualize?

在项目目录运行 npm install @appcues/snabbdom-virtualize,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:snabbdom(^3.1.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@appcues/snabbdom-virtualize 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/appcues__snabbdom-virtualize 是否存在。

@appcues/snabbdom-virtualize 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@appcues/snabbdom-virtualize 可以商用吗?

@appcues/snabbdom-virtualize 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@appcues/snabbdom-virtualize 的下载量有多少?

根据 npm 官方下载统计,@appcues/snabbdom-virtualize 在 2026/10/07 – 2026/11/05 这 30 天内被下载 32,820 次,日均约 1,094 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@appcues/snabbdom-virtualize 还在维护吗?

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