簡體   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