In Vite, you can use .yak.ts or .yak.tsx files to run TypeScript/JavaScript at build time to generate design tokens or expand data. These files are evaluated in a sandboxed context, and the resulting values are injected into your CSS.
Note: For simple string constants, use standard .ts modules. Use .yak files only when you need build-time logic (loops, calculations, etc.) to generate styles.
// Base scale for spacing (in px)
const baseSpacing = [0, 4, 8, 12, 16, 24, 32, 40, 48];
export const spacing = Object.fromEntries(
baseSpacing.map((value, index) => [`s${index}`, `${value}px`]),
);
// Generate a simple color ramp
const baseHue = 220;
export const colors = Array.from({ length: 9 }, (_, i) => {
const lightness = 30 + i * 5;
return `hsl(${baseHue}, 80%, ${lightness}%)`;
});
/** @jsxImportSource next-yak */
import { styled } from "next-yak";
import { spacing, colors } from "./theme/tokens.yak";
const Card = styled.div`
padding: ${spacing.s3};
background-color: ${colors[4]};
border-radius: ${spacing.s2};
`;