@barefootjs/jinja

中文文档
0.41.0发布于 2026/11/10MITTypeScriptES Module构建来源已验证近 30 天下载 1705

Jinja2 adapter for BarefootJS — compiles IR to .jinja templates and ships the Python BarefootJS rendering runtime; runs under any Python web framework (Flask, etc.)

原始使用文档
文档目录3 节
  1. Template output shape
  2. Python runtime
  3. Usage

@barefootjs/jinja

Jinja2 adapter for BarefootJS: compiles the BarefootJS IR (JSX → IR, see spec/compiler.md) into .jinja template files plus the client JS bundle every other adapter produces, and ships a Python rendering runtime (python/barefootjs/) that renders those templates through a plain jinja2.Environment — no framework is required (Flask, Django, bare WSGI, etc. all work the same way).

Near-mechanical port of @barefootjs/xslate (the Text::Xslate/Kolon adapter) to Jinja2 syntax. See src/adapter/jinja-adapter.ts's header comment for the full Kolon↔Jinja syntax-mapping table and the JS/Python semantics divergences this port handles uniformly (truthiness, stringification, reserved-word identifier mangling, and the evaluator-only higher-order-callback lowering since Jinja has no lambda expression).

Template output shape

  • name: 'jinja', extension: '.jinja', templatesPerComponent: true — one .jinja file per component, named by snake-casing the PascalCase component name (UserCard → user_card.jinja).
  • Hydration markers (bf-s, bf-h/bf-m/bf-r, bf-p, slot/conditional comment markers, loop boundary comments) use the SAME runtime method names as every other adapter's bf.* calls (bf.scope_attr(), bf.hydration_attrs(), bf.text_start/text_end, bf.comment(...), …) — see spec/template-helpers.md for the shared helper contract.
  • Every text/attribute interpolation of a possibly-non-string value is routed through bf.string(...) (or bf.bool_str(...) for boolean-shaped values); every non-comparison condition position is routed through bf.truthy(...). Both are pure Python-runtime helpers — see the Python runtime pointer below.

Python runtime

python/barefootjs/ is a self-contained Python package (only dependency: jinja2) implementing the engine-agnostic bf object every emitted template calls into: hydration markers, context propagation (provide_context/use_context), child-component rendering (render_child), script registration, and the JS-compatible helper library (string, bool_str, truthy, number, floor/ceil/round, array/string helpers, the *_eval evaluator helpers, spread_attrs, query, …). It mirrors the layering of packages/adapter-perl/lib/ (an engine-agnostic core plus a thin per-engine backend) — see spec/template-helpers.md for the semantic contract each helper must satisfy, and the package's own tests under python/tests/.

Usage

// vite.config.ts
import { defineConfig } from 'vite'
import { barefoot } from '@barefootjs/jinja/vite'

export default defineConfig({
  plugins: barefoot({
    components: ['./src/components'],
    templates: './dist/templates',
  }),
})

vite build then emits .jinja templates + client JS under templates / Vite's own build.outDir. A Python host renders a component by constructing a jinja2.Environment (with autoescape=True, undefined=barefootjs.JsUndefined, trim_blocks=True, lstrip_blocks=True — see the adapter header comment for why the last two are required) over a FileSystemLoader pointed at the emitted templates, wiring in barefootjs.backend_jinja as the render backend. JinjaBackend(paths=[...]) builds exactly that Environment.

JS undefined and null are two values: host None is JS null, and barefootjs.UNDEF (a JsUndefined) is JS undefined. To leave a prop unset, omit its key (an omitted prop takes its destructure default, or reads as undefined); pass UNDEF for a prop that is present and undefined. A prop passed as None is null: its default does not apply.

版本与发布记录

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

版本发布日期距今
0.41.0latest2026/11/10今天
0.40.22026/11/027 天前
0.40.12026/10/2911 天前
0.40.02026/10/2614 天前
0.39.92026/10/2515 天前
0.39.82026/10/2119 天前

关于 @barefootjs/jinja 的常见问题

@barefootjs/jinja 是什么?

Jinja2 adapter for BarefootJS — compiles IR to .jinja templates and ships the Python BarefootJS rendering runtime; runs under any Python web framework (Flask, etc.)

如何安装 @barefootjs/jinja?

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

@barefootjs/jinja 支持 TypeScript 吗?

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

@barefootjs/jinja 支持 ESM 还是 CommonJS?

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

@barefootjs/jinja 可以商用吗?

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

@barefootjs/jinja 的下载量有多少?

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

@barefootjs/jinja 还在维护吗?

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