繁体   English   中英

为单个文件禁用 prettier

[英]Disable prettier for a single file

我需要在我的 Vs-code 项目中为单个文件(API URL 文件)禁用prettier 实际上,我需要每个 API 及其 URL 都在一行中,但 prettier 将它们分成两行。

export const GET_SEARCH_TEACHERS = params => myexampleFunction_app_base(`teachers/search/${params.search}`);

export const GET_SEARCH_TEACHERS = params =>
myexampleFunction_app_base(`teachers/search/${params.search}`);

如果您希望 repo 中的某个文件永远不会被 prettier 格式化,您可以将其添加到 .prettierignore 文件中:为一个文件禁用Prettier

从文档:

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

例子:

 # Ignore artifacts: build coverage # Ignore all HTML files: *.html

感谢evolutionxbox ,到目前为止找到了两种解决方案。

  1. 延期

我们可以使用扩展程序在需要时在特定页面上切换格式,例如更漂亮。

格式切换https://marketplace.visualstudio.com/items?itemName=tombonnike.vscode-status-bar-format-toggle

  1. 忽略代码更漂亮

Prettier 提供了一个逃生舱来忽略代码块或阻止整个文件被格式化。

忽略文件

要从格式中排除文件,请将条目添加到项目root.prettierignore文件或设置--ignore-path CLI 选项。 .prettierignore使用 gitignore 语法。

/app/src/scripts/example.js

JavaScript

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

例如:

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

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

将转化为:

    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>

更多: https : //prettier.io/docs/en/ignore.html

在 repo 的根目录中创建 .prettierignore 文件,并添加要忽略的文件夹的名称,并添加要忽略的文件的完整路径并保存。

使用 .gitignore 格式更新你的文件,你也可以在更漂亮的网站上阅读它https://prettier.io/docs/en/ignore.html#ignoring-files

另一种选择是使用更漂亮的块状切换,禁用文件中“块”的格式化。 例如,在函数定义开始前添加\/\/ prettier-ignore<\/code> ,将禁用该函数的更漂亮的格式。

... (code up here is formatted by prettier)

// prettier-ignore
function noPrettierFormattingInHere(){
  ...
}

... (code down here is formatted by prettier)

. React 项目的 prettierignore

build/
node_modules/
internals/generators/
internals/scripts/
package-lock.json
yarn.lock
package.json
coverage

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM