@fluixi/template-parser

中文文档
1.0.0-alpha.108发布于 2026/11/07MITTypeScriptESM + CJS零依赖近 30 天下载 3976

dependency-free tokenizer and recursive-descent parser for html`` templates, producing a located, recoverable AST; shared by the compiler and the TypeScript plugin

原始使用文档
文档目录6 节
  1. Why a separate parser
  2. Usage
  3. What it parses
  4. Extensibility
  5. Diagnostics
  6. License

@fluixi/template-parser

The parser for Fluixi html`...` templates: a dependency-free tokenizer + recursive-descent parser that produces a located, recoverable Template AST.

It is the shared foundation for everything that needs to understand a template. Today that is @fluixi/compiler and @fluixi/ts-plugin; the point of splitting it out is that they agree on one grammar, one node model and one set of diagnostics, and that a formatter or a language server can join later without a second grammar appearing.

Why a separate parser

The compiler used to scan template text straight into codegen IR in one pass. That was fine for emitting code but useless for tooling: no source spans, no diagnostics, no error recovery, and hard-wired to Babel. This package is the parse layer split out so every tool can reuse it:

pieces → Reader (lexing + holes) → parseTemplate → Template AST
                                                      │
                    ┌─────────────────────────────────┴──────┐
               compiler (→ IR)                            TS plugin

The parser never resolves the JavaScript inside ${...}. Each hole is recorded as an opaque slot (an index + a source span). Downstream, each tool binds slots to its own representation: a Babel node in the compiler, a ts.Expression in the TS plugin, raw text in a formatter. That single seam is what keeps the parser host-agnostic.

Usage

import { parse } from '@fluixi/template-parser';

const { root, diagnostics } = parse([
  '<button @click=',        // static segment 0
  '>',                      // hole 0 sits between segment 0 and 1
  '</button>',              // static segment 1
]); // → <button @click=${…}>${…}</button>

parse(statics) takes the static string segments (one more than the number of holes) and returns { root, diagnostics }. Hosts with real source positions build Piece[] directly (via parseTemplate) so every span maps back to the original file.

What it parses

Standard HTML, components (<Button/>, <Ctx.Provider>), fragments (<>), comments, SVG, raw-text elements, void elements, and the full binding surface:

SyntaxNode
name, name="x", name=${x}, name="a ${x} b"AttributeNode
?disabledAttributeNode (boolean)
.currentTime=${t}PropertyBindingNode
@click=${h} / onClick=${h}EventBindingNode
ref=${el}RefBindingNode
...${props} / ${obj}SpreadNode
if=${c} / elseIfDirectiveNode / ElseDirectiveNode
each=${xs} (+ key)EachDirectiveNode
bind:value=${sig}BindDirectiveNode
class:active=${on}ClassDirectiveNode
style:color=${c}StyleDirectiveNode
use=${d} / use:tooltipUseDirectiveNode

Extensibility

Adding a directive is a new entry in the directive registry, never a change to the core parser:

import { CORE_DIRECTIVES, parseTemplate, type DirectiveDescriptor } from '@fluixi/template-parser';

const highlight: DirectiveDescriptor = {
  id: 'highlight',
  match: (name) => name === 'highlight',
  build: (raw) => ({ kind: 'Attribute', name: 'data-highlight', value: raw.value, boolean: false, loc: raw.loc }),
};

parseTemplate(pieces, { directives: [highlight, ...CORE_DIRECTIVES] });

Diagnostics

The parser never throws and never stalls. It recovers from malformed input and reports located diagnostics: unclosed / mismatched / stray tags, unterminated comments, duplicate directives, and invalid bindings. Every diagnostic carries a SourceSpan.

License

MIT

版本与发布记录

@fluixi/template-parser 共发布 14 个版本,首次发布于 2026/07/13;过去 12 个月发布了 14 个版本。

版本发布日期距今
1.0.0-alpha.108latest2026/11/078 天前
1.0.0-alpha.1072026/11/0511 天前
1.0.0-alpha.1062026/11/0213 天前
1.0.0-alpha.1052026/11/0115 天前
1.0.0-alpha.1042026/11/0115 天前
1.0.0-alpha.1032026/10/2620 天前

关于 @fluixi/template-parser 的常见问题

@fluixi/template-parser 是什么?

dependency-free tokenizer and recursive-descent parser for html`` templates, producing a located, recoverable AST; shared by the compiler and the TypeScript plugin

如何安装 @fluixi/template-parser?

在项目目录运行 npm install @fluixi/template-parser,或使用 pnpm add / yarn add / bun add。

@fluixi/template-parser 支持 TypeScript 吗?

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

@fluixi/template-parser 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@fluixi/template-parser 可以商用吗?

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

@fluixi/template-parser 的下载量有多少?

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

@fluixi/template-parser 还在维护吗?

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