Skip to main content

忽略代码

使用 .prettierignore 完全忽略(即不重新格式化)某些文件和文件夹。

¥Use .prettierignore to ignore (i.e. not reformat) certain files and folders completely.

使用“prettier-ignore”注释来忽略部分文件。

¥Use “prettier-ignore” comments to ignore parts of files.

忽略文件:.prettierignore

¥Ignoring Files: .prettierignore

要从格式中排除文件,请在项目的根目录中创建一个 .prettierignore 文件。.prettierignore 使用 gitignore 语法

¥To exclude files from formatting, create a .prettierignore file in the root of your project. .prettierignore uses gitignore syntax.

示例:

¥Example:

# Ignore artifacts:
build
coverage

# Ignore all HTML files:
**/*.html

建议在你的项目中有一个 .prettierignore!通过这种方式,你可以运行 prettier --write . 以确保所有内容都已格式化(不会破坏你不想要的文件,或阻塞生成的文件)。而且 - 你的编辑器会知道哪些文件不应该格式化!

¥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!

默认情况下,prettier 会忽略版本控制系统目录(".git"、".jj"、".sl"、".svn" 和 ".hg")和 node_modules(除非指定了 --with-node-modules CLI 选项)中的文件。如果 ".gitignore" 文件存在于运行它的同一目录中,Prettier 还将遵循 ".gitignore" 文件中指定的规则。

¥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 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

**/.git
**/.svn
**/.hg
**/node_modules

¥and

**/.git
**/.svn
**/.hg

如果 --with-node-modules CLI 选项 提供

¥if --with-node-modules CLI option provided

(另见 --ignore-path CLI 选项。)

¥(See also the --ignore-path CLI option.)

JavaScript

// prettier-ignore 的 JavaScript 注释将从格式化中排除抽象语法树中的下一个节点。

¥A JavaScript comment of // prettier-ignore will exclude the next node in the abstract syntax tree from formatting.

例如:

¥For example:

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:

matrix(1, 0, 0, 0, 1, 0, 0, 0, 1);

// prettier-ignore
matrix(
1, 0, 0,
0, 1, 0,
0, 0, 1
)

JSX

<div>
{/* prettier-ignore */}
<span ugly format='' />
</div>

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

/* prettier-ignore */
.my ugly rule
{

}

Markdown

<!-- prettier-ignore -->
Do not format this

范围忽略

¥Range Ignore

在 v1.12.0+ 中可用

¥available in v1.12.0+

这种类型的忽略只允许在顶层使用,旨在禁用自动生成内容的格式,例如 all-contributorsmarkdown-toc

¥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, markdown-toc, etc.

<!-- 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 -->

重要:你必须在 <!-- prettier-ignore-start --><!-- prettier-ignore-end --> 之前有一个空行,以便 Prettier 识别注释。

¥Important: You must have a blank line before <!-- prettier-ignore-start --> and <!-- prettier-ignore-end --> for Prettier to recognize the comments.

YAML

要忽略 YAML 文件的一部分,# prettier-ignore 应该放在被忽略节点上方的行上:

¥To ignore a part of a YAML file, # prettier-ignore should be placed on the line immediately above the ignored node:

# prettier-ignore
key : value
hello: world

GraphQL

{
# prettier-ignore
addReaction(input:{superLongInputFieldName:"MDU6SXNzdWUyMzEzOTE1NTE=",content:HOORAY}) {
reaction {content}
}
}

Handlebars

{{! prettier-ignore }}
<div>
"hello! my parent was ignored"
{{#my-crazy-component "shall" be="preserved"}}
<This
is = "also preserved as is"
/>
{{/my-crazy-component}}
</div>

命令行文件模式

¥Command Line File Patterns

对于一次性命令,当你想排除一些文件而不将它们添加到 .prettierignore 时,否定模式可以派上用场:

¥For one-off commands, when you want to exclude some files without adding them to .prettierignore, negative patterns can come in handy:

prettier . "!**/*.{js,jsx,vue}" --write

请参阅 fast-glob 以了解有关高级 glob 语法的更多信息。

¥See fast-glob to learn more about advanced glob syntax.