withastro / compiler

The Astro compiler. Written in Go. Distributed as WASM.
Other
502 stars 59 forks source link

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

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

import { parse } from "@astrojs/compiler";
import { walk, 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);
  }
});

Develop

VSCode / CodeSpaces

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

Contributing

CONTRIBUTING.md