# @astrojs/compiler

> Astro’s [Go](https://golang.org/) + WASM compiler.

Latest version **4.0.0** (published 2026-04-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install @astrojs/compiler
pnpm add @astrojs/compiler
yarn add @astrojs/compiler
bun add @astrojs/compiler
```

## Health

**Score 65/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; high maintenance score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 4.0.0 |
| Published | 2026-04-27 |
| First published | 2021-08-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 5 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 659 |
| Author | withastro |
| Maintainers | fredkschott, matthewp |

## Links

- npm: https://www.npmjs.com/package/@astrojs/compiler
- Repository: https://github.com/withastro/compiler
- Homepage: https://astro.build
- Issues: https://github.com/withastro/compiler/issues
- npm.io page: https://npm.io/package/@astrojs/compiler

## Recent versions

- 4.0.0 (latest) — 2026-04-27
- 3.0.0-beta.1 (beta) — 2026-02-17
- 0.0.0-render-script-20251003120459 (next--render-script) — 2025-10-03
- 0.0.0-script-order-20251002140654 (next--script-order) — 2025-10-02
- 0.0.0-next-result-create-astro-20250926081949 (next--next-result-create-astro) — 2025-09-26
- 0.0.0-server-islands-20240620184241 (next--server-islands) — 2024-06-20
- 0.0.0-head-body-content-20240329184853 (next--head-body-content) — 2024-03-29
- 0.0.0-literal-20240329135330 (next--literal) — 2024-03-29
- 0.0.0-test-non-admin-snapshot-20240308150815 (next--test-non-admin-snapshot) — 2024-03-08
- 0.0.0-literal-parsing-20240308150251 (next--literal-parsing) — 2024-03-08
- 0.0.0-github-actions-20240228180311 (next--github-actions) — 2024-02-28
- 0.0.0-transition-reload-20240211162919 (next--transition-reload) — 2024-02-11
- 0.0.0-mk-slot-expr-fix-20240130083013 (next--mk-slot-expr-fix) — 2024-01-30
- 0.0.0-dynamic-slot-names-20240111152903 (next--dynamic-slot-names) — 2024-01-11
- 0.0.0-tsx-prefix-20231229054418 (next--tsx-prefix) — 2023-12-29
- … 358 more at https://npm.io/package/@astrojs/compiler/versions

## README

# Astro Compiler

Astro’s [Go](https://golang.org/) + 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.

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

#### 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`.

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

## Develop

### VSCode / CodeSpaces

A `devcontainer` configuration is available for use with VSCode's [Remote Development extension pack](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.vscode-remote-extensionpack) and GitHub CodeSpaces.

## Contributing

[CONTRIBUTING.md](/CONTRIBUTING.md)

---
_Source: https://npm.io/package/@astrojs/compiler · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
