简体   繁体   English

为单个文件禁用 prettier

[英]Disable prettier for a single file

I need to disable prettier for a single file (API URLs file) in my project in Vs-code.我需要在我的 Vs-code 项目中为单个文件(API URL 文件)禁用prettier actually, I need each API and its URL to be in one line, but prettier breaks them in two lines.实际上,我需要每个 API 及其 URL 都在一行中,但 prettier 将它们分成两行。

before

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

after

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

If you want a certain file in a repo to never be formatted by prettier, you can add it to a .prettierignore file: Disable Prettier for one file如果您希望 repo 中的某个文件永远不会被 prettier 格式化,您可以将其添加到 .prettierignore 文件中:为一个文件禁用Prettier

From the docs:从文档:

To exclude files from formatting, create a .prettierignore file in the root of your project.要从格式中排除文件,请在项目的根目录中创建一个 .prettierignore 文件。 .prettierignore uses gitignore syntax. .prettierignore 使用gitignore语法。

Example:例子:

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

Thanks to evolutionxbox , so far two solutions were found.感谢evolutionxbox ,到目前为止找到了两种解决方案。

  1. Extension延期

We can use an extension to toggle formatting like prettier on the specific page when you need it.我们可以使用扩展程序在需要时在特定页面上切换格式,例如更漂亮。

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

  1. Ignoring Code in prettier忽略代码更漂亮

Prettier offers an escape hatch to ignore a block of code or prevent entire files from being formatted. Prettier 提供了一个逃生舱来忽略代码块或阻止整个文件被格式化。

Ignoring Files忽略文件

To exclude files from formatting, add entries to a .prettierignore file in the project root or set the --ignore-path CLI option.要从格式中排除文件,请将条目添加到项目root.prettierignore文件或设置--ignore-path CLI 选项。 .prettierignore uses gitignore syntax. .prettierignore使用 gitignore 语法。

/app/src/scripts/example.js

JavaScript JavaScript

A JavaScript comment of // prettier-ignore will exclude the next node in the abstract syntax tree from formatting. // prettier-ignore JavaScript 注释将从格式化中排除抽象语法树中的下一个节点。

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 JSX

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

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

create .prettierignore file in the root of your repo and add the name of the folders that you want to ignore and add full path of the file that you want to ignore and save it.在 repo 的根目录中创建 .prettierignore 文件,并添加要忽略的文件夹的名称,并添加要忽略的文件的完整路径并保存。

use the .gitignore format to update your file you can read about it in the prettier website too https://prettier.io/docs/en/ignore.html#ignoring-files使用 .gitignore 格式更新你的文件,你也可以在更漂亮的网站上阅读它https://prettier.io/docs/en/ignore.html#ignoring-files

Another option is to use the prettier block-like toggle, to disable formatting for a "block" within a file.另一种选择是使用更漂亮的块状切换,禁用文件中“块”的格式化。 For example, adding \/\/ prettier-ignore<\/code> before the start of a function definition, will disable prettier formatting for that function.例如,在函数定义开始前添加\/\/ prettier-ignore<\/code> ,将禁用该函数的更漂亮的格式。

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

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

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

. . prettierignore for React project 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