alanrzhang
alanrzhang

Reputation: 389

How can I base a .prettierignore file on .gitignore?

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?

Update 2023-09-05 Editing to highlight @starball's answer, where they note that prettier now respects .gitignore files by default.

Upvotes: 22

Views: 18198

Answers (5)

starball
starball

Reputation: 51373

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:

Upvotes: 10

Yann Dìnendal
Yann Dìnendal

Reputation: 1627

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

Upvotes: 4

Stanley Thijssen
Stanley Thijssen

Reputation: 405

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

Upvotes: 38

BlackICE
BlackICE

Reputation: 8926

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.

Upvotes: 3

Zac Anger
Zac Anger

Reputation: 7787

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.

Upvotes: 7

Related Questions