使用通用的 async-tree 库进一步缩小一个小型 JavaScript 博客静态网站生成器

April 23, 2025

2025年4月23日

Using the async-tree library substantially cuts down the source code for a minimalist static site generator (SSG) in JavaScript, at a very modest cost in dependencies. The result is still fast and flexible.

使用 async-tree 库大大减少了 JavaScript 中极简静态网站生成器 (SSG) 的源代码,依赖成本非常低。结果仍然快速且灵活。

In the first post in this series, I recreated a simple blog in Astro that felt complicated. I rewrote the blog in plain JavaScript with zero dependencies. This post discusses yet another rewrite, this one predicated on sharing code.

在本系列的第一篇文章中,我重建了一个看起来复杂的 Astro 博客。我用零依赖的 纯 JavaScript 重写了这个博客。这篇文章讨论了另一个重写,这次是基于共享代码的。

You can look at the final async-tree blog source code and the live site.

您可以查看最终的 async-tree 博客 源代码实时网站

Okay, maybe a few dependencies

好吧,也许有一些依赖

The zero-dependency version felt quite good, although insisting on no dependencies was a little extreme.

零依赖版本感觉相当不错,尽管坚持 依赖有点极端。

While half the source code was unique to the project, the features in the other half can be cleanly handled by libraries, like:

虽然一半的源代码是项目独有的,但另一半的功能可以通过库来干净地处理,例如:

  • Transforming markdown to HTML. Markdown processing can be expressed as a pure function that accepts markdown and returns HTML. A processor like marked fits the bill.
  • 将markdown转换为HTML。Markdown处理可以表示为一个纯函数,该函数接受markdown并返回HTML。像 marked 这样的处理器符合要求。
  • Transforming a blog feed from JSON Feed format to RSS.
  • 将博客源 从 JSON Feed 格式转换为 RSS

These are both pure functions, a much easier kind of dependency to take on. You decide when to call the function and what input to give it; it gives you back a result without any side effects. This contract greatly reduces the potential for surprise or frustration.

这些都是纯函数,这是一种更容易承担的依赖关系。你决定何时调用函数以及提供什么输入;它会给你返回一个结果,而没有任何副作用。这个契约大大减少了意外或挫折的可能性。

The async-tree library

async-tree 库

The remaining sharable code in the zero-dependency version comprises generic utility functions:

零依赖版本中剩余的可共享代码包括通用实用函数:

  • A higher-order function that maps the keys and values of an object to return a new, transformed object
  • 一个高阶函数,将对象的键和值映射以返回一个新的、转换后的对象
  • A way to read a...
开通本站会员,查看完整译文。

trang chủ - Wiki
Copyright © 2011-2025 iteam. Current version is 2.143.0. UTC+08:00, 2025-05-03 11:33
浙ICP备14020137号-1 $bản đồ khách truy cập$