Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reformat a Vue.js component in vscode?

I'm using Visual Studio Code to code a Vue.js component and need to reformat the code of that component.

I did not find any built-in formatters, and the first choice for a plugin was vue-buetify which informs after installation that

There are many bugs in the extension, please do not use it, the better choice is vetur

I then tried Vetur by installing it but there is no place where I see an option to beautify the code currently in the editor. The Shift + Alt + F command has no effect.

How can I actually beautify (reformat) the code for a Vue component?

like image 345
WoJ Avatar asked May 16 '18 10:05

WoJ


2 Answers

I've been fiddling with formatting quite a bit since my previously working project stopped formatting one day. Here's what I think the current state of the art is:

Use extensions vetur and prettier (specifically, esbenp.prettier-vscode Prettier - Code formatter). (You get these preinstalled by Vue.js Extension Pack esbenp.prettier-vscodeand others.)

Vetur is the (current) mandatory default tooling for vue. Accept no substitutes.

Prettier doesn't support .vue files per se, so that filetype is disabled by default: https://github.com/prettier/prettier-vscode/issues/338.

But Vetur understand its limitations and instead delegates formatting of individual sections of the .vue file to a potentially different formatter. By default, though, it delegates everything other than HTML sections to Prettier. https://vuejs.github.io/vetur/formatting.html. It disables formatting for HTML sections.

Vetur developers are down on js-beautify-html, although it is still apparently functional: https://vuejs.github.io/vetur/formatting.html. And they don't make an alternative recommendation at this time.

Prettier support for HTML, which would be the obvious choice if only it existed, is a long, sad story. Currently (May 2018), prettier formats HTML as JSX. Many subtleties are mentioned, but one issue that I have grasped is that JSX converts begin/empty/end tags to empty tags, e.g to . Apparently React and (I believe) Vue, do not like this, hence vetur disables Prettier for HTML.

So I'm going forward with enabling js-beautify-html in vetur settings, hoping for the best and keeping my eyes peeled. But I'm such a superficial coder that I may never trip over its known issues.

like image 174
BobHy Avatar answered Nov 15 '22 09:11

BobHy


If you have installed the Vetur extension on VS Code,

  1. Go to the VS Code extension area.
  2. Find Vetur and select the gear icon to enter settings of Vetur.
  3. Scroll down until you find
Vetur › Format › Default Formatter: JS
Default formatter for <script> region
  1. Select pretter-eslint from dropdown menu
    (if you don't see that option you can install Prettier extension).
  2. Now you can see it formats your code automatically whenever you save.
like image 20
HeshanHH Avatar answered Nov 15 '22 10:11

HeshanHH