Public Wiki51,523View on GitHub
Want prettier/prettier knowledge in your AI?

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

AngularReactTypeScriptVue.jsGraphQLJest

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

index.js
src/index.js

Application entry point

Start here to understand how the application initializes

Coding Conventions

Standards and patterns used in this codebase

Module System

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';
index.jsstandalone.js
Architecture

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.
scripts/generate-builtin-plugins.js
Formatting Pipeline

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 string
src/main/plugins
Configuration

Use 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.js
prettier.config.jseslint.config.jsjest.config.jsknip.config.js
Code Generation

Use 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 exports
scripts/generate-builtin-plugins.jsscripts/generate-changelog.jsscripts/generate-schema.js
File Naming

Use 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.cjs
puppeteer.config.cjsscripts/build-website.jsscripts/lint-changelog.js
Platform Abstraction

Use 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 usage
standalone.js
Testing

Use 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 settings
jest.config.js
External Packages

External 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 plugins
knip.config.js
Environment-Aware Configuration

Configuration 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
puppeteer.config.cjs