Astro
This guide walks through setting up NemCSS in an Astro project using @nemcss/vite via Astro's vite config option.
Vite plugin type compatibility
There is a known type compatibility issue between Vite plugins built against Vite 7 and Astro projects that still use Vite 6. You may see the following TypeScript error:
Type 'Plugin[]' is not assignable to type 'PluginOption'If you hit this, add // @ts-expect-error above the plugin line in your Astro config (see Step 3), or use @nemcss/postcss instead.
See withastro/astro#14030 for more details.
Step 1: Create an Astro project
npm create astro@latestpnpm create astro@latestyarn create astroStep 2: Install @nemcss/vite
npm install -D @nemcss/vitepnpm add -D @nemcss/viteyarn add -D @nemcss/vitebun add -D @nemcss/viteStep 3: Add the plugin to your Astro config
// astro.config.mjs
import { defineConfig } from "astro/config";
import { nemcss } from "@nemcss/vite";
export default defineConfig({
vite: {
plugins: [
// @ts-expect-error: Vite plugin type mismatch between Vite 6 (Astro) and Vite 7
nemcss(),
],
},
});Step 4: Initialize nemcss
npx nemcss initpnpm dlx nemcss inityarn dlx nemcss initThis creates nemcss.config.json and a design-tokens/ folder with example token files.
Step 5: Configure content paths
Edit nemcss.config.json to point at your Astro components and pages:
{
"content": ["src/**/*.astro", "src/**/*.ts", "src/**/*.tsx"],
"tokensDir": "design-tokens"
}Step 6: Add @nemcss base; to a global CSS file
Create src/styles/global.css:
@nemcss base;Step 7: Import the global CSS in your layout
---
// src/layouts/Layout.astro
import '../styles/global.css'
---
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>My Astro site</title>
</head>
<body>
<slot />
</body>
</html>Step 8: Use the generated classes
---
// src/pages/index.astro
import Layout from '../layouts/Layout.astro'
---
<Layout>
<h1 class="text-default">Hello NemCSS</h1>
<p class="p-md text-muted">Styled with design tokens.</p>
</Layout>Step 9: Start the dev server
npm run devpnpm devyarn devHMR is enabled by default. Your CSS updates without a full page reload when you change a token file, your config, or any content file.