Sub topic 10
Ignoring Files and Code Blocks
This chapter assumes you have installed Prettier and can run prettier . --write and prettier . --check from the command line. It also assumes you know what files Git tracks and what .gitignore does.
When you run prettier ., Prettier formats every supported file under the current directory. You usually do not want that for generated output, vendored libraries, or lockfiles. Prettier gives you two controls. Ignore files skip whole paths, and ignore comments skip single pieces of code.
What Prettier Skips by Default
Prettier 3 already skips some paths without any configuration:
node_modules, unless you pass--with-node-modules- Version control folders such as
.git - Any path listed in
.gitignoreor.prettierignorein the directory where you run Prettier
Build output in .gitignore, such as dist/, is therefore skipped automatically. You only need .prettierignore for files that Git tracks but Prettier should leave alone.
Writing a .prettierignore File
.prettierignore uses the same pattern syntax as .gitignore. Put it in the project root:
# Generated but committed
coverage/
public/vendor/
# Lockfiles change often and are not hand-edited
package-lock.json
pnpm-lock.yaml
# Fixtures whose exact formatting matters to tests
test/fixtures/**/*.jsTo check whether a specific file is ignored, run Prettier on it directly. An ignored file prints nothing and produces no error:
npx prettier --check package-lock.jsonIf you keep your ignore list somewhere else, point Prettier at it with --ignore-path. Passing this flag replaces the defaults, so list every file you want Prettier to read:
npx prettier . --check --ignore-path .gitignore --ignore-path config/.prettierignoreIgnoring a Single Block of Code
A prettier-ignore comment tells Prettier to leave the next AST node exactly as written. In JavaScript, that node is usually the next statement. This is useful for hand-aligned data:
// prettier-ignore
const matrix = [
1, 0, 0,
0, 1, 0,
0, 0, 1,
];
const formatted = [1, 2, 3]; // this line is still formattedThe comment syntax depends on the language:
| Language | Comment |
|---|---|
| JavaScript / TypeScript | `// prettier-ignore` |
| JSX | `{/* prettier-ignore */}` |
| CSS / SCSS | `/* prettier-ignore */` |
| YAML | `# prettier-ignore` |
| Markdown | `<!-- prettier-ignore -->` |
Markdown also supports ignoring a range:
<!-- prettier-ignore-start -->
| a | b |
|--|--|
| keep | this spacing |
<!-- prettier-ignore-end -->Exercise
Your project commits a generated file src/schema.generated.js, and dist/ is listed in .gitignore. Running prettier . --check fails on the schema file. What do you change, and do you need to add dist/ anywhere?
Show answer
Add src/schema.generated.js to .prettierignore. You do not need to add dist/, because Prettier 3 reads .gitignore by default and already skips it. The exception is a run with a custom --ignore-path that leaves out .gitignore.
Exercise
You put // prettier-ignore above function setup() { ... }. Which code does Prettier leave unformatted?
Show answer
Only the setup function declaration, because it is the next node. Code after the function is still formatted.