2024-01-08 08:58:03 +01:00
|
|
|
---
|
2024-05-18 18:58:57 +02:00
|
|
|
layout: mini
|
2024-01-08 08:58:03 +01:00
|
|
|
permalink: /n/prettier.html
|
2024-06-06 15:50:35 +02:00
|
|
|
sitemap: true
|
|
|
|
robots: noai
|
2024-05-30 08:05:52 +02:00
|
|
|
lang: en
|
2024-06-06 15:50:35 +02:00
|
|
|
title: Prettier packages I use
|
|
|
|
excerpt: I use them both directly, and within pre-commit
|
2024-01-08 08:58:03 +01:00
|
|
|
---
|
|
|
|
|
2024-06-14 05:51:26 +02:00
|
|
|
_{{ page.excerpt }}_
|
2024-06-06 15:50:35 +02:00
|
|
|
|
|
|
|
<!-- editorconfig-checker-disable -->
|
|
|
|
<!-- prettier-ignore-start -->
|
|
|
|
|
|
|
|
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
|
|
|
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
|
|
|
<em lang="fi">Automaattinen sisällysluettelo</em> / <em lang="en">Automatically generated Table of Contents</em>
|
|
|
|
|
|
|
|
- [The packages](#the-packages)
|
|
|
|
- [Installation](#installation)
|
2024-06-07 05:43:15 +02:00
|
|
|
- [Configuration](#configuration)
|
2024-06-06 15:50:35 +02:00
|
|
|
- [`.pre-commit-config.yaml`](#pre-commit-configyaml)
|
|
|
|
- [Offline](#offline)
|
|
|
|
- [Further information](#further-information)
|
|
|
|
|
|
|
|
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
|
|
|
|
|
|
|
<!-- prettier-ignore-end -->
|
|
|
|
<!-- editorconfig-checker-enable -->
|
|
|
|
|
|
|
|
## The packages
|
2024-01-08 08:58:03 +01:00
|
|
|
|
2024-01-08 11:47:02 +01:00
|
|
|
- [@prettier/plugin-ruby](https://www.npmjs.com/package/@prettier/plugin-ruby)
|
|
|
|
- [@prettier/plugin-xml](https://www.npmjs.com/package/@prettier/plugin-xml)
|
2024-06-17 10:08:11 +02:00
|
|
|
- [prettier](https://www.npmjs.com/package/prettier)
|
|
|
|
- [prettier-plugin-nginx](https://www.npmjs.com/package/prettier-plugin-nginx)
|
2024-06-07 05:40:38 +02:00
|
|
|
- [prettier-plugin-sh](https://www.npmjs.com/package/prettier-plugin-sh)
|
2024-06-17 10:08:11 +02:00
|
|
|
- [prettier-plugin-toml](https://www.npmjs.com/package/prettier-plugin-toml)
|
2024-01-09 07:15:10 +01:00
|
|
|
|
2024-06-06 15:50:35 +02:00
|
|
|
## Installation
|
|
|
|
|
2024-06-21 06:02:31 +02:00
|
|
|
1. `corepack pnpm install -D -E prettier@latest prettier-plugin-nginx@latest @prettier/plugin-ruby@latest prettier-plugin-toml@latest @prettier/plugin-xml@latest prettier-plugin-sh@latest`
|
|
|
|
or probably just `corepack pnpm install -D` if it's not your project.
|
2024-06-19 07:19:44 +02:00
|
|
|
1. If they don't exist already
|
|
|
|
`echo "{}" > .prettierrc && touch .prettierignore`
|
2024-06-21 06:02:31 +02:00
|
|
|
1. `corepack pnpm exec prettier . --write` or
|
|
|
|
`corepack pnpm exec prettier . --check`
|
2024-06-06 15:50:35 +02:00
|
|
|
|
2024-06-07 05:43:15 +02:00
|
|
|
## Configuration
|
|
|
|
|
2024-07-07 09:08:39 +02:00
|
|
|
I do with `.editorconfig` what I can, but for example my template
|
|
|
|
`.prettierrc.json` looks like this. I use pre-commit's pretty-format-json, while
|
|
|
|
the one included on this page gets managed by prettier.
|
2024-06-07 05:43:15 +02:00
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
2024-07-07 09:08:39 +02:00
|
|
|
"bracketSameLine": true,
|
|
|
|
"endOfLine": "auto",
|
2024-06-19 07:29:42 +02:00
|
|
|
"insertPragma": true,
|
2024-06-17 10:08:11 +02:00
|
|
|
"overrides": [
|
2024-07-07 09:08:39 +02:00
|
|
|
{
|
|
|
|
"files": ".prettierrc",
|
|
|
|
"options": {
|
|
|
|
"parser": "json"
|
|
|
|
}
|
|
|
|
},
|
2024-06-17 10:08:11 +02:00
|
|
|
{
|
|
|
|
"files": "conf/librewolf.overrides.cfg",
|
2024-07-07 09:08:39 +02:00
|
|
|
"options": {
|
|
|
|
"parser": "babel"
|
|
|
|
}
|
2024-06-19 07:29:42 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"files": "conf/autoconfig.js.online",
|
2024-07-07 09:08:39 +02:00
|
|
|
"options": {
|
|
|
|
"parser": "babel"
|
|
|
|
}
|
2024-06-17 10:08:11 +02:00
|
|
|
}
|
2024-07-07 09:08:39 +02:00
|
|
|
],
|
|
|
|
"plugins": [
|
|
|
|
"@prettier/plugin-ruby",
|
|
|
|
"@prettier/plugin-xml",
|
|
|
|
"prettier-plugin-nginx",
|
|
|
|
"prettier-plugin-sh",
|
|
|
|
"prettier-plugin-toml"
|
|
|
|
],
|
|
|
|
"proseWrap": "always",
|
|
|
|
"quoteProps": "consistent",
|
|
|
|
"requirePragma": false,
|
|
|
|
"singleAttributePerLine": true
|
2024-06-07 05:43:15 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2024-07-07 09:08:39 +02:00
|
|
|
See the documentation linked on the bottom, but here are some explanations
|
|
|
|
anyway:
|
|
|
|
|
|
|
|
- `bracketSameLine: true` - HTML tags closing on the same line as something else
|
|
|
|
just looks so much better for my eyes.
|
|
|
|
- `endOfLine: auto` - The default is `lf` line-endings, which I support in
|
|
|
|
general, but I think `prettier` is the wrong place to configure it. I have it
|
|
|
|
in `.gitattributes` which will take priority on check-in and `.editorconfig`.
|
|
|
|
- `insertPragma: true` - adds a comment on files formatted
|
|
|
|
- `overrides` and `plugins` - self-explanatory, but if not see the documentation
|
|
|
|
(bottom of the page). I know I don't actually have `.prettierrc` as it's
|
|
|
|
`.prettierrc.json`, but it doesn't seem to hurt and `babel` seems to be used
|
|
|
|
for JavaScript by default, which those two are regardless of the name.
|
|
|
|
- `proseWrap: always` - wraps long markdown lines as individual newlines don't
|
|
|
|
matter (two begin a new paragraph).
|
|
|
|
- `quoteProps: consistent` - quotes things if anything is quoted.
|
|
|
|
- `requirePragma: false` - the other side of `insertPragma: true`, it's here as
|
|
|
|
a note to self.
|
|
|
|
- `singleAttributePerLine: true` - I just think it looks better.
|
2024-06-07 05:43:15 +02:00
|
|
|
|
2024-06-06 15:50:35 +02:00
|
|
|
## `.pre-commit-config.yaml`
|
|
|
|
|
|
|
|
This is the file that controls [`pre-commit`]s behaviour.
|
|
|
|
|
|
|
|
### Offline
|
|
|
|
|
2024-06-19 07:19:44 +02:00
|
|
|
I accidentally wrote this while updating this page to reflect me using prettier
|
|
|
|
outside of [`pre-commit`] too nowadays. This has the advantage that the same
|
|
|
|
local environment gets reused and dependencies are managed centrally, but
|
|
|
|
assumes everyone uses pnpm, won't work in [`pre-commit` ci] and may have other issues
|
|
|
|
I am not thinking of as a not-coder myself.
|
2024-06-06 15:50:35 +02:00
|
|
|
|
|
|
|
[`pre-commit`]: https://pre-commit.com
|
|
|
|
[`pre-commit` ci]: https://pre-commit.ci
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
ci:
|
2024-06-20 20:21:53 +02:00
|
|
|
skip: [pnpm-install-dev, prettier]
|
|
|
|
|
2024-06-06 15:50:35 +02:00
|
|
|
repos:
|
|
|
|
- repo: local
|
|
|
|
hooks:
|
2024-06-20 20:21:53 +02:00
|
|
|
- id: pnpm-install-dev
|
|
|
|
name: Install pnpm dev dependencies
|
2024-06-21 06:02:31 +02:00
|
|
|
entry: corepack pnpm install -D
|
2024-06-20 20:21:53 +02:00
|
|
|
language: system
|
|
|
|
always_run: true
|
2024-06-21 06:02:31 +02:00
|
|
|
#verbose: true
|
2024-06-20 20:21:53 +02:00
|
|
|
pass_filenames: false
|
2024-06-19 16:46:00 +02:00
|
|
|
- id: prettier
|
2024-06-06 15:50:35 +02:00
|
|
|
name: prettier
|
2024-06-21 06:02:31 +02:00
|
|
|
entry: corepack pnpm exec prettier --cache --ignore-unknown --write
|
2024-06-06 15:50:35 +02:00
|
|
|
language: system
|
|
|
|
# Better handled by pretty-format-json from pre-commit-hooks.
|
|
|
|
# Remember to have *.json in .prettierignore!
|
2024-07-07 09:08:39 +02:00
|
|
|
#exclude_types: [json]
|
2024-06-06 15:50:35 +02:00
|
|
|
```
|
|
|
|
|
2024-07-07 09:08:39 +02:00
|
|
|
<!--
|
|
|
|
|
2024-06-06 15:50:35 +02:00
|
|
|
### Online
|
2024-01-09 07:15:10 +01:00
|
|
|
|
|
|
|
```yaml
|
|
|
|
repos:
|
2024-06-06 15:50:35 +02:00
|
|
|
#- repo: https://github.com/pre-commit/mirrors-prettier
|
|
|
|
- repo: https://github.com/rbubley/mirrors-prettier
|
|
|
|
rev: "v3.3.1"
|
2024-01-09 07:15:10 +01:00
|
|
|
hooks:
|
|
|
|
- id: prettier
|
2024-06-06 15:50:35 +02:00
|
|
|
# Better handled by pretty-format-json from pre-commit-hooks.
|
|
|
|
# Remember to have *.json in .prettierignore!
|
|
|
|
exclude_types: [json]
|
2024-01-09 07:15:10 +01:00
|
|
|
additional_dependencies: [
|
|
|
|
# https://aminda.eu/n/prettier
|
2024-06-06 15:50:35 +02:00
|
|
|
"prettier@3.3.1",
|
2024-01-09 07:15:10 +01:00
|
|
|
"prettier-plugin-nginx@1.0.3",
|
|
|
|
"@prettier/plugin-ruby@4.0.4",
|
|
|
|
"prettier-plugin-toml@2.0.1",
|
2024-06-06 15:50:35 +02:00
|
|
|
"@prettier/plugin-xml@3.4.1",
|
2024-01-09 07:15:10 +01:00
|
|
|
]
|
|
|
|
```
|
2024-06-06 15:50:35 +02:00
|
|
|
|
2024-07-07 09:08:39 +02:00
|
|
|
-->
|
|
|
|
|
2024-06-06 15:50:35 +02:00
|
|
|
## Further information
|
|
|
|
|
|
|
|
- [prettier docs install](https://prettier.io/docs/en/install)
|
2024-07-03 18:12:29 +02:00
|
|
|
- [prettier parser options](https://github.com/prettier/prettier/blob/main/docs/options.md#parser)
|
2024-06-21 06:02:31 +02:00
|
|
|
- [corepack docs](https://nodejs.org/api/corepack.html)
|
|
|
|
- [corepack readme](https://github.com/nodejs/corepack/blob/main/README.md)
|