Prettier for ERB

I love using Prettier as a formatter for my code. However, I write a lot of ERB to do Ruby on Rails and out of the box, prettier doesn't play nice with ERB. There is a prettier-erb package but it is old and does not seem to work with new versions of prettier. However, I found an easy workaround.

In my .prettierrc.json, I add the following configuration:

{
  "overrides": [
    {
      "files": "*.erb",
      "options": {
        "parser": "html"
      }
    },
    {
      "files": "erb",
      "options": {
        "parser": "html"
      }
    },
    {
      "files": "*.html.erb",
      "options": {
        "parser": "html"
      }
    },
  ]
}

Prettier works really well with HTML and just telling it to treat ERB as HTML by overriding the formatter works super well. This combined with the prettier-vscode extension and you can now format ERB with prettier pretty effectively.

If anyone knows a better way, I'd love to hear it!