|
| 1 | +# pp-dev — Claude Code Instructions |
| 2 | + |
| 3 | +## What this project is |
| 4 | + |
| 5 | +`@metricinsights/pp-dev` is a Vite/Next.js-based **local development framework and build tool** for Metric Insights' Portal Pages. It: |
| 6 | +- Proxies API requests to a live MI instance |
| 7 | +- Injects a **dev panel** (minimize, template sync) into the served page |
| 8 | +- Synchronizes the built template back to MI via WebSocket-triggered `next build` / Vite build |
| 9 | + |
| 10 | +## Essential commands |
| 11 | + |
| 12 | +```bash |
| 13 | +npm run build # Build node + client bundles; also packs a .tgz |
| 14 | +npm run test # Unit + integration (vitest) |
| 15 | +npm run test:unit # Unit tests only |
| 16 | +npm run test:integration # Integration tests (forked processes) |
| 17 | +npm run audit:all # npm audit in root AND every tests/* package — always use this, never bare npm audit |
| 18 | +``` |
| 19 | + |
| 20 | +## After any dependency change |
| 21 | + |
| 22 | +Always verify with the **repo-wide audit**, not just root: |
| 23 | +```bash |
| 24 | +npm run audit:all |
| 25 | +``` |
| 26 | +This runs `npm audit` in root + `tests/test-commonjs`, `tests/test-nextjs`, `tests/test-nextjs-cjs`. All must exit 0. |
| 27 | + |
| 28 | +If test-fixture lockfiles need patching, add/update `overrides` in their `package.json` and run `npm install` there. |
| 29 | + |
| 30 | +## After changing root package source |
| 31 | + |
| 32 | +```bash |
| 33 | +npm run reinstall:all # builds dist/ + .tgz, then reinstalls in all test fixtures |
| 34 | +``` |
| 35 | + |
| 36 | +## Architecture overview |
| 37 | + |
| 38 | +``` |
| 39 | +src/cli.ts — 8 CLI commands: serve (default), next, build, changelog, … |
| 40 | +src/index.ts — withPPDev() Vite config builder; loads pp-dev.config.* |
| 41 | +src/plugin.ts — Vite plugin interface (re-exported) |
| 42 | +src/lib/ |
| 43 | + client.service.ts — WebSocket event handler (info-data, template:sync, …) |
| 44 | + dist.service.ts — Build artifact manager: backups, VERSION, BUILD-MANIFEST, zip |
| 45 | + dev-panel.ts — EJS panel injection + static asset middleware |
| 46 | + pp-ws-server.ts — Raw ws server for Next.js (Vite-WS-compatible facade) |
| 47 | + version-manifest.ts — VERSION file + BUILD-MANIFEST generation (shared) |
| 48 | + middleware/ — Request pipeline: redirect → proxy cache → load-pp-data → proxy-pass → rewrite-response |
| 49 | +src/plugins/ |
| 50 | + client-injection-plugin.ts — Vite transformIndexHtml: injects panel markup |
| 51 | + version-plugin.ts — Vite build hook: writes VERSION into dist |
| 52 | +src/client/ |
| 53 | + index.ts — Browser-side dev panel (sync button, minimize) |
| 54 | + hot-context.ts — import.meta.hot shim for Next.js WS transport |
| 55 | +``` |
| 56 | + |
| 57 | +**WebSocket transport:** Vite dev server uses Vite HMR WS. Next.js uses `PPDevHotServer` (raw `ws`, path `/@pp-dev-hmr`). The client picks whichever is available: `import.meta.hot ?? createPPDevHotContext()`. |
| 58 | + |
| 59 | +## Test structure |
| 60 | + |
| 61 | +| Suite | Config | Pool | Timeout | Location | |
| 62 | +|---|---|---|---|---| |
| 63 | +| Unit | `vitest.config.ts` | threads | 10 s | `tests/unit/**/*.spec.ts` | |
| 64 | +| Integration | `vitest.integration.config.ts` | forks | 30 s | `tests/integration/**/*.spec.ts` | |
| 65 | +| E2E | `playwright.config.ts` | browser | — | `e2e/` | |
| 66 | + |
| 67 | +Test fixtures (real apps installed with the local .tgz): |
| 68 | +- `tests/test-nextjs/` — ESM Next.js app |
| 69 | +- `tests/test-nextjs-cjs/` — CJS Next.js app |
| 70 | +- `tests/test-commonjs/` — CommonJS Vite app |
| 71 | + |
| 72 | +## Key conventions |
| 73 | + |
| 74 | +- **Dual ESM/CJS output** — Rollup builds `dist/esm/`, `dist/cjs/`, `dist/types/` from 4 entry points. |
| 75 | +- **Config caching** — `src/config.ts` caches loaded config (30 s) and `package.json` (60 s). |
| 76 | +- **Lazy heavy imports** — `esbuild`, `jsdom`, `sharp` are imported lazily to keep startup fast. |
| 77 | +- **Axios instance cache** — one Axios instance per base URL; `keepAlive: false` avoids max-listeners warnings. |
| 78 | +- **No bare `npm audit`** — always `npm run audit:all` so test fixtures are included. |
| 79 | + |
| 80 | +## PR message format |
| 81 | + |
| 82 | +See `.cursor/rules/pr-message-format.mdc`. Use emojis: 🚀 features, 🔧 fixes, 🔐 security, 🧪 tests, 🧹 chore. |
0 commit comments