# @astrojs/compiler 中文文档

规范地址：https://npmcn.com/package/%40astrojs/compiler/

版本：4\.0\.0
npm 元数据更新时间：2026-04-27T15:00:12.142Z
中文译文更新时间：2026-11-07T01:08:45.557Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40astrojs/compiler
原文对照：https://npmcn.com/package/%40astrojs/compiler/?lang=original
项目主页：https://astro.build/
源码仓库：https://github.com/withastro/compiler

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：5.3 MB，39 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：0 个
- 首次发布：2021-08-05T19:39:43.151Z
- 最新版本发布：2026-04-27T15:00:11.868Z
- 近 30 天下载量：29534210（2026-10-07 至 2026-11-05，npm 官方下载统计）
- 已发布版本数：373（近 12 个月 6 个）

## 中文简介

阿斯特罗 [走开](https://golang.org/) + WASM 编译器.

## 安装

```sh
npm install @astrojs/compiler
```

也可以使用：`pnpm add @astrojs/compiler`、`yarn add @astrojs/compiler`、`bun add @astrojs/compiler`

## README

# Astro 编译器

阿斯特罗 [走开](https://golang.org/) + WASM 编译器.

## 安装

```
npm install @astrojs/compiler
```

## 使用量

#### 变形 `.astro` 到有效的类型脚本

Astro 编译器可以转换 `.astro` 用于默认导出 HTML 的 TypeScript 模块的语法.

**一些注释**.

-   类型脚本有效 `.astro` 语法! 输出代码可能需要额外的后处理步骤来生成有效的JavaScript.
-   `.astro` 文件依赖于已曝光的服务器执行 `astro/runtime/server/index.js` 在节点生态系统。 目前其他运行时间需要通过下列途径实现并参考: `internalURL`\- 这是一个疼痛点,我们正在研究修复.

```js
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",
});
```

#### 解析 `.astro` 并返回一个 AST

Astro 编译器可以使用 `parse` 方法.

**一些注释**.

-   立场数据目前不完整,在某些情况下是不正确的。 我们正在努力!
-   A 类 `TextNode` 能够代表两个 HTML `text` 和JavaScript/TypeScript源代码.
-   这个 `@astrojs/compiler/utils` 切入点曝光 `walk` 财务报告和审定财务报表 `walkAsync` 函数,可用于穿行 AST 。 这也暴露了 `is` 可用作守护的帮助者,用于为每个守护者得出合适的类型 `node`。 。 。 .

```js
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)
  }
});
```

## 开发

### VSCode / 编码空间

A 类 `devcontainer` 配置可用 VSCode 的配置 [远程开发扩展包](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.vscode-remote-extensionpack) 和 GitHub 代码空间.

## 捐款

[捐款额](https://npmcn.com/CONTRIBUTING.md)

## 常见问题

### @astrojs/compiler 是什么？

阿斯特罗 走开 \+ WASM 编译器\.

### 如何安装 @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 和提交记录判断。

