Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorder blocks in vue SFCs #26874

Merged
merged 3 commits into from
Sep 2, 2023
Merged

Reorder blocks in vue SFCs #26874

merged 3 commits into from
Sep 2, 2023

Conversation

silverwind
Copy link
Member

@silverwind silverwind commented Sep 2, 2023

The recommended order for SFC blocks is script -> template -> style, which we were violating because template and script were swapped. I do find script first also easier to read because the imports are on top, letting me immideatly see a component's dependencies.

This is a pure cut-paste refactor with some removal of some empty lines.

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Sep 2, 2023
@pull-request-size pull-request-size bot added the size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. label Sep 2, 2023
@silverwind silverwind added type/refactoring Existing code has been cleaned up. There should be no new functionality. skip-changelog This PR is irrelevant for the (next) changelog, for example bug fixes for unreleased features. labels Sep 2, 2023
Copy link
Member

@delvh delvh left a comment

Choose a reason for hiding this comment

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

I think we should certainly start adding a .git-blame-ignore-revs and assigning this PR to it (once it is merged).

@@ -106,85 +183,6 @@
</div>
</div>
</template>

<script>
Copy link
Member

@delvh delvh Sep 2, 2023

Choose a reason for hiding this comment

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

Now I'm confused.
How did the diff algorithm suddenly manage to move the <script> instead of the <template> block?

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe this move detection is based on number of lines and it moves the one with less lines?

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Sep 2, 2023
@wxiaoguang
Copy link
Contributor

wxiaoguang commented Sep 2, 2023

I do not see there is a rule for this.

  1. According to the questionnaire https://www.reddit.com/r/vuejs/comments/rmeoy7/what_order_do_you_have_your_template_and_script/
    • Major are Template then script
  2. According to https://vuejs.org/guide/scaling-up/sfc.html
    • The <template>, <script>, and <style> blocks encapsulate and colocate the view, logic and styling of a component in the same file.
    • <template> also comes first.
  3. According to https://vuejs.org/api/sfc-spec.html
    • First <template> then <script>
  4. According to https://vuejs.org/style-guide/rules-recommended.html#single-file-component-top-level-element-order
    • <template> then <script> is "good", we are not violating

Copy link
Contributor

@wxiaoguang wxiaoguang left a comment

Choose a reason for hiding this comment

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

I think the <template><script> order is good enough and it is the vast majority

image

@GiteaBot GiteaBot added lgtm/blocked A maintainer has reservations with the PR and thus it cannot be merged and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Sep 2, 2023
@delvh
Copy link
Member

delvh commented Sep 2, 2023

I'm fine with both

@silverwind
Copy link
Member Author

silverwind commented Sep 2, 2023

As I said, I find this order more logical because

  • Imports are on top, like in every other programming language
  • Return value (the template and CSS) is on bottom, similar to React

It does seem the Vue authors agree with me at least, the docs and https://play.vuejs.org both show this order.

@wxiaoguang wxiaoguang dismissed their stale review September 2, 2023 11:07

dismiss (see comment)

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/blocked A maintainer has reservations with the PR and thus it cannot be merged labels Sep 2, 2023
@wxiaoguang
Copy link
Contributor

wxiaoguang commented Sep 2, 2023

As I said, I find this order more logical because
* Imports are on top, like in every other programming language
* Return value (the template and CSS) is on bottom, similar to React

I do not have such feeling because Vue SFC is not the same as React/JSX. React uses render to return the DOM, they are embedded into the Component class. But Vue SFC declares the template separately, they look more like HTML code, for HTML code, the intuition is we first have the HTML definition and bind JS code to it.

It does seem the Vue authors agree with me at least, the docs and https://play.vuejs.org both show this order.

See the comment above, they also agree with tempate - script - style

Screenshots of Vue official documents with "template - script - style"

image

image


Update: why the "play.vuejs.org" shows script first, because it is <script setup>, a totally new mechanism in Vue 3.

But we are still using <script> (no setup).


Personally I prefer to follow the majority (template - script(nosetup) - style) at the moment, until there are other best practices, while I won't block it. Or use modern Vue 3 (script(setup) - template - style)

Copy link
Contributor

@wxiaoguang wxiaoguang left a comment

Choose a reason for hiding this comment

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

Maybe it's a good start for refactoring legacy <script> to modern <script setup>

@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Sep 2, 2023
@lafriks lafriks enabled auto-merge (squash) September 2, 2023 14:51
@lafriks lafriks merged commit 9a3de43 into go-gitea:main Sep 2, 2023
24 checks passed
@GiteaBot GiteaBot added this to the 1.22.0 milestone Sep 2, 2023
@silverwind
Copy link
Member Author

silverwind commented Sep 3, 2023

Yes, Composition API is the modern way of writing vue components and to me it very much looks like a react hooks component. Composition API seems to prefer the script > template > style order as well. It seems similar to the Hooks vs. Class components in React, no one writes class components any more today in React.

@silverwind silverwind deleted the sfc-order branch September 3, 2023 14:48
zjjhot added a commit to zjjhot/gitea that referenced this pull request Sep 4, 2023
* giteaofficial/main:
  [skip ci] Updated licenses and gitignores
  Update documents to fix some links (go-gitea#26885)
  clarify aspects of the dump command (go-gitea#26887)
  Relocate the `RSS user feed` button (go-gitea#26882)
  Use Go 1.21 and update dependencies (go-gitea#26878)
  Update docs about attachment path (go-gitea#26883)
  Refactor "shortsha" (go-gitea#26877)
  Fix wrong review requested number (go-gitea#26784)
  Refactor `og:description` to limit the max length (go-gitea#26876)
  Reorder blocks in vue SFCs (go-gitea#26874)
  Make it posible to customize nav text color via css var (go-gitea#26807)
  Enable djlint H008 and fix issues (go-gitea#26869)
  Improve opengraph previews (go-gitea#26851)
  Add more descriptive error on forgot password page (go-gitea#26848)
  Allow users with write permissions for issues to add attachments with API (go-gitea#26837)
  Move licenses.txt to /assets directory (go-gitea#26866)

# Conflicts:
#	templates/base/footer_content.tmpl
@techknowlogick techknowlogick removed this from the 1.22.0 milestone Sep 5, 2023
@techknowlogick techknowlogick added this to the 1.21.0 milestone Sep 5, 2023
@go-gitea go-gitea locked as resolved and limited conversation to collaborators Dec 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. skip-changelog This PR is irrelevant for the (next) changelog, for example bug fixes for unreleased features. type/refactoring Existing code has been cleaned up. There should be no new functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants