Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.

feat(nuxt3): remove <script setup> transform and prefer top level await - #579

Merged
pi0 merged 14 commits into
mainfrom
feat/script-setup-transform
Sep 30, 2021
Merged

pi0 merged 14 commits into
mainfrom
feat/script-setup-transform

Conversation

@antfu

@antfu antfu commented Sep 25, 2021 •

Copy link
Copy Markdown
Member

Resolves nuxt/bridge#101

  • Refactored the transformation from vite and webpack to nuxt3 using unplugin.
  • When asyncData is presented in the SFC, the nuxt modifier could be omitted.
  • Always enable the transformation for <script setup> (see comments below)
  • Reduce the overhead of using defineNuxtComponent by avoiding object allocation for the normal components.
  • Emits error when asyncData is called outside of defineNuxtComponent
  • Updated docs a bit, but not yet sure if it's expressed well enough.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Sep 25, 2021 •

Copy link
Copy Markdown

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 15a04ca
Status:⚡️  Build in progress...

View logs

@antfu

antfu commented Sep 25, 2021

Copy link
Copy Markdown
Member Author

Here I ran into a problem on Vite, with the new vue plugin, the SFC in dev now transforms into a single file instead of descriptors with queries. As our transformation needs to be after the vue's compilation, not the explicitly set <script setup nuxt> modifier get lost on Vite dev.

To solve this:

  • We need to have two plugins, one for pre to scan the code before Vue's complication and record the id-flags map
  • Or we just always apply the transformation

I have checked the defineNuxtComponent implementation and I guess the overhead is pretty minimal given for most of the cases, if the component does not use asyncData we already do the early return:

const vm = getCurrentNuxtComponentInstance()
let promises = vm[NuxtComponentPendingPromises] = vm[NuxtComponentPendingPromises] || []
const res = setup?.(props, ctx) || {}
if (options.asyncData) {
promises.push(runLegacyAsyncData(res, options.asyncData))
}
if (!promises.length && !(res instanceof Promise)) {
return res
}

@antfu

antfu commented Sep 25, 2021

Copy link
Copy Markdown
Member Author

After a second thought, I guess always apply the transformation makes more sense here.

  • The overhead of defineNuxtComponent is fairly small (also mode some tiny perf improvement here)
  • Less action needed for users to use and no extra syntax is introduced

In the last two commits, I have make the transformation always enabled.

@antfu antfu changed the title feat: support implicit <script setup> for asyncData feat: always transform <script setup> Sep 25, 2021
@pi0

pi0 commented Sep 27, 2021 •

Copy link
Copy Markdown
Member

Thanks for PR @antfu. Some notes:

  • defineNuxtComponent is designed to offer more than just await syntax shortcut (options API compact) and while we are doing this change, I think maybe we can refactor the promise queue handling logic out of this utility
  • While it is in theory a low impact transform, we are still making each nuxt component essentially at least one tick slower than any other vue 3 application and doing transform for all components. I think we should keep using syntax detection until see how much this await shortcut idea is usable to move forward (or if we find alternatives to make awaitless syntax)
  • useAsyncData can be used with await and without wrapper

export function useAsyncData (defaults?: AsyncDataOptions) {
const nuxt = useNuxtApp()
const vm = getCurrentInstance()
const vm = getCurrentNuxtComponentInstance('asyncData')

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.

useAsyncData CAN be used without defineNuxtComponent as well! Only promise shortcut (usage without await) needs wrapping :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

so asyncData has to be in nuxt component, while useAsyncData doesn't right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

AH, I see. But I guess then we will need to figure out a way to warn users when used with non-await usage without defineNuxtComponent - especially when we are doing API detection and it could be used in the nested composable, in that this will fail silently and leading to a confusing result which could be hard for users to realize that <script setup nuxt> is required. s

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.

We can check nuxt._pendingPromises at some point before sending a response in development to warn users about pending promises.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

So I guess we will still need to do some transformation to check that for all components but in dev only?

@atinux

atinux commented Sep 27, 2021

Copy link
Copy Markdown
Member

Shall we remove the async-data and only use async-data-setup since I believe we shall keep simplicity and minimalism in our examples.

Our website can document both version :)

@antfu

antfu commented Sep 30, 2021 •

Copy link
Copy Markdown
Member Author

Another thought:

Giving the fact that

  • Nuxt 3 is Vue 3 exclusively
  • Vue 3's <script setup> supports top-level await natively
  • Without <script setup>, defineNuxtComponent can be directly used.

We might not actually need this transform in the first place?

@pi0

pi0 commented Sep 30, 2021

Copy link
Copy Markdown
Member

Vue 3's <script setup> supports top-level await natively

That's a good point! We can instead try to mimic the top-level await with instance preservation for vue 2 (bridge) users :)

@pi0 pi0 changed the title feat: always transform <script setup> feat: remove <script setup> transform and prefer top level await Sep 30, 2021
@pi0

pi0 commented Sep 30, 2021

Copy link
Copy Markdown
Member

Summary of last commits:

  • Docs only advising to use <script setup>
  • <script setup nuxt> is removed in favor of top level await support
  • await-less syntax sugar support removed from asyncData
  • defineNuxtComponent is undocumented and reserved for options API support only

@pi0 pi0 changed the title feat: remove <script setup> transform and prefer top level await feat(nuxt3): remove <script setup> transform and prefer top level await Sep 30, 2021
@pi0
pi0 merged commit e13baf9 into main Sep 30, 2021
@pi0
pi0 deleted the feat/script-setup-transform branch September 30, 2021 18:19
@pi0 pi0 mentioned this pull request Apr 13, 2022
11 of 14 tasks
@danielroe danielroe added the 3.x label Jan 19, 2023

This branch was successfully deployed

1 active deployment
Preview — 15a04cae Deployed Sep 30, 2021 by vercel[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid using <script setup nuxt>

4 participants