astrojs-compiler-sync

raw JSON →
1.1.1 verified Fri May 01 auth: no javascript

A synchronous wrapper around the @astrojs/compiler, enabling synchronous processing of Astro files. Current stable version is 1.1.1. Maintained actively with regular releases. Key differentiator: unlike the official compiler which is async-only, this package provides a sync API for use in environments where async is inconvenient, such as certain build tools or scripts. Requires @astrojs/compiler >=0.27.0 as a peer dependency and Node.js ^18.18.0 || >=20.9.0. Ships TypeScript definitions.

error ERR_REQUIRE_ESM: require() of ES Module not supported
cause Using CommonJS require to import an ESM package.
fix
Use dynamic import() or convert to ESM and use import statements.
error Cannot find module '@astrojs/compiler'
cause Missing peer dependency @astrojs/compiler.
fix
Run npm install @astrojs/compiler@>=0.27.0.
error parse is not a function
cause Incorrect import (e.g., default import instead of named export).
fix
Use import { parse } from 'astrojs-compiler-sync'.
breaking Dropped support for Node.js <18.18 (v1.0.0)
fix Upgrade Node.js to ^18.18.0 or >=20.9.0.
breaking Requires @astrojs/compiler >=0.27.0 (v0.3.0)
fix Update @astrojs/compiler to >=0.27.0 (e.g., npm install @astrojs/compiler@latest).
gotcha CJS entry typo fixed in v1.1.0 - incorrect typing for cjs entry
fix Update to v1.1.0 or later.
gotcha Crash in browser for earlier versions (fixed in v0.3.3)
fix Update to >=0.3.3.
deprecated Older versions use require('cjs') internally; v1.1.1 refactored to use require directly in CJS context
fix Update to v1.1.1 to avoid potential issues with CJS bundlers.
npm install astrojs-compiler-sync
yarn add astrojs-compiler-sync
pnpm add astrojs-compiler-sync

Shows how to synchronously parse and transform Astro code using the sync wrapper.

import { parse, transform } from 'astrojs-compiler-sync';

const code = `<html><body><h1>Hello</h1></body></html>`;

// Parse
const parsed = parse(code);
console.log('Parsed AST:', parsed.ast);

// Transform
const result = transform(code, { sourcemap: true });
console.log('Transformed code:', result.code);
console.log('Source map:', result.map);