|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance for Claude Code when working with the next-yak repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +next-yak is a CSS-in-JS solution for Next.js that combines styled-components syntax with build-time CSS extraction. It uses an SWC plugin (written in Rust) to transform TypeScript/JavaScript and extract CSS at compile time. |
| 8 | + |
| 9 | +## Repository Structure |
| 10 | + |
| 11 | +``` |
| 12 | +next-yak/ |
| 13 | +├── packages/ |
| 14 | +│ ├── next-yak/ # Main TypeScript/JavaScript package |
| 15 | +│ ├── yak-swc/ # SWC plugin (Rust → WASM) |
| 16 | +│ │ ├── yak_swc/ # Core SWC plugin implementation |
| 17 | +│ │ ├── css_in_js_parser/ # CSS-in-JS parser library |
| 18 | +│ │ └── relative_posix_path/ # Path utility |
| 19 | +│ ├── eslint-plugin-yak/ # ESLint plugin |
| 20 | +│ └── example/ # Next.js example (symlinked to examples/next-js) |
| 21 | +├── examples/ |
| 22 | +│ ├── next-js/ # Next.js example app |
| 23 | +│ └── vite/ # Vite example app |
| 24 | +├── docs/ # Documentation site (yak.js.org) |
| 25 | +├── cross-file-tests/ # Cross-file transformation tests |
| 26 | +└── benchmarks/ # Performance benchmarks |
| 27 | +``` |
| 28 | + |
| 29 | +## Common Commands |
| 30 | + |
| 31 | +All commands use **pnpm** (v10.15.0+). Run from the repository root unless specified. |
| 32 | + |
| 33 | +### Building |
| 34 | + |
| 35 | +```bash |
| 36 | +# Build the main next-yak TypeScript package |
| 37 | +pnpm build |
| 38 | + |
| 39 | +# Build the Rust SWC plugin (compiles to WASM) |
| 40 | +pnpm build:swc |
| 41 | + |
| 42 | +# Build everything (required before first run) |
| 43 | +pnpm build && pnpm build:swc |
| 44 | +``` |
| 45 | + |
| 46 | +### Testing |
| 47 | + |
| 48 | +```bash |
| 49 | +# Run all tests (builds first) |
| 50 | +pnpm test |
| 51 | + |
| 52 | +# Run tests in watch mode |
| 53 | +pnpm test:watch |
| 54 | + |
| 55 | +# Update test snapshots (both JS and Rust) |
| 56 | +pnpm test:snapshots |
| 57 | +``` |
| 58 | + |
| 59 | +### Running Examples |
| 60 | + |
| 61 | +```bash |
| 62 | +# Run Next.js example (requires build first) |
| 63 | +pnpm example |
| 64 | + |
| 65 | +# Run Vite example |
| 66 | +pnpm example:vite |
| 67 | + |
| 68 | +# Run documentation site |
| 69 | +pnpm docs |
| 70 | +``` |
| 71 | + |
| 72 | +### Package-Specific Commands |
| 73 | + |
| 74 | +```bash |
| 75 | +# Build only yak-swc WASM |
| 76 | +cd packages/yak-swc && pnpm build:yak |
| 77 | + |
| 78 | +# Run Rust tests only |
| 79 | +cd packages/yak-swc && pnpm test |
| 80 | + |
| 81 | +# Update Rust test snapshots |
| 82 | +cd packages/yak-swc && pnpm test:snapshots |
| 83 | + |
| 84 | +# Format Rust code |
| 85 | +cd packages/yak-swc && pnpm prettier |
| 86 | +``` |
| 87 | + |
| 88 | +## Key Files |
| 89 | + |
| 90 | +### TypeScript/JavaScript (packages/next-yak/) |
| 91 | +- `loaders/vite-plugin.ts` - Vite plugin implementation |
| 92 | +- `loaders/webpack-loader.ts` - Webpack loader |
| 93 | +- `loaders/turbo-loader.ts` - Turbopack loader |
| 94 | +- `withYak/index.ts` - Next.js config wrapper |
| 95 | +- `cross-file-resolver/` - Cross-file constant resolution |
| 96 | + |
| 97 | +### Rust (packages/yak-swc/yak_swc/src/) |
| 98 | +- `lib.rs` - Main SWC plugin entry point and visitor |
| 99 | +- `plugin.rs` - WASM plugin wrapper |
| 100 | +- `naming_convention.rs` - CSS class/variable naming |
| 101 | +- `yak_transforms.rs` - Transformation implementations |
| 102 | +- `yak_imports.rs` - Import tracking |
| 103 | +- `variable_visitor.rs` - Variable/constant tracking |
| 104 | + |
| 105 | +### Configuration |
| 106 | +- `pnpm-workspace.yaml` - Workspace configuration with version catalogs |
| 107 | +- `packages/yak-swc/Cargo.toml` - Rust dependencies |
| 108 | + |
| 109 | +## Development Workflow |
| 110 | + |
| 111 | +1. **After pulling changes**: Run `pnpm install` then `pnpm build && pnpm build:swc` |
| 112 | + |
| 113 | +2. **When modifying Rust code**: |
| 114 | + - Run `cargo test` in `packages/yak-swc/yak_swc` for quick iteration |
| 115 | + - Run `pnpm build:swc` to rebuild the WASM plugin for integration testing |
| 116 | + - The WASM target is `wasm32-wasip1` |
| 117 | + |
| 118 | +3. **When modifying TypeScript**: |
| 119 | + - Run `pnpm build` to rebuild |
| 120 | + - Use `pnpm watch` for automatic rebuilds |
| 121 | + |
| 122 | +4. **Testing changes**: |
| 123 | + - Use the examples (`pnpm example` or `pnpm example:vite`) to verify |
| 124 | + - Run `pnpm test` for the full test suite |
| 125 | + |
| 126 | +## SWC Fixture Tests |
| 127 | + |
| 128 | +The SWC plugin uses fixture-based snapshot testing located in `packages/yak-swc/yak_swc/tests/fixture/`. |
| 129 | + |
| 130 | +### How It Works |
| 131 | + |
| 132 | +Tests are auto-generated from fixture directories using `#[testing::fixture("tests/fixture/**/input.tsx")]` macro in `lib.rs`. Each fixture directory contains: |
| 133 | + |
| 134 | +``` |
| 135 | +fixture/ |
| 136 | +├── my-test-case/ |
| 137 | +│ ├── input.tsx # Input file (required) |
| 138 | +│ ├── output.dev.tsx # Dev mode output (generated) |
| 139 | +│ ├── output.prod.tsx # Prod mode output (generated) |
| 140 | +│ ├── output.turbo.dev.tsx # Turbopack dev output (generated) |
| 141 | +│ └── output.turbo.prod.tsx # Turbopack prod output (generated) |
| 142 | +``` |
| 143 | + |
| 144 | +### Adding a New Test |
| 145 | + |
| 146 | +1. Create a new directory: `mkdir tests/fixture/my-new-test` |
| 147 | +2. Create `input.tsx` with your test case |
| 148 | +3. Run `pnpm test:snapshots` to generate output files |
| 149 | +4. Review the generated outputs |
| 150 | + |
| 151 | +### Running Tests |
| 152 | + |
| 153 | +```bash |
| 154 | +# Run all Rust tests (from packages/yak-swc) |
| 155 | +cargo test |
| 156 | + |
| 157 | +# Run specific fixture test |
| 158 | +cargo test my_test_name |
| 159 | + |
| 160 | +# Update snapshots when output changes |
| 161 | +pnpm test:snapshots |
| 162 | + |
| 163 | +# Or using cargo directly with UPDATE flag |
| 164 | +UPDATE=1 cargo test |
| 165 | +``` |
| 166 | + |
| 167 | +### Test Configurations |
| 168 | + |
| 169 | +Each fixture generates 4 tests: |
| 170 | +- `fixture_dev` - Development mode with CSS modules |
| 171 | +- `fixture_prod` - Production mode with CSS modules |
| 172 | +- `fixture_dev_turbo` - Development mode with Turbopack (DataUrl) |
| 173 | +- `fixture_prod_turbo` - Production mode with Turbopack (DataUrl) |
| 174 | + |
| 175 | +## Architecture Notes |
| 176 | + |
| 177 | +### SWC Plugin Flow |
| 178 | +1. SWC passes AST to `TransformVisitor` in `lib.rs` |
| 179 | +2. Visitor identifies styled-components/CSS template literals |
| 180 | +3. CSS is extracted and transformed based on `CssDependencyMode`: |
| 181 | + - `InlineMatchResource` - Webpack inline resource syntax |
| 182 | + - `DataUrl` - Base64 encoded CSS (Turbopack) |
| 183 | + - `Custom` - Configurable import specifier (Vite) |
| 184 | +4. JavaScript is transformed to reference generated CSS classes |
| 185 | + |
| 186 | +### Vite Plugin |
| 187 | +- Uses virtual modules (`virtual:yak-css:*`) for CSS |
| 188 | +- The `{{__MODULE_PATH__}}` placeholder in import specifiers is replaced by the Rust plugin with the actual file path |
| 189 | +- CSS is resolved and bundled by Vite's CSS pipeline |
| 190 | + |
| 191 | +## Debugging |
| 192 | + |
| 193 | +Enable debug logging in Next.js config: |
| 194 | +```js |
| 195 | +export default withYak({ |
| 196 | + experiments: { |
| 197 | + debug: true, // or regex like 'component.tsx.css$' |
| 198 | + }, |
| 199 | +}); |
| 200 | +``` |
| 201 | + |
| 202 | +For Vite, debug logs are controlled the same way via plugin options. |
| 203 | + |
| 204 | +## Prerequisites |
| 205 | + |
| 206 | +- Node.js >= 22 |
| 207 | +- pnpm >= 10.15.0 |
| 208 | +- Rust toolchain (install from rust-lang.org, not brew) |
| 209 | +- WASM target: `rustup target add wasm32-wasip1` |
0 commit comments