|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## What This Is |
| 6 | + |
| 7 | +A Webpack plugin (`@cloudbeds/webpack-module-federation-types-plugin`) that generates and shares TypeScript type definitions between Webpack Module Federation micro-frontends. It has two main functions: |
| 8 | +1. **Compile types** — generates `.d.ts` files from exposed MF modules |
| 9 | +2. **Download types** — fetches `.d.ts` files from remote MF modules |
| 10 | + |
| 11 | +Published to GitHub Packages (`npm.pkg.github.com`). |
| 12 | + |
| 13 | +## Commands |
| 14 | + |
| 15 | +```bash |
| 16 | +npm run build # Compile TypeScript (tsc) to dist/ |
| 17 | +npm run watch # Compile in watch mode |
| 18 | +npm test # Run tests (vitest) |
| 19 | +npm run test:watch # Run tests in watch mode |
| 20 | +npm run test:coverage # Run tests with Istanbul coverage |
| 21 | +npm run lint # Biome check with auto-fix (--write --unsafe) |
| 22 | +npm run lint:ts # TypeScript type-check (tsc --noEmit) |
| 23 | +npm run check # lint + lint:ts + test (full validation) |
| 24 | +``` |
| 25 | + |
| 26 | +Run a single test file: `npx vitest run src/path/to/file.test.ts` |
| 27 | + |
| 28 | +## Architecture |
| 29 | + |
| 30 | +### Plugin Entry Point (`src/plugin.ts`) |
| 31 | +`ModuleFederationTypesPlugin` implements `WebpackPluginInstance`. It hooks into the Webpack compiler lifecycle: |
| 32 | +- `beforeRun` / `watchRun` — downloads remote types on startup |
| 33 | +- `afterEmit` — compiles types for exposed modules; in dev mode, also sets an interval to re-download remote types periodically |
| 34 | + |
| 35 | +The plugin discovers `ModuleFederationPlugin` options from the compiler's plugin list (supports MF v1, v2, and Rspack variants). |
| 36 | + |
| 37 | +### Type Compilation (`src/compileTypes/`) |
| 38 | +- `compileTypes.ts` — synchronous compilation using the TypeScript Compiler API with an in-memory emit host. Produces a single `index.d.ts` bundle. |
| 39 | +- `compileTypesAsync.ts` — runs compilation in a `Worker` thread to avoid blocking Webpack. Manages worker lifecycle (terminates previous workers on recompilation). |
| 40 | +- `compileTypesWorker.ts` — the worker thread entry point; calls `compileTypes` then `rewritePathsWithExposedFederatedModules`. |
| 41 | +- `rewritePathsWithExposedFederatedModules.ts` — rewrites `declare module` paths in the emitted `.d.ts` to use federated module names (e.g., `"appName/ExposedModule"` instead of internal file paths). Also handles module aliases and node_modules type inclusion. |
| 42 | + |
| 43 | +### Type Downloading (`src/downloadTypes/`) |
| 44 | +- `downloadTypes.ts` — orchestrates downloading `.d.ts` files from remote MF apps. Resolves remote entry URLs from direct config or manifest files, then downloads in parallel. |
| 45 | +- Helpers handle manifest fetching, URL resolution, and file writing to the local `@types/remotes/` directory. |
| 46 | + |
| 47 | +### CLI Binaries (`src/bin/`) |
| 48 | +- `make-federated-types` — standalone CLI to compile types without running Webpack (reads federation config from `federation.config.json` or webpack config) |
| 49 | +- `download-federated-types` — standalone CLI to download remote types |
| 50 | + |
| 51 | +### Key Types (`src/models/`) |
| 52 | +- `ModuleFederationTypesPluginOptions` — plugin configuration (directories, disable flags, remote URLs, manifest URLs) |
| 53 | +- `FederationConfig` — the MF plugin's `name`, `exposes`, `remotes`, `shared` config |
| 54 | +- `RemoteEntryUrls` / `RemoteManifestUrls` — URL mappings for remote type resolution |
| 55 | + |
| 56 | +## Code Style |
| 57 | + |
| 58 | +- **Biome** for linting and formatting: 2-space indent, single quotes, 100 char line width, no parens on single-param arrows |
| 59 | +- **Pre-commit hook** (simple-git-hooks): runs `npm run lint` |
| 60 | +- Output is CommonJS (`"module": "CommonJS"` in tsconfig) |
| 61 | +- Global `Dict` type is defined in `src/@types/utility.d.ts` |
| 62 | +- Tests are co-located with source in `__tests__/` subdirectories |
| 63 | + |
| 64 | +## CI |
| 65 | + |
| 66 | +The `push.yml` workflow runs lint, test, and build on non-main pushes. Releases are automated via semantic-release on `main`. |
0 commit comments