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 apps/marketing/astro.config.mjs
- build
- dev
- preview
- check
- sync
apps/docs/astro.config.mjs
- build
- dev
- preview
- check
- sync
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.
$ nx run-many -t build ✔ nx run marketing:build [existing outputs match the cache, left as is] ✔ nx run docs:build [existing outputs match the cache, left as is] NX Successfully ran target build for 2 projects Nx read the output from the cache instead of running the command for 2 out of 2 tasks.
$ nx affected -t build ✔ nx run docs:build NX Successfully ran target build for project docs
$ 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.
nx g @geekvetica/nx-astro:application 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.jsonand anastrodependency. -
Copies the files, leaving out
node_modules, build output,.astroand lock files. -
Sets
outDirinastro.config.mjstodist/<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.jsonwith dev, build, preview, check and sync.
nx g @geekvetica/nx-astro:import \
--source=../my-astro-blog \
--name=blog \
--directory=apps/blog --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.
| 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.
"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.jsonreferences. -
Before each build, Astro-related dependency versions are copied from the root
package.jsoninto the app’s own, so Astro finds integrations that are installed at the root.
---
import Button from '@acme/ui/Button.astro';
---
<Button>Sign up</Button> {
"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 --astroVersionpicks the major to install. - Package managers
- npmpnpmyarnbun
- Detected from
packageManageror 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.
-
Install
Add the plugin as a dev dependency. Yarn Classic needs
-Wtoo.pnpm add -Dw @geekvetica/nx-astro -
Initialise
Registers the plugin in
nx.jsonand addsastroif it is missing.nx g @geekvetica/nx-astro:init -
Generate
Creates
apps/my-appfrom the minimal template.nx g @geekvetica/nx-astro:application my-app -
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.