Prettier - An Opinionated Code Formatter

Automatic code formatting from editor and command line.

Sometimes Prettier has a different opinions about code should be formatted than we do, But a fast,consistent and reliable formatting is worth it.

We use Prettier with the prettier-plugin-go-template which handles the go templates that will otherwise be corrupted.

Install

Prettier is a npm application so just use npm to install to your current workspace folder.

Easiest is to install Prettier and the used plugins in one shot to your current workplace1 using npm.

npm install --save-dev prettier prettier-plugin-go-template prettier-plugin-toml

Usage

Mostly used to autoformat source files in VSCode on save.

Configuration

To enable the plugin you have to add it to the .prettierrc. And ofc you may configure the formatting to some degree.

Here’s my .prettierrc for reference:

{
   "plugins": ["prettier-plugin-go-template", "prettier-plugin-toml"],
   "endOfLine": "lf",
   "printWidth": 100,
   "tabWidth": 3,
   "overrides": [
      {
         "files": [".code-workspace", ".prettierrc"],
         "options": {
            "parser": "json",
            "semi": true
         }
      },
      {
         "files": ["*.html", "*.gotmpl", "layouts/partials/rest/*"],
         "options": {
            "parser": "go-template"
         }
      },
      {
         "files": "*.md",
         "options": {
            "parser": "markdown",
            "proseWrap": "always",
            "tabWidth": 3
         }
      },
      {
         "files": ["*.toml"],
         "options": {
            "alignEntries": true,
            "alignComments": true,
            "indentEntries": true,
            "indentTables": true,
            "tabWidth": 3
         }
      },
      {
         "files": ["*.yaml", "*.yml"],
         "options": {
            "tabWidth": 2
         }
      }
   ]
}

  1. could’t make it with a –global install ↩︎