nx- 天体
Astro 的 Nx 插件 - 用 Nx 单列波创建 Astro 快速网站
特征
- 自动任务推论 - 检测Astro项目并自动配置任务
- 代码生成器 - 容易操作的脚手架应用程序和部件
- 完整类型脚本支持 - 类型安全配置和发展
- Nx 缓存 - 闪电快车用智能快车建造
- 内容集 - 对Astro内容集的一等支持
- 安保部门支助 - 建立静态网站或服务器应用程序
- 测试集成 - 为单位和综合测试提供内建测试支助
快速启动
安装
# Create a new Nx workspace
npx create-nx-workspace@latest my-workspace
# Install nx-astro
cd my-workspace
npm install --save-dev @geekvetica/nx-astro
# Initialize the plugin
nx g @geekvetica/nx-astro:init
创建您的第一个应用程序
# Generate an Astro application
nx g @geekvetica/nx-astro:application my-app
# Start development server
nx dev my-app
# Build for production
nx build my-app
创建组件
# Generate a component
nx g @geekvetica/nx-astro:component Button --project=my-app
# Generate a component in a subdirectory
nx g @geekvetica/nx-astro:component Card --project=my-app --directory=ui
可用命令
一旦您在工作空间中有Astro应用程序,您就可以运行这些命令:
# Development
nx dev my-app # Start dev server with HMR
nx dev my-app --port=3000 # Custom port
# Building
nx build my-app # Production build
nx build my-app --verbose # Verbose output
# Preview
nx preview my-app # Preview production build
# Type Checking
nx check my-app # Run type checking
nx check my-app --watch # Watch mode
# Testing
nx test my-app # Run tests
nx test my-app --coverage # With coverage
# Content Collections
nx sync my-app # Generate content types
Monorepo 工作流程
该插件与 Nx 单列波 特性无缝融合 :
# Run tasks for multiple projects
nx run-many --target=build --projects=app1,app2,app3
# Build all projects
nx run-many --target=build --all
# Build only affected projects (based on git changes)
nx affected --target=build
# View project graph
nx graph
模板
创建应用程序时从启动模板中选择 :
- 最小 - 基本启动文件
- 博客 - 有内容收藏的博客模板
- 组合 - 展示项目的组合模板
nx g @geekvetica/nx-astro:application my-blog --template=blog
nx g @geekvetica/nx-astro:application my-portfolio --template=portfolio
导入已存在的项目
将已有的 Astro 项目移入您的 Nx 工作空间 import 生成器 :
# Import from a local path
nx g @geekvetica/nx-astro:import --source=../my-astro-app
# Import with custom name and directory
nx g @geekvetica/nx-astro:import --source=./external/astro-site --name=my-site --directory=apps/websites
# Import with tags for organization
nx g @geekvetica/nx-astro:import --source=/path/to/app --tags=astro,web,public-facing
# Import with custom TypeScript path alias
nx g @geekvetica/nx-astro:import --source=../app --name=my-app --importPath=@myorg/my-app
导入生成器自动 :
- 验证源是一个有效的 Astro 项目
- 复制所有工程文件(不包括节点-模块、构建输出等)
- 创建 包含所有标准目标的 Nx 项目配置( dev, 构建, 预览, 检查, 同步)
- 更新 tsconfig.base.json 中的 TypeScript 路径映射
- 在工作区注册工程
导入后 :
# Install dependencies
pnpm install
# Start development
nx dev my-site
# Build for production
nx build my-site
配置
插件配置
配置插件在 nx.json数字 :
{
"plugins": [
{
"plugin": "@geekvetica/nx-astro",
"options": {
"devTargetName": "dev",
"buildTargetName": "build",
"checkTargetName": "check"
}
}
]
}
项目配置
插件自动推断任务 astro.config.mjs。要自定义,请创建 project.json数字 :
{
"name": "my-app",
"targets": {
"dev": {
"executor": "@geekvetica/nx-astro:dev",
"options": {
"port": 3000,
"host": true
}
},
"build": {
"executor": "@geekvetica/nx-astro:build",
"options": {
"outputPath": "dist/apps/my-app",
"site": "https://example.com"
}
}
}
}
共享图书馆
创建共享组件库, 供多个 Astro 应用程序使用 :
# Create shared library
nx g @nx/js:library shared-ui --directory=libs/shared-ui
# Configure path aliases in tsconfig.base.json
{
"compilerOptions": {
"paths": {
"@my-org/shared-ui/*": ["libs/shared-ui/src/*"]
}
}
}
# Use in any Astro app
import Button from '@my-org/shared-ui/components/Button.astro';
内容集
完全支持带有类型生成的 Astro 内容收藏 :
// src/content/config.ts
import { defineCollection, z } from 'astro:content';
const blog = defineCollection({
type: 'content',
schema: z.object({
title: z.string(),
pubDate: z.date(),
description: z.string(),
}),
});
export const collections = { blog };
# Generate types
nx sync my-app
# Types are now available
import { getCollection } from 'astro:content';
const posts = await getCollection('blog'); // Fully typed!
安保部门支助
构建服务器任务应用程序 :
// astro.config.mjs
import { defineConfig } from 'astro/config';
import node from '@astrojs/node';
export default defineConfig({
output: 'server',
adapter: node({ mode: 'standalone' }),
});
nx build my-app
node dist/apps/my-app/server/entry.mjs
测试
插件包括用于测试的 Vitest 配置 :
// src/components/Button.test.ts
import { experimental_AstroContainer as AstroContainer } from 'astro/container';
import Button from './Button.astro';
test('renders button', async () => {
const container = await AstroContainer.create();
const result = await container.renderToString(Button, {
slots: { default: 'Click me' },
});
expect(result).toContain('Click me');
});
nx test my-app
nx test my-app --coverage
nx test my-app --watch
业绩
Nx 缓存会大大地改进构建时间 :
# First build (cold)
nx build my-app
# ✓ Built in 15.3s
# Second build (cached)
nx build my-app
# ✓ Nx read the output from the cache (0.2s)
CI/CD 一体化
优化您的 CI 管道与受影响的检测:
# .github/workflows/ci.yml
- name: Build affected projects
run: nx affected --target=build --base=origin/main
- name: Test affected projects
run: nx affected --target=test --base=origin/main
文档
指南
参考文献
设置
- CI/CD 设置指南 - 配置连续集成
- E2E 测试指南 - 端到端测试设置
- 建筑 - 技术架构概览
所需资源
- 无: 21.6.4或以上
- 天体: 5.x, 6.x, 或 7.x
- 节点.js:22.12.0或更高(所有支持的Astro版本的全项目最低标准)
- 类型脚本: 5.9.x或 6.x (中文(简体) )
astro check尚不支持类型Script 7)
Astro 5.x和 6.x仍然被这个插件所支持,但它们使用相同的节点.js 22.12.0+基线作为Astro 7.x.
版本支持
| nx- 天体 | 天体 | 节点.js | 状态 |
|---|---|---|---|
| 1.x 运算 | 5.x 电话 | 第22.12.0号决定 | 活动 |
| 1.x 运算 | 6.x (单位:千美元) | 第22.12.0号决定 | 活动 |
| 2.x 数据 | 5.x 电话 | 第22.12.0号决定 | 活动 |
| 2.x 数据 | 6.x (单位:千美元) | 第22.12.0号决定 | 活动 |
| 2.x 数据 | 7.x 电话 | 第22.12.0号决定 | 活动 |
Astro 版本选择
在初始化插件时, 您可以指定要安装的 Astro 格式 :
# Install with Astro 7.x (default)
nx g @geekvetica/nx-astro:init
# Install with Astro 7.x explicitly
nx g @geekvetica/nx-astro:init --astro-version=7
# Install with Astro 6.x
nx g @geekvetica/nx-astro:init --astro-version=6
# Install with Astro 5.x
nx g @geekvetica/nx-astro:init --astro-version=5
该插件会自动检测到现有的Astro安装并使用该版本范围.
注意:无论Astro主版本选择如何,此插件都需要22.12.0或更高.
从Astro 6 迁移到Astro 7
如果您有使用 Astro 6 的现有项目, 并且想要升级到 Astro 7 :
-
更新您的工作空间依赖性 :
pnpm add -D astro@^7.3.6 @astrojs/node@^11.1.7更新任何其他
@astrojs/*与Astro 7的相容主机融合. -
节点.js要求不变(22.12.0+)
-
Astro 7 键断键变化 :
- Vite 8( 请检查Vite 插件兼容性)
- 基于 Rust 的编译器取代了 Go 编译器,并对无效的 HTML 更为严格(例如现在的未关闭标记出错). 删除
experimental.rustCompiler如果设置. compressHTML默认为'jsx'设置compressHTML: true以保留先前的 HTML 意识白空间处理.- Markdown默认由Sätteri提供. 要保留备注/恢复插件, 安装
@astrojs/markdown-remark设置markdown.processor: unified()。 。 。 . - 高级路由稳定并使用
src/fetch.ts作为它的入门点。 移动experimental.advancedRouting.fetchFile到顶层fetchFile选项(或设置)fetchFile: null以禁用). @astrojs/db编辑astro db编号 :astro login编号 :astro linkCLI 命令被删除.- 折旧
astro:transitions帮助者( e)TRANSITION_*常数,isTransition*Event(), (中文(简体) ),createAnimationScope()页:1.
-
astro dev当检测到AI编码代理时, 现在解入背景 。 Nx -Astro号dev执行器将服务器保存在前景( 它设置)ASTRO_DEV_BACKGROUND=0所以Nx可以监督它。 设定ASTRO_DEV_BACKGROUND=1自己选择回去.
从Astro 5搬到Astro 6
如果您有使用 Astro 5 的现有项目,并想要升级到 Astro 6 :
-
更新您的工作空间依赖性 :
pnpm add -D astro@^6.2.0 @astrojs/node@^10.0.0 -
确保节点.js版本为22.12.0或更高
-
更新内容集以使用内容层 API( 遗产集在 Astro 6 中被删除)
-
如果您需要迁移帮助, 请启用您的遗留兼容旗
astro.config.mjs数字 :export default defineConfig({ legacy: { collectionsBackwardsCompat: true, }, }); -
Astro 6 中键断切变化 :
- 节点18/20支持下降(需要22.12.0+)
- Vite 7.0 (检查Vite插件兼容性)
- Zod 4.0 (如果使用自定义验证, 更新内容计划)
Astro.glob()删除( 使用import.meta.glob()改为)- CJS 配置文件已删除( 使用)
.mjs或者说.ts(中文(简体) ) - 遗留内容收藏已删除( 复制到内容层 API)
发电机
| 发电机 | 说明 |
|---|---|
init | 初始化 nx- astro 插件 |
application | 从模板创建新的 Astro 应用程序 |
import | 将已有的 Astro 项目导入工作空间 |
component | 生成 Astro 组件 |
执行者
| 执行者 | 说明 |
|---|---|
dev | 用 HMR 运行开发服务器 |
build | 生产建筑 |
preview | 预览制作建筑 |
check | 运行类型脚本类型检查 |
test | 运行虚拟测试 |
sync | 生成内容收藏类型 |
实例
多站点的 Monorepo
my-workspace/
├── apps/
│ ├── marketing/ # Main website
│ ├── blog/ # Company blog
│ └── docs/ # Documentation
└── libs/
└── shared-ui/ # Shared components
# Build all sites
nx run-many --target=build --all
# Run dev servers with different ports
nx dev marketing # port 4321
nx dev blog # port 4322
nx dev docs # port 4323
内容收藏博客
# Create blog
nx g @geekvetica/nx-astro:application my-blog --template=blog
# Generate types for content
nx sync my-blog
# Add posts to src/content/blog/
# Types are automatically updated
共享组件库
# Create library
nx g @nx/js:library shared-ui
# Add components
# libs/shared-ui/src/components/Button.astro
# libs/shared-ui/src/components/Card.astro
# Use in apps
import { Button, Card } from '@my-org/shared-ui';
社区
捐款
欢迎捐款! 见 捐款额 准则.
许可证
麻省理工学院
支助
有问题吗? 检查 解决问题指南 或者说 打开一个问题。 。 。 .
更改日志
见 释放 用于版本历史.