Skip to content

本节总结 CLI 工具的相关选项配置

1. --check

检测代码,但不进行修复。只会报告错误

2. --write

检测代码,并进行修复。

3. --debug-check

If you're worried that Prettier will change the correctness of your code, add --debug-check to the command.

This will cause Prettier to print an error message if it detects that code correctness might have changed.

Note that --write cannot be used with --debug-check.

以上引用是官方网站原话

笔者测试了下,很多场景不怎么生效,用途不大。

4. --find-config-path

找出启用的 config 文件路径

shell
npx prettier --find-config-path src/main.js

# It will maybe print ".prettierrc.js"

5. --config

设置指定的 config 目录或文件

shell
npx prettier . --write --config path/to/config

6. --ignore-path

忽略文件

7. --list-different

类似于 --check,但是信息更加精简,只会列出不合规范的目标文件,没有其他多余信息。

shell
npx prettier . --single-quote --list-different

8. --no-config

不再寻找配置文件,只会使用 prettier 的内置配置。

9. --config-precedence

设置配置的优先级。有以下几种选项:

  1. cli-override默认CLI 选项配置的优先级高于 config 文件;

  2. file-overrideconfig 文件的优先级高于 CLI 选项配置;

  3. prefer-file: 如果 config 文件存在的话,会忽略 CLI 选项配置。否则,会采用 CLI 选项配置。

10. --no-editorconfig

当调用 resolveConfig 时,不解析 .editorconfig 文件。

阅读resolveConfig

11. --with-node-modules

Prettier 默认会忽略检测 node_modules 下的文件。

如果想要更改上述情形的话,可以使用 --with-node-modules

12. --log-level

日志等级,有以下可配置选项:

  1. error
  2. warn
  3. log 默认
  4. debug
  5. silent

13. --stdin-filepath

声明文件路径,Prettier 会将其当做 stdin 处理:

css
/* abc.css */
.name {
  display: none;
}
shell
$ cat abc.css | prettier --stdin-filepath abc.css
.name {
  display: none;
}

14. --ignore-unknown

用来忽略无法识别的文件类型。

假设某文件为 unknown.abcPrettier 指令为 prettier **/* --write

此时,执行指令,会报错:

txt
[error] No parser could be inferred for file "/path/to/unknown.abc".

为了保证指令的正常执行,可以修改指令为 prettier **/* --write --ignore-unknown

15. --no-error-on-unmatched-pattern

当某文件类型无法识别时,阻止报错。

譬如 prettier **/* --write --no-error-on-unmatched-pattern

16. --cache

运行 Prettier 时,使用 cache 缓存。

除非以下几项变化:

  1. Prettier 版本

  2. Node.js 版本

  3. Options

  4. file metadata,当设置 --cache-strategymetadata

  5. file content,当设置 --cache-strategycontent

17. --cache-location

默认的 cache 目录为 ./node_modules/.cache/prettier/.prettier-cache,可以通过 --cache-location 来自定义缓存目录。

譬如:

shell
prettier . --write --cache --cache-location=path/to/cache-file

如果不想使用 cache 的话,可以将缓存目录删除,重新执行指令

18. --cache-strategy

用来判断文件是否发生更新的缓存策略。

可以设置为 metadatacontent。默认为 content