@astrojs/compiler

中文文档
4.0.0发布于 2026/04/27MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 2953.4万

Astro’s Go + WASM compiler.

原始使用文档
文档目录5 节
  1. Install
  2. Usage
  3. Develop
  4. VSCode / CodeSpaces
  5. Contributing

Astro Compiler

Astro’s Go + WASM compiler.

Install

npm install @astrojs/compiler

Usage

Transform .astro to valid TypeScript

The Astro compiler can convert .astro syntax to a TypeScript Module whose default export generates HTML.

Some notes...

  • TypeScript is valid .astro syntax! The output code may need an additional post-processing step to generate valid JavaScript.
  • .astro files rely on a server implementation exposed as astro/runtime/server/index.js in the Node ecosystem. Other runtimes currently need to bring their own rendering implementation and reference it via internalURL. This is a pain point we're looking into fixing.
import { transform, type TransformResult } from "@astrojs/compiler";

const result = await transform(source, {
  filename: "/Users/astro/Code/project/src/pages/index.astro",
  sourcemap: "both",
  internalURL: "astro/runtime/server/index.js",
});

Parse .astro and return an AST

The Astro compiler can emit an AST using the parse method.

Some notes...

  • Position data is currently incomplete and in some cases incorrect. We're working on it!
  • A TextNode can represent both HTML text and JavaScript/TypeScript source code.
  • The @astrojs/compiler/utils entrypoint exposes walk and walkAsync functions that can be used to traverse the AST. It also exposes the is helper which can be used as guards to derive the proper types for each node.
import { parse } from "@astrojs/compiler";
import { walk, walkAsync, is } from "@astrojs/compiler/utils";

const result = await parse(source, {
  position: false, // defaults to `true`
});

walk(result.ast, (node) => {
  // `tag` nodes are `element` | `custom-element` | `component`
  if (is.tag(node)) {
    console.log(node.name);
  }
});

await walkAsync(result.ast, async (node) => {
  if (is.tag(node)) {
    node.value = await expensiveCalculation(node)
  }
});

Develop

VSCode / CodeSpaces

A devcontainer configuration is available for use with VSCode's Remote Development extension pack and GitHub CodeSpaces.

Contributing

CONTRIBUTING.md

版本与发布记录

@astrojs/compiler 共发布 373 个版本,首次发布于 2021/08/06;过去 12 个月发布了 6 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
4.0.0latest2026/04/276 个月前
3.0.12026/03/167 个月前
3.0.02026/03/068 个月前
3.0.0-beta.12026/02/178 个月前
3.0.0-beta.02026/02/069 个月前
2.13.12026/02/069 个月前

其他发布通道:canary → 0.1.0-canary.36,next--wasm → 0.11.0-next--wasm.0,pre → 0.15.2-pre.0,metadata-preview → 0.16.0-metadata-preview,render-head-pre → 0.17.0-pre.4,no-canonical → 0.19.0-pre.0。

关于 @astrojs/compiler 的常见问题

@astrojs/compiler 是什么?

Astro’s Go + WASM compiler.

如何安装 @astrojs/compiler?

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

@astrojs/compiler 支持 TypeScript 吗?

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

@astrojs/compiler 支持 ESM 还是 CommonJS?

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

@astrojs/compiler 可以商用吗?

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

@astrojs/compiler 的下载量有多少?

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

@astrojs/compiler 还在维护吗?

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