src
This is the source code for Prettier, an opinionated code formatter that parses source code and reprints it with consistent style rules. It implements a plugin-based pipeline that takes source code, parses it into an AST, converts it to an intermediate document representation (Doc IR), and then prints it as a formatted output string, supporting JavaScript, TypeScript, CSS, HTML, Markdown, GraphQL, YAML, JSON, Handlebars, and more.
Tech Stack
Key Features
- Document IR
- Core Formatting Pipeline
- Plugin System
- JavaScript/TypeScript/Flow Plugin
- CSS/SCSS/Less Plugin
- HTML/Vue/Angular Plugin
- Markdown/MDX Plugin
- Config Resolution
- CLI
- Standalone Browser Build
- Playground & Website Build
Entry Points
Start here when working with this codebase
src/index.jsApplication entry point
Start here to understand how the application initializes
Coding Conventions
Standards and patterns used in this codebase
Use ES modules (import/export) throughout the codebase. Barrel export files re-export from internal src/ modules to provide clean public API surfaces.
export * from './src/index.js';Follow a plugin-based architecture where each language is a separate plugin module with parsing, printing, and comment handling. Plugins are lazy-loaded via generated builtin plugin files.
Language plugins in src/language-js, src/language-css, etc. each provide parsers, printers, and options. scripts/generate-builtin-plugins.js creates lazy-loaded exports.Core formatting follows a pipeline: parse source → AST → document IR (using builders like group, indent, line) → output string. The document IR is an intermediate representation decoupled from language-specific logic.
Source code → Parser → AST → Printer (produces Doc IR) → Printer algorithm → formatted stringUse dedicated config files at project root for each tool (ESLint, Jest, Knip, Prettier, Puppeteer). Prettier's own config uses .js extension with export default syntax.
export default { ... }; in prettier.config.jsUse scripts/ directory for code generation and build tooling. Generator scripts are CLI-executable and produce derived source files or artifacts (changelogs, schemas, plugin registrations).
scripts/generate-builtin-plugins.js scans plugin directories and creates lazy-loaded plugin exportsUse kebab-case for file and directory names. Configuration files use standard tool-specific naming conventions (e.g., eslint.config.js, jest.config.js). Only .cjs extension used for CommonJS-required files.
src/language-js, src/language-css, scripts/build-website.js, puppeteer.config.cjsUse a universal/ abstraction layer to handle platform differences between Node.js and browser environments, enabling a standalone browser build alongside the Node.js version.
src/universal/ provides platform-specific implementations; standalone.js re-exports from src/standalone.js for browser usageUse Jest as the test framework with configuration for custom test environments, snapshot testing, and path mappings. Tests are co-located or in dedicated test directories.
Jest config with testEnvironment, testPathPattern, and moduleNameMapper settingsExternal parser plugins (Hermes, OXC) are published as separate packages in a packages/ directory, keeping the core lightweight while allowing optional parser choices.
packages/ directory contains independently published parser pluginsConfiguration files use environment variables (e.g., CI) to conditionally adjust behavior for different execution contexts.
Puppeteer config checks CI environment to set cache directory and download behavior