Files
test-repo/website/versioned_docs/version-stable/ignore.md
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

194 lines
4.0 KiB
Markdown
Raw Normal View History

---
id: ignore
title: Ignoring Code
---
2020-07-01 08:44:21 +02:00
Use `.prettierignore` to ignore (i.e. not reformat) certain files and folders completely.
2020-07-01 08:44:21 +02:00
Use “prettier-ignore” comments to ignore parts of files.
2020-07-01 08:44:21 +02:00
## Ignoring Files: .prettierignore
To exclude files from formatting, create a `.prettierignore` file in the root of your project. `.prettierignore` uses [gitignore syntax](https://git-scm.com/docs/gitignore#_pattern_format).
Example:
2023-07-05 21:41:09 +09:00
```text
2020-07-01 08:44:21 +02:00
# Ignore artifacts:
build
coverage
# Ignore all HTML files:
2023-07-05 21:41:09 +09:00
**/*.html
2020-07-01 08:44:21 +02:00
```
It’s recommended to have a `.prettierignore` in your project! This way you can run `prettier --write .` to make sure that everything is formatted (without mangling files you don’t want, or choking on generated files). And – your editor will know which files _not_ to format!
2024-11-26 13:49:17 +09:00
By default prettier ignores files in version control systems directories (".git", ".jj", ".sl", ".svn" and ".hg") and `node_modules` (unless the [`--with-node-modules` CLI option](cli.md#--with-node-modules) is specified). Prettier will also follow rules specified in the ".gitignore" file if it exists in the same directory from which it is run.
So by default it will be
2023-07-05 21:41:09 +09:00
```text
**/.git
**/.svn
**/.hg
**/node_modules
```
and
2023-07-05 21:41:09 +09:00
```text
**/.git
**/.svn
**/.hg
```
if [`--with-node-modules` CLI option](cli.md#--with-node-modules) provided
2020-07-01 08:44:21 +02:00
(See also the [`--ignore-path` CLI option](cli.md#--ignore-path).)
## JavaScript
A JavaScript comment of `// prettier-ignore` will exclude the next node in the abstract syntax tree from formatting.
For example:
<!-- prettier-ignore -->
```js
matrix(
1, 0, 0,
0, 1, 0,
0, 0, 1
)
// prettier-ignore
matrix(
1, 0, 0,
0, 1, 0,
0, 0, 1
)
```
will be transformed to:
```js
matrix(1, 0, 0, 0, 1, 0, 0, 0, 1);
// prettier-ignore
matrix(
1, 0, 0,
0, 1, 0,
0, 0, 1
)
```
## JSX
```jsx
<div>
{/* prettier-ignore */}
<span ugly format='' />
</div>
```
## HTML
```html
<!-- prettier-ignore -->
<div class="x" >hello world</div >
<!-- prettier-ignore-attribute -->
<div
(mousedown)=" onStart ( ) "
(mouseup)=" onEnd ( ) "
></div>
<!-- prettier-ignore-attribute (mouseup) -->
<div
(mousedown)="onStart()"
(mouseup)=" onEnd ( ) "
></div>
```
## CSS
```css
/* prettier-ignore */
.my ugly rule
{
}
```
## Markdown
```markdown
<!-- prettier-ignore -->
Do not format this
```
### Range Ignore
_available in v1.12.0+_
This type of ignore is only allowed to be used in top-level and aimed to disable formatting for auto-generated content, e.g. [`all-contributors`](https://github.com/kentcdodds/all-contributors), [`markdown-toc`](https://github.com/jonschlinkert/markdown-toc), etc.
```markdown
<!-- prettier-ignore-start -->
<!-- SOMETHING AUTO-GENERATED BY TOOLS - START -->
| MY | AWESOME | AUTO-GENERATED | TABLE |
|-|-|-|-|
| a | b | c | d |
<!-- SOMETHING AUTO-GENERATED BY TOOLS - END -->
<!-- prettier-ignore-end -->
```
2025-02-09 20:21:04 +09:00
**Important:** You must have a blank line before `<!-- prettier-ignore-start -->` and `<!-- prettier-ignore-end -->` for Prettier to recognize the comments.
2020-07-01 08:44:21 +02:00
## YAML
To ignore a part of a YAML file, `# prettier-ignore` should be placed on the line immediately above the ignored node:
```yaml
# prettier-ignore
key : value
hello: world
```
## GraphQL
```graphql
{
# prettier-ignore
addReaction(input:{superLongInputFieldName:"MDU6SXNzdWUyMzEzOTE1NTE=",content:HOORAY}) {
reaction {content}
}
}
```
2020-03-21 21:59:05 +02:00
## Handlebars
<!-- prettier-ignore -->
```hbs
{{! prettier-ignore }}
<div>
"hello! my parent was ignored"
{{#my-crazy-component "shall" be="preserved"}}
<This
is = "also preserved as is"
/>
{{/my-crazy-component}}
</div>
```
2021-05-10 00:00:41 +03:00
## Command Line File Patterns
For one-off commands, when you want to exclude some files without adding them to `.prettierignore`, negative patterns can come in handy:
```bash
2023-07-05 21:41:09 +09:00
prettier . "!**/*.{js,jsx,vue}" --write
2021-05-10 00:00:41 +03:00
```
See [fast-glob](https://prettier.io/docs/cli#file-patterns) to learn more about advanced glob syntax.