Nx plugin for Astro · v3.2.0 · MIT

Add Astro to your Nx monorepo.

nx-astro finds every astro.config in your workspace and gives each app build, dev, preview, check and sync targets. Builds are cached, nx affected skips what did not change, and generators create a new app or bring in one you already have.

pnpm add -Dw @geekvetica/nx-astro

Works with Nx 22 to 24 and Astro 5 to 7. Yarn Classic needs -W in a workspace root.

Example workspace: what changed?
marketing build runs

apps/marketing/astro.config.mjs

  • build
  • dev
  • preview
  • check
  • sync
docs build runs

apps/docs/astro.config.mjs

  • build
  • dev
  • preview
  • check
  • sync
ui changed

libs/ui, imported by .astro pages of both apps

libs/ui changed. Both apps are affected because their .astro pages import it, so both builds run again.

terminalillustrative output, timings left out
$ nx affected -t build

✔  nx run marketing:build
✔  nx run docs:build

 NX   Successfully ran target build for 2 projects

What the plugin does

Generators, executors and inferred tasks for Astro projects.

Each item below maps to a generator, an executor or the task inference shipped in @geekvetica/nx-astro 3.2.0.

01Generate

Generate a new Astro app

The application generator writes a minimal Astro project under apps/, points its outDir at dist/apps/<name>, adds a project.json with five targets and references the project from the root tsconfig.json.

The component generator adds .astro components to a project you already have.

application generator reference

terminal
nx g @geekvetica/nx-astro:application marketing
created in apps/marketing
public/favicon.svg
src/components/Welcome.astro
src/layouts/Layout.astro
src/pages/index.astro
src/env.d.ts
.gitignore
astro.config.mjs     outDir: '../../dist/apps/marketing'
package.json
project.json         dev, build, preview, check, sync
tsconfig.base.json   extends the workspace config
tsconfig.json        copies the workspace aliases

02Import

Import an Astro project you already have

Point the import generator at a standalone Astro project. It copies the sources into the workspace and rewires the parts that differ in a monorepo.

  • Checks that the source has an Astro config, a package.json and an astro dependency.
  • Copies the files, leaving out node_modules, build output, .astro and lock files.
  • Sets outDir in astro.config.mjs to dist/<project root>, unless the config already has one or is written in an unusual form.
  • Merges the project’s dependencies into the root package.json.
  • Writes project.json with dev, build, preview, check and sync.
terminal
nx g @geekvetica/nx-astro:import \
  --source=../my-astro-blog \
  --name=blog \
  --directory=apps/blog
options
--source       path to the existing project (required)
--name         project name in the workspace
--directory    where to place it
--tags         comma-separated tags
--importPath   TypeScript path alias for the project
--skipInstall  do not install dependencies afterwards
--skipFormat   do not format generated files

03Inferred tasks

Tasks inferred from your Astro config

The plugin looks for astro.config.mjs, .js or .ts anywhere in the workspace. Every directory that has one becomes a project tagged astro with the targets below. You can rename each target in nx.json.

Targets created for each Astro project
Target Runs Cached Runs first
build Production build; dist/<project root> is cached yes ^build of its dependencies
dev Dev server, on the port of the Astro config, otherwise 4321 no nothing
preview Serves the built site no build
check Astro type checking and diagnostics yes sync
sync Type generation for content collections no nothing
test Vitest. Added only when vitest is in the project’s or the root package.json yes nothing

04Caching and affected

Cached builds and affected runs

The build’s inputs are the app’s production files, the production files of the projects it depends on, its package.json and the installed astro version. When none of them changed, Nx restores dist/ from its cache and does not run Astro.

nx affected -t build goes one step further and only considers projects touched by your change, as in the example at the top of this page.

inferred build target
"build": {
  "executor": "@geekvetica/nx-astro:build",
  "cache": true,
  "dependsOn": ["^build"],
  "inputs": [
    "production",
    "^production",
    "{projectRoot}/package.json",
    { "externalDependencies": ["astro"] }
  ],
  "outputs": ["{workspaceRoot}/dist/{projectRoot}"]
}

05Sharing code

Share code between apps

An Astro app is a regular project in the Nx graph. Imports in .astro files create dependencies too, so a library is built before the apps that use it and a change in it invalidates their cached builds.

  • Generated and imported apps copy the workspace aliases into their tsconfig.json. Aliases of libraries you create later are added by hand.
  • Every generated or imported project is added to the root tsconfig.json references.
  • Before each build, Astro-related dependency versions are copied from the root package.json into the app’s own, so Astro finds integrations that are installed at the root.
apps/marketing/src/pages/index.astro
---
import Button from '@acme/ui/Button.astro';
---

<Button>Sign up</Button>
apps/marketing/tsconfig.json
{
  "extends": "./tsconfig.base.json",
  "include": [".astro/types.d.ts", "**/*"],
  "exclude": ["dist"],
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@acme/ui/*": ["../../libs/ui/src/*"],
      "@/*": ["./src/*"]
    }
  }
}

06Compatibility

Supported versions

The ranges come from the peer dependencies of the published package. The end-to-end suite runs in CI against every supported Nx major.

Nx
222324
Peer range >=22.0.0 <25.0.0
Astro
567
Peer range >=5.0.0 <8.0.0. init --astroVersion picks the major to install.
Package managers
npmpnpmyarnbun
Detected from packageManager or the lock file.

Quick start

Four commands to a running app

Run them in an existing Nx workspace. If nx is not installed globally, put your package manager in front, such as pnpm exec nx or npx nx.

  1. Install

    Add the plugin as a dev dependency. Yarn Classic needs -W too.

    pnpm add -Dw @geekvetica/nx-astro
  2. Initialise

    Registers the plugin in nx.json and adds astro if it is missing.

    nx g @geekvetica/nx-astro:init
  3. Generate

    Creates apps/my-app from the minimal template.

    nx g @geekvetica/nx-astro:application my-app
  4. Run

    Starts the Astro dev server on port 4321.

    nx dev my-app

Start with the getting started guide.

It covers installing the plugin, initialising the workspace, generating an app and running its targets, with the files each step changes.

Who maintains this

nx-astro is developed and maintained by Geekvetica and released under the MIT license. This site is built with the plugin.