|
| 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 | +Microbundle is a zero-configuration bundler for tiny JavaScript libraries, powered by Rollup. It takes source code and produces multiple output formats (modern ES modules, CommonJS, UMD) with minimal setup. |
| 8 | + |
| 9 | +## Development Commands |
| 10 | + |
| 11 | +### Building |
| 12 | + |
| 13 | +- `npm run build` - Full build: first builds with Babel, then self-builds |
| 14 | +- `npm run build:babel` - Build using Babel only |
| 15 | +- `npm run build:self` - Self-build using microbundle's own dist output |
| 16 | +- `npm run prepare` - Runs full build (called before npm publish) |
| 17 | + |
| 18 | +### Testing |
| 19 | + |
| 20 | +- `npm test` - Run linter, build, and Jest tests |
| 21 | +- `npm run jest` - Run Jest tests only (without lint/build) |
| 22 | +- Test timeout is set to 11 seconds for fixture tests |
| 23 | + |
| 24 | +### Linting and Formatting |
| 25 | + |
| 26 | +- `npm run lint` - Run ESLint on src directory |
| 27 | +- `npm run format` - Format all JS and CSS files with Prettier |
| 28 | + |
| 29 | +### Running Tests |
| 30 | + |
| 31 | +Individual tests can be run with Jest's standard CLI options: |
| 32 | + |
| 33 | +```bash |
| 34 | +npm run jest -- --testNamePattern="build shebang" |
| 35 | +npm run jest -- test/index.test.js |
| 36 | +``` |
| 37 | + |
| 38 | +## Architecture |
| 39 | + |
| 40 | +### Entry Points |
| 41 | + |
| 42 | +- **src/cli.js** - CLI entry point (shebang script), parses arguments and invokes main function |
| 43 | +- **src/index.js** - Main bundling logic, exports the `microbundle()` function |
| 44 | +- **src/prog.js** - CLI command definitions using `sade` library |
| 45 | + |
| 46 | +### Core Build Flow |
| 47 | + |
| 48 | +1. **Input Resolution** (`getInput()` in src/index.js:205) |
| 49 | + |
| 50 | + - Resolves entry files from CLI args, `source` field in package.json, or defaults (src/index.js, index.js) |
| 51 | + - Supports glob patterns for multiple entries |
| 52 | + - Handles TypeScript (.ts/.tsx) and JavaScript files |
| 53 | + |
| 54 | +2. **Output Resolution** (`getOutput()` in src/index.js:227) |
| 55 | + |
| 56 | + - Determines output location from CLI args or package.json `main` field |
| 57 | + - Defaults to dist/ directory |
| 58 | + |
| 59 | +3. **Format Generation** (`getMain()` in src/index.js:278) |
| 60 | + |
| 61 | + - Maps each format (modern, es, cjs, umd) to appropriate output filenames |
| 62 | + - Reads from package.json fields: `module`, `main`, `exports`, `unpkg`, etc. |
| 63 | + - Respects `{"type":"module"}` for ES Module packages |
| 64 | + |
| 65 | +4. **Configuration Creation** (`createConfig()` in src/index.js:327) |
| 66 | + |
| 67 | + - Creates Rollup configuration for each entry/format combination |
| 68 | + - Configures plugins: Babel, TypeScript, PostCSS, Terser, etc. |
| 69 | + - Handles externals (dependencies vs devDependencies) |
| 70 | + - Manages source maps, compression, and name caching |
| 71 | + |
| 72 | +5. **Build Execution** |
| 73 | + - Sequential builds with caching (cjs format builds first) |
| 74 | + - Watch mode available via Rollup's watch API |
| 75 | + |
| 76 | +### Format Types |
| 77 | + |
| 78 | +- **modern** - ES2017+ with modern syntax (async/await, arrow functions) for `<script type="module">` |
| 79 | +- **es** (esm) - Transpiled ES modules for older bundlers |
| 80 | +- **cjs** - CommonJS for Node.js |
| 81 | +- **umd** - Universal Module Definition for CDNs and older environments |
| 82 | +- **iife** - Immediately Invoked Function Expression |
| 83 | + |
| 84 | +### Key Subdirectories |
| 85 | + |
| 86 | +**src/lib/** - Utility modules: |
| 87 | + |
| 88 | +- **babel-custom.js** - Custom Babel plugin configuration |
| 89 | +- **package-info.js** - Reads and normalizes package.json config |
| 90 | +- **terser.js** - Minification options normalization |
| 91 | +- **css-modules.js** - CSS Modules configuration logic |
| 92 | +- **compressed-size.js** - Calculates gzipped/brotli bundle sizes |
| 93 | +- **option-normalization.js** - Parses CLI arguments (--alias, --define, etc.) |
| 94 | + |
| 95 | +**test/fixtures/** - Integration test fixtures (each subdirectory is a test case with package.json and source) |
| 96 | + |
| 97 | +### External Dependencies Handling |
| 98 | + |
| 99 | +- **Externals** (src/index.js:331-357): By default, `dependencies` and `peerDependencies` are external (not bundled) |
| 100 | +- **Bundled**: `devDependencies` are bundled into the output |
| 101 | +- Override with `--external` flag or `--external none` to bundle everything |
| 102 | + |
| 103 | +### Special Features |
| 104 | + |
| 105 | +**TypeScript Support** (src/index.js:533-564): |
| 106 | + |
| 107 | +- Automatically detected by .ts/.tsx file extension |
| 108 | +- Uses rollup-plugin-typescript2 |
| 109 | +- Generates declaration files when `types` or `typings` is set in package.json |
| 110 | +- Respects tsconfig.json with overrides for `module: "ESNext"` and `target: "esnext"` |
| 111 | + |
| 112 | +**CSS Handling** (src/index.js:490-502): |
| 113 | + |
| 114 | +- PostCSS with autoprefixer |
| 115 | +- CSS Modules support (files ending in .module.css or via --css-modules flag) |
| 116 | +- Output modes: external (default) or inline |
| 117 | + |
| 118 | +**Property Mangling** (src/index.js:385-433): |
| 119 | + |
| 120 | +- Reads from `mangle.json` or package.json `mangle`/`minify` field |
| 121 | +- Name cache persisted across builds for consistent output |
| 122 | + |
| 123 | +**Worker Support** (src/index.js:648): |
| 124 | + |
| 125 | +- Bundles Web Workers via @surma/rollup-plugin-off-main-thread |
| 126 | +- Only works with es/modern formats |
| 127 | +- Enable with --workers flag |
| 128 | + |
| 129 | +### Testing Structure |
| 130 | + |
| 131 | +- **test/index.test.js** - Main test suite |
| 132 | +- **test/fixtures/** - Each subdirectory contains a mini-project to build |
| 133 | +- Tests snapshot the build output and directory structure |
| 134 | +- Uses Jest with a custom build harness |
| 135 | + |
| 136 | +## Common Development Patterns |
| 137 | + |
| 138 | +### Adding New CLI Options |
| 139 | + |
| 140 | +1. Add option definition in src/prog.js |
| 141 | +2. Access via `options.<name>` in src/index.js |
| 142 | +3. Pass to relevant plugin configuration in `createConfig()` |
| 143 | + |
| 144 | +### Adding Rollup Plugins |
| 145 | + |
| 146 | +Add to the plugins array in src/index.js:488-701, typically with conditional logic and `.filter(Boolean)` to remove falsy entries |
| 147 | + |
| 148 | +### Debugging Builds |
| 149 | + |
| 150 | +- Use `--no-compress` to disable minification |
| 151 | +- Check rollup warnings in `onwarn` handler (src/index.js:469-482) |
| 152 | +- Modern format disables Rollup cache (src/index.js:437) to prevent legacy transpilation leaking |
| 153 | + |
| 154 | +## Important Notes |
| 155 | + |
| 156 | +- The build is "self-hosted": microbundle builds itself (see build:self script) |
| 157 | +- CJS format always builds first to populate cache for other formats (src/index.js:109) |
| 158 | +- Modern format uses Babel's bugfixes mode to target browsers with `<script type="module">` support |
| 159 | +- Shebang lines are extracted and re-added to preserve them (src/index.js:524-531, 712-713) |
0 commit comments