From d5c95ad47255491ca336b2255b9cdb985b870652 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sat, 16 Dec 2023 11:09:41 +0000 Subject: [PATCH] fix(nuxt): revert async transform of `setup` within `defineComponent` (#24784) --- .../nuxt/src/app/composables/asyncContext.ts | 10 ++++----- .../nuxt/src/app/composables/component.ts | 22 +++---------------- packages/nuxt/src/imports/presets.ts | 3 ++- packages/nuxt/test/auto-imports.test.ts | 2 -- packages/schema/src/config/build.ts | 1 - test/basic.test.ts | 8 ++----- .../basic/pages/async-transform-component.vue | 14 ------------ 7 files changed, 11 insertions(+), 49 deletions(-) delete mode 100644 test/fixtures/basic/pages/async-transform-component.vue diff --git a/packages/nuxt/src/app/composables/asyncContext.ts b/packages/nuxt/src/app/composables/asyncContext.ts index 56fb88232c..5fa88df529 100644 --- a/packages/nuxt/src/app/composables/asyncContext.ts +++ b/packages/nuxt/src/app/composables/asyncContext.ts @@ -1,11 +1,9 @@ // @ts-expect-error withAsyncContext is internal API import { getCurrentInstance, withAsyncContext as withVueAsyncContext } from 'vue' -export function withNuxtContext (fn: () => T) { - const nuxtApp = getCurrentInstance()?.appContext.app.$nuxt - return nuxtApp ? nuxtApp.runWithContext(fn) : fn() -} - export function withAsyncContext (fn: () => PromiseLike) { - return withVueAsyncContext(() => withNuxtContext(fn)) + return withVueAsyncContext(() => { + const nuxtApp = getCurrentInstance()?.appContext.app.$nuxt + return nuxtApp ? nuxtApp.runWithContext(fn) : fn() + }) } diff --git a/packages/nuxt/src/app/composables/component.ts b/packages/nuxt/src/app/composables/component.ts index 8d28f5608e..31e43f8c6e 100644 --- a/packages/nuxt/src/app/composables/component.ts +++ b/packages/nuxt/src/app/composables/component.ts @@ -1,9 +1,8 @@ -import { defineComponent as _defineComponent, getCurrentInstance, reactive, toRefs } from 'vue' -import type { ComponentOptions, DefineComponent } from 'vue' +import { getCurrentInstance, reactive, toRefs } from 'vue' +import type { DefineComponent, defineComponent } from 'vue' import { useHead } from '@unhead/vue' import type { NuxtApp } from '../nuxt' import { useNuxtApp } from '../nuxt' -import { withNuxtContext } from './asyncContext' import { useAsyncData } from './asyncData' import { useRoute } from './router' import { createError } from './error' @@ -29,7 +28,7 @@ async function runLegacyAsyncData (res: Record | Promise, arg2?: Partial) => { - if (typeof arg1 === 'function') { - return _defineComponent((...args) => withNuxtContext(() => arg1(...args)), arg2) - } - - if (arg1.setup) { - return _defineComponent({ - ...arg1, - setup: (...args: any[]) => withNuxtContext(() => arg1.setup(...args)) - }) as any - } - - return _defineComponent(arg1) -} diff --git a/packages/nuxt/src/imports/presets.ts b/packages/nuxt/src/imports/presets.ts index dde8d847cd..2ea4e66a78 100644 --- a/packages/nuxt/src/imports/presets.ts +++ b/packages/nuxt/src/imports/presets.ts @@ -30,7 +30,7 @@ const granularAppPresets: InlinePreset[] = [ from: '#app/config' }, { - imports: ['defineComponent', 'defineNuxtComponent'], + imports: ['defineNuxtComponent'], from: '#app/composables/component' }, { @@ -156,6 +156,7 @@ const vuePreset = defineUnimportPreset({ 'onScopeDispose', // Component + 'defineComponent', 'defineAsyncComponent', 'resolveComponent', 'getCurrentInstance', diff --git a/packages/nuxt/test/auto-imports.test.ts b/packages/nuxt/test/auto-imports.test.ts index 024fefd9db..9f8477aafa 100644 --- a/packages/nuxt/test/auto-imports.test.ts +++ b/packages/nuxt/test/auto-imports.test.ts @@ -81,8 +81,6 @@ describe('imports:nuxt', () => { }) const excludedVueHelpers = [ - // Nuxt stub for this helper - 'defineComponent', // Already globally registered 'defineEmits', 'defineExpose', diff --git a/packages/schema/src/config/build.ts b/packages/schema/src/config/build.ts index ba3175f142..9025441b1c 100644 --- a/packages/schema/src/config/build.ts +++ b/packages/schema/src/config/build.ts @@ -188,7 +188,6 @@ export default defineUntypedSchema({ asyncTransforms: { asyncFunctions: ['defineNuxtPlugin', 'defineNuxtRouteMiddleware'], objectDefinitions: { - defineComponent: ['setup'], defineNuxtComponent: ['asyncData', 'setup'], defineNuxtPlugin: ['setup'], definePageMeta: ['middleware', 'validate'] diff --git a/test/basic.test.ts b/test/basic.test.ts index 88b67b1289..1b2172c7e3 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -1951,14 +1951,10 @@ describe.skipIf(isDev() || isWindows || !isRenderingJson)('payload rendering', ( }) }) -describe('Async context', () => { - it.skipIf(process.env.TEST_CONTEXT !== 'async')('should be available', async () => { +describe.skipIf(process.env.TEST_CONTEXT !== 'async')('Async context', () => { + it('should be available', async () => { expect(await $fetch('/async-context')).toContain('"hasApp": true') }) - it('should transform `setup` within defineComponent', async () => { - const html = await $fetch('/async-transform-component') - expect(html).toContain('using automatic `setup` async transform') - }) }) describe.skipIf(isWindows)('useAsyncData', () => { diff --git a/test/fixtures/basic/pages/async-transform-component.vue b/test/fixtures/basic/pages/async-transform-component.vue deleted file mode 100644 index 03b7e2a3e0..0000000000 --- a/test/fixtures/basic/pages/async-transform-component.vue +++ /dev/null @@ -1,14 +0,0 @@ - - -