Skip to content

Commit a81d107

Browse files
committed
docs: add CLAUDE.md
1 parent cd8b6d2 commit a81d107

1 file changed

Lines changed: 110 additions & 0 deletions

File tree

โ€ŽCLAUDE.mdโ€Ž

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# CLAUDE.md
2+
3+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4+
5+
## Project Overview
6+
7+
`vite-plugin-mock-dev-server` is a Vite plugin for mocking API endpoints during development. It intercepts HTTP requests matching configured proxy patterns and returns mocked responses defined in `.mock.ts` files.
8+
9+
## Architecture
10+
11+
### Plugin Entry Point
12+
13+
The main plugin is created in `vite-plugin-mock-dev-server/src/core/plugin.ts` via `mockDevServerPlugin()`. This returns an array of two Vite plugins:
14+
15+
- **serverPlugin** (`enforce: 'pre'`) - Handles dev server mock interception
16+
- **buildPlugin** (`enforce: 'post'`) - Generates standalone mock server during production build
17+
18+
### Request Flow
19+
20+
1. `serverPlugin` configures the Vite dev server via `configureServer` hook
21+
2. `initMockMiddlewares()` in `src/core/init.ts` creates the middleware chain
22+
3. `Compiler` class (`src/compiler/compiler.ts`) loads and watches mock files
23+
4. `createMockMiddleware()` (`src/mockHttp/middleware.ts`) intercepts requests and returns mocked responses
24+
25+
### Key Source Directories
26+
27+
- `src/core/` - Plugin lifecycle, option resolution, logger
28+
- `src/compiler/` - Mock file compilation using esbuild/rolldown, hot reload
29+
- `src/mockHttp/` - HTTP mock request matching, parsing, response handling, CORS
30+
- `src/mockWebsocket/` - WebSocket mock support
31+
- `src/helpers/` - User-facing helpers (`defineMock`, `defineMockData`, `createSSEStream`)
32+
- `src/recorder/` - Request recording and replay for proxy responses
33+
- `src/build/` - Standalone mock server generation for production builds
34+
- `src/cookies/` - Cookie handling via `cookies` package
35+
- `src/types/` - TypeScript type definitions
36+
37+
### Build Output
38+
39+
Uses `tsdown` (configured in `tsdown.config.ts`) to produce:
40+
41+
- `dist/index.js` - Main plugin export
42+
- `dist/helper.js` - Helper utilities for mock files
43+
- `dist/server.js` - Standalone server
44+
- `dist/types.d.ts` - Combined type definitions
45+
46+
## Commands
47+
48+
```bash
49+
# Build the plugin
50+
pnpm build
51+
52+
# Run tests (vitest)
53+
pnpm test
54+
55+
# Run tests in watch mode
56+
pnpm test -- --watch
57+
58+
# Run a single test file
59+
pnpm test -- src/__tests__/validator.spec.ts
60+
61+
# Lint
62+
pnpm lint
63+
64+
# Run example dev server
65+
pnpm dev
66+
67+
# Build example
68+
pnpm example:build
69+
70+
# Build documentation
71+
pnpm docs:build
72+
```
73+
74+
## Project Structure
75+
76+
```
77+
vite-plugin-mock-server/
78+
โ”œโ”€โ”€ pnpm-workspace.yaml # Monorepo workspace config
79+
โ”œโ”€โ”€ vitest.config.ts # Test configuration (runs spec.ts in __tests__)
80+
โ”œโ”€โ”€ eslint.config.js # ESLint using @pengzhanbo/eslint-config
81+
โ”œโ”€โ”€ vite-plugin-mock-dev-server/
82+
โ”‚ โ”œโ”€โ”€ src/
83+
โ”‚ โ”‚ โ”œโ”€โ”€ index.ts # Public exports
84+
โ”‚ โ”‚ โ”œโ”€โ”€ core/ # Plugin core (plugin.ts, options.ts, init.ts)
85+
โ”‚ โ”‚ โ”œโ”€โ”€ compiler/ # Mock file compilation
86+
โ”‚ โ”‚ โ”œโ”€โ”€ mockHttp/ # HTTP mock handling
87+
โ”‚ โ”‚ โ”œโ”€โ”€ mockWebsocket/ # WebSocket mock handling
88+
โ”‚ โ”‚ โ”œโ”€โ”€ helpers/ # defineMock, createSSEStream, etc.
89+
โ”‚ โ”‚ โ”œโ”€โ”€ recorder/ # Request recording/replay
90+
โ”‚ โ”‚ โ”œโ”€โ”€ build/ # Standalone server generation
91+
โ”‚ โ”‚ โ”œโ”€โ”€ cookies/ # Cookie handling
92+
โ”‚ โ”‚ โ”œโ”€โ”€ utils/ # Utility functions
93+
โ”‚ โ”‚ โ””โ”€โ”€ types/ # TypeScript types
94+
โ”‚ โ”œโ”€โ”€ __tests__/ # Test files
95+
โ”‚ โ””โ”€โ”€ dist/ # Build output
96+
โ”œโ”€โ”€ example/ # Example usage
97+
โ””โ”€โ”€ docs/ # VitePress documentation
98+
```
99+
100+
## Mock File Compilation
101+
102+
Mock files (`.mock.ts`, `.mock.js`, `.mock.json`, etc.) are compiled by the `Compiler` class using esbuild (with rolldown as fallback for Vite 7+). The compilation:
103+
104+
- Runs in parallel with concurrency limiting (64 files)
105+
- Injects `viteDefine` (from `config.define`) and `alias` resolution
106+
- Supports HMR when mock files or their dependencies change
107+
108+
## Note on Vite Version Compatibility
109+
110+
The plugin automatically selects `esbuild` or `rolldown` for compilation based on the Vite version. Peer dependencies `esbuild` and `rolldown` are optional but one is required for mock file compilation.

0 commit comments

Comments
ย (0)