Skip to content

[vue] Introduce proper support for Vue Single File Components (SFC)#3563

Merged
vjeux merged 1 commit intoprettier:masterfrom
vjeux:vue_sfc
Dec 25, 2017
Merged

[vue] Introduce proper support for Vue Single File Components (SFC)#3563
vjeux merged 1 commit intoprettier:masterfrom
vjeux:vue_sfc

Conversation

@vjeux
Copy link
Copy Markdown
Contributor

@vjeux vjeux commented Dec 24, 2017

There's a lot of demand for vue sfc (#2097). This introduces partial support for them: all the html is printed as is, except for the script and style tags which are printed using prettier. I believe that this should cover a lot of the use cases while being simple to support and if we want we can extend to more in the future.

I copy pasted the html parser used by vue (it's just a single 400 lines file) so that we don't run the chancesof conflicts. I'm also very conservative: I only print the style and script at the top level and for the lang attributes we support.

I expect this to be landable as is and provide value, review welcome :)

@j-f1
Copy link
Copy Markdown
Member

j-f1 commented Dec 24, 2017

Don’t forget the README and the website :)

@Haroenv
Copy link
Copy Markdown
Contributor

Haroenv commented Dec 24, 2017

Cc @octref who did a lot of work on Vetur, the vs code plugin which already uses prettier for css and js: https://github.com/vuejs/vetur

@vjeux vjeux requested a review from azz December 24, 2017 17:26
</template>
<template >
<h1 >{{greeting}} world</h1 >
<script>kikoo ( ) </script>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not format non-toplevel scripts? What's the concern?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t want to add a lot of heuristics to figure out the correct indentation level

There's a lot of demand for vue sfc (prettier#2097). This introduces partial support for them: all the html is printed as is, except for the script and style tags which are printed using prettier. I believe that this should cover a lot of the use cases while being simple to support and if we want we can extend to more in the future.

I copy pasted the html parser used by vue (it's just a single 400 lines file) so that we don't run the chancesof conflicts. I'm also very conservative: I only print the style and script at the top level and for the lang attributes we support.

I expect this to be landable as is and provide value, review welcome :)
@vjeux vjeux merged commit c40b061 into prettier:master Dec 25, 2017
@vjeux
Copy link
Copy Markdown
Contributor Author

vjeux commented Dec 25, 2017

Oops, looks like I broke the build! Sorry about that. It’ll be a day before I can fix it. Feel free to revert in the meantime.

:class="{ 'issue-realtime-pre-pulse': preAnimation,
'issue-realtime-trigger-pulse': pulseAnimation}"
v-html="titleHtml"
>
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't say this is the preferred way to write Vue interpolations.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We started working on a full html pretty printer and added this vue file as an example. But it’s far from being usable so instead this PR just prints the html part as is and only reformats the js and css pieces.

@octref
Copy link
Copy Markdown

octref commented Dec 25, 2017

@vjeux Thanks a lot for your hard work. It would be great to see prettier officially supporting Vue!

Meanwhile, I think cases such as

<template lang="jade">
div
 h1 Hello
</template>

I think you might want to output some error message for Vue that has non-supported sections. Same for stylus, sass, coffee.

@vjeux
Copy link
Copy Markdown
Contributor Author

vjeux commented Dec 25, 2017

What I went for is to just reprint as is things that prettier doesn’t support. So you don’t have the nice formatting but at least it doesn’t throw on you. This way you can just run prettier all the time and it will work when it can and do nothing when it can’t. The downside is that it makes troubleshooting harder but should help with adoption.

Thanks for looking at it!

@duailibe duailibe added this to the 1.10 milestone Dec 30, 2017
@chinchang
Copy link
Copy Markdown
Contributor

chinchang commented Jan 3, 2018

@vjeux Any date for the 1.10 milestone release? Excited for this :)

Update: Got the date here -> #3603 (comment). Its expected for 10th January!

@lock lock bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Jan 18, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Jan 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants