score:45

Accepted answer

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

from the docs:

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 

score:4

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.

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

score:4

another option is to use the prettier block-like toggle, to disable formatting for a "block" within a file.
for example, adding // prettier-ignore before the start of a function definition, will disable prettier formatting for that function.
similarly, if you put the line above an if statement, only the if block is ignored.

basically a block is denoted by a pair of { } matching braces.

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

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

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

score:29

thanks to evolutionxbox, so far couple of solutions were found.

ignoring files or folders

to exclude files from formatting, add entries to a .prettierignore file in the project root or set the --ignore-path cli option. .prettierignore uses gitignore syntax.

/app/src/scripts/example.js
/app/src/folder/

ignore based on extension

to exclude files based on extesntion you can add entries to a .prettierignore file as well

*.html.erb

ignore lines

javascript

a javascript comment of // prettier-ignore will exclude the next node in the abstract syntax tree from formatting.

    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>

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

using an 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


Related Query

More Query from same tag