Skip to content

chore: format committed files using Prettier#495

Open
skirtles-code wants to merge 1 commit intostackblitz-labs:mainfrom
skirtles-code:lint-git-hook
Open

chore: format committed files using Prettier#495
skirtles-code wants to merge 1 commit intostackblitz-labs:mainfrom
skirtles-code:lint-git-hook

Conversation

@skirtles-code
Copy link
Copy Markdown
Contributor

This PR adds automatic formatting of code when committing to git.

  • simple-git-hooks is used to add a pre-commit git hook to run lint-staged.
  • lint-staged is used to run Prettier. It will only run on staged files, i.e. those included in the commit.

The formatting is already checked as part of the CI. This just helps contributors to avoid the headache of changes failing the CI due to trivial formatting problems.

Why use simple-git-hooks?

It's the most common choice in the Vite & Vue ecosystems, so that's what I'm used to.

Why the postinstall?

That ensures simple-git-hooks will run for every pnpm install, so that the git hook is kept in sync. You'll see something similar in projects like vuejs/core and vitejs/vite.

While the config doesn't tend to change often, contributors tend not to run simple-git-hooks manually, so if the config does change it can be problematic getting contributors to run it.

The docs for simple-git-hooks advise against using postinstall, but that only applies if the package.json is published to npm, which it isn't in this case.

Why is simple-git-hooks: false in allowBuilds?

simple-git-hooks has its own postinstall script that runs when the package is first installed. But it doesn't run for every subsequent pnpm install, which can lead to the config getting out of sync. As noted earlier, we have our own postinstall script to handle that, so we don't need the postinstall script from simple-git-hooks itself.

Why --ignore-unknown?

When running Prettier for the whole project, e.g. via pnpm run format, it'll ignore any files with unknown extensions.

But as we're only formatting staged files, Prettier will be a little stricter and will throw an error for any files with unknown extensions. The --ignore-unknown option tells it to ignore those files.

This is the same as the example configuration at:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants