18

Looking at the Prettier docs, there is a tip to base your .prettierignore file on your .gitignore file.

From the docs:

Tip! Base your .prettierignore on .gitignore and .eslintignore (if you have one).

Can someone explain what this means and how to do this? Is there a way to automatically add everything to your .prettierignore included in your .gitignore?

starball
  • 20,030
  • 7
  • 43
  • 238
alanrzhang
  • 199
  • 1
  • 1
  • 7

5 Answers5

31

You can actually pass a flag to your prettier command --ignore-path and then give it the path of your .gitignore file so it will use that instead of the .prettierignore file.

prettier --ignore-path .gitignore
  • 3
    Shame that you can only give that option once (last one takes precedence): https://github.com/prettier/prettier/issues/8048 E.g. I have submodule which I need to ignore for prettier but cannot be in the .gitignore. – Ciro Santilli OurBigBook.com Dec 29 '21 at 17:04
  • This solution may work for the command line, but this is usually not the best strategy (at least alone), because if you have a prettier IDE plugin, then that plugin will not know about the fact that you want to use .gitignore. (That is, unless you can configure your IDE's prettier plugin like shown in https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode#prettier.ignorepath-default.prettierignore , but that is more work and may have complications, such as not working in an untrusted workspace). – davidbludlow Jan 17 '23 at 20:19
  • I think the info here (including in the comments) will be outdated in Prettier 3.0. See [my answer](https://stackoverflow.com/a/76608483/11107541) – starball Jul 03 '23 at 22:45
8

They're not implying there's an automatic way to do this, but you could cp .gitignore .prettierignore (or copy it some other way, such as in your IDE). There is a proposal to include other ignore files as well as a node tool to automate this for you if you want to, though.

Zac Anger
  • 6,983
  • 2
  • 15
  • 42
4

Don't copy the file, link it to your .gitignore so they keep in sync if you edit them in the future:

ln -s .gitignore .prettierignore
Yann Dìnendal
  • 1,422
  • 2
  • 17
  • 27
  • 1
    Doesn't that create a link with a full path? How is it going to work in a repository which is developed by a team? – buraky Dec 27 '22 at 18:46
  • @buraky: From what I see it seems to create a relative symlink, it doesn't include my full path. It works in our git setup. – Yann Dìnendal Jun 23 '23 at 13:46
3

From what I can tell from reading the docs is they mean to have the same files in your .prettierignore as you do in your .gitignore, since anything in .gitignore won't be check into your repository anyway, not that you can point the .prettierignore to .gitignore and have it use that file, though that would be cool. There's a similar issue raised and close here that mentions using symlink since they use the same structure.

BlackICE
  • 8,816
  • 3
  • 53
  • 91
2

The documentation being quoted in the question post is/was probably alluding to just copying the contents of your .gitignore and pasting it into your .prettierignore.

There's been other work on improving this for Prettier v3.0:

In Prettier 3.0, you can specify multiple ignore files on the commandline. See Accept multiple --ignore-paths #14332. Ex. prettier --ignore-path .gitignore --ignore-path .prettierignore . Actually you won't even need to do that, because...

In Prettier 3.0, .gitignore will be honoured by default. See pull request: Ignore .gitignored files #14731, follow up discussion in Ignoring .gitignoreed files by default is overkill. #14831.

If you're interested in the history of this, see these:

starball
  • 20,030
  • 7
  • 43
  • 238