• Stars
    star
    430
  • Rank 97,463 (Top 2 %)
  • Language
    Go
  • License
    Other
  • Created almost 3 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The Astro compiler. Written in Go. Distributed as WASM.

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 } 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 a walk function 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, 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

More Repositories

1

astro

The web framework for content-driven websites. ⭐️ Star to support our work!
TypeScript
41,861
star
2

starlight

🌟 Build beautiful, accessible, high-performance documentation websites with Astro
TypeScript
3,456
star
3

docs

Astro documentation
MDX
1,200
star
4

prettier-plugin-astro

Prettier plugin for Astro
TypeScript
412
star
5

astro.build

MDX
320
star
6

houston.astro.build

Experimental AI assistant trained on the Astro docs
Astro
243
star
7

language-tools

Language tools for Astro
TypeScript
220
star
8

roadmap

Ideas, suggestions, and formal RFC proposals for the Astro project.
218
star
9

action

A GitHub Action that deploys your Astro project to GitHub Pages
126
star
10

blog-tutorial-demo

Astro
125
star
11

houston-vscode

Visual Studio Code theme featuring cool blues, minty greens, and soft purples
CSS
98
star
12

cli-kit

JavaScript
48
star
13

adapters

Home for Astro's core maintained adapters
TypeScript
34
star
14

astro.new

Astro
31
star
15

github-pages

Automatically deploy an Astro site to GitHub Pages
Astro
29
star
16

cli

The Pika CLI
TypeScript
24
star
17

astro-repl

TypeScript
24
star
18

wormhole

TypeScript
18
star
19

registry

The Pika Registry - more info coming soon... https://editor.pika.dev/
TypeScript
17
star
20

.github

Community health files for the @withastro organization
15
star
21

site-kit

Styles, components, and tooling config for use in *.astro.build websites
JavaScript
15
star
22

esnext-conf-2020

Pika Presents: ESNEXT CONF 2020
HTML
12
star
23

contribute.docs.astro.build

An entire Starlight site for the Astro Docs contributor guides.
MDX
11
star
24

view-transitions-demo

Astro
10
star
25

automation

Centralized repo for GitHub actions for the `withastro` org
10
star
26

houston-discord

TypeScript
9
star
27

studio-templates

Astro
7
star
28

astro-playground

TypeScript
6
star
29

ASTRO-E2

beep boop beep boop
JavaScript
5
star
30

astro-ecosystem-ci

Astro Ecosystem CI
TypeScript
5
star
31

astro-og-image

JavaScript
4
star
32

action-studio

TypeScript
3
star
33

withastro.github.io

HTML
2
star
34

templates

A mirror of the examples from the main monorepo
Astro
2
star