From 88cc6ba9b89350512a870dcc06c63bf793bbd97e Mon Sep 17 00:00:00 2001 From: harlan Date: Tue, 21 Jan 2025 17:28:10 +1100 Subject: [PATCH] chore: use internal apis --- .../src/app/components/island-renderer.ts | 2 +- .../nuxt/src/app/composables/component.ts | 2 +- packages/nuxt/src/app/composables/ssr.ts | 2 +- packages/nuxt/src/app/nuxt.ts | 2 +- .../plugins/cross-origin-prefetch.client.ts | 2 +- packages/nuxt/src/app/types/augments.d.ts | 2 +- packages/nuxt/src/head/module.ts | 44 ++++++++++++++----- test/nuxt/composables.test.ts | 4 ++ 8 files changed, 42 insertions(+), 18 deletions(-) diff --git a/packages/nuxt/src/app/components/island-renderer.ts b/packages/nuxt/src/app/components/island-renderer.ts index 189c980e50..32c6d0d9db 100644 --- a/packages/nuxt/src/app/components/island-renderer.ts +++ b/packages/nuxt/src/app/components/island-renderer.ts @@ -1,8 +1,8 @@ import type { defineAsyncComponent } from 'vue' import { createVNode, defineComponent, onErrorCaptured } from 'vue' -import { injectHead } from '@unhead/vue' import { createError } from '../composables/error' +import { injectHead } from '../composables/head' // @ts-expect-error virtual file import { islandComponents } from '#build/components.islands.mjs' diff --git a/packages/nuxt/src/app/composables/component.ts b/packages/nuxt/src/app/composables/component.ts index 914533e757..abc0522e23 100644 --- a/packages/nuxt/src/app/composables/component.ts +++ b/packages/nuxt/src/app/composables/component.ts @@ -1,8 +1,8 @@ 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 { useHead } from './head' import { useAsyncData } from './asyncData' import { useRoute } from './router' import { createError } from './error' diff --git a/packages/nuxt/src/app/composables/ssr.ts b/packages/nuxt/src/app/composables/ssr.ts index 41091a6f47..b70630b31c 100644 --- a/packages/nuxt/src/app/composables/ssr.ts +++ b/packages/nuxt/src/app/composables/ssr.ts @@ -1,12 +1,12 @@ import type { H3Event } from 'h3' import { setResponseStatus as _setResponseStatus, appendHeader, getRequestHeader, getRequestHeaders, getResponseHeader, removeResponseHeader, setResponseHeader } from 'h3' import { computed, getCurrentInstance, ref } from 'vue' -import { useServerHead } from '@unhead/vue' import type { H3Event$Fetch } from 'nitro/types' import type { NuxtApp } from '../nuxt' import { useNuxtApp } from '../nuxt' import { toArray } from '../utils' +import { useServerHead } from './head' /** @since 3.0.0 */ export function useRequestEvent (nuxtApp: NuxtApp = useNuxtApp()) { diff --git a/packages/nuxt/src/app/nuxt.ts b/packages/nuxt/src/app/nuxt.ts index f127c9c94f..4314d78b3a 100644 --- a/packages/nuxt/src/app/nuxt.ts +++ b/packages/nuxt/src/app/nuxt.ts @@ -9,7 +9,7 @@ import type { EventHandlerRequest, H3Event } from 'h3' import type { AppConfig, AppConfigInput, RuntimeConfig } from 'nuxt/schema' import type { RenderResponse } from 'nitro/types' import type { LogObject } from 'consola' -import type { MergeHead, VueHeadClient } from '@unhead/vue' +import type { MergeHead, VueHeadClient } from '@unhead/vue/types' import type { NuxtAppLiterals } from 'nuxt/app' diff --git a/packages/nuxt/src/app/plugins/cross-origin-prefetch.client.ts b/packages/nuxt/src/app/plugins/cross-origin-prefetch.client.ts index 66788be26b..f4a2c0e321 100644 --- a/packages/nuxt/src/app/plugins/cross-origin-prefetch.client.ts +++ b/packages/nuxt/src/app/plugins/cross-origin-prefetch.client.ts @@ -1,6 +1,6 @@ import { ref } from 'vue' -import { useHead } from '@unhead/vue' import { defineNuxtPlugin } from '../nuxt' +import { useHead } from '../composables/head' const SUPPORTED_PROTOCOLS = ['http:', 'https:'] diff --git a/packages/nuxt/src/app/types/augments.d.ts b/packages/nuxt/src/app/types/augments.d.ts index 89d3482309..b556680e15 100644 --- a/packages/nuxt/src/app/types/augments.d.ts +++ b/packages/nuxt/src/app/types/augments.d.ts @@ -1,4 +1,4 @@ -import type { UseHeadInput } from '@unhead/vue' +import type { UseHeadInput } from '@unhead/vue/types' import type { NuxtApp, useNuxtApp } from '../nuxt.js' declare global { diff --git a/packages/nuxt/src/head/module.ts b/packages/nuxt/src/head/module.ts index be0edf37be..4ae286e04e 100644 --- a/packages/nuxt/src/head/module.ts +++ b/packages/nuxt/src/head/module.ts @@ -1,7 +1,6 @@ -import { resolve } from 'pathe' -import { addComponent, addImportsSources, addPlugin, addTemplate, defineNuxtModule, tryResolveModule } from '@nuxt/kit' +import { dirname, resolve } from 'pathe' +import { addComponent, addPlugin, addTemplate, defineNuxtModule, tryResolveModule } from '@nuxt/kit' import type { NuxtOptions } from '@nuxt/schema' -import { unheadVueComposablesImports } from '@unhead/vue' import { distDir } from '../dirs' const components = ['NoScript', 'Link', 'Base', 'Title', 'Meta', 'Style', 'Head', 'Html', 'Body'] @@ -14,10 +13,6 @@ export default defineNuxtModule({ async setup (options, nuxt) { const runtimeDir = resolve(distDir, 'head/runtime') const isNuxtV4 = nuxt.options.future?.compatibilityVersion === 4 - - // Transpile @unhead/vue - nuxt.options.build.transpile.push('@unhead/vue') - // Register components const componentsPath = resolve(runtimeDir, 'components') for (const componentName of components) { @@ -39,8 +34,9 @@ export default defineNuxtModule({ ] } - const exportPath = resolve(runtimeDir, 'exports', isNuxtV4 ? 'v4' : 'v3') - nuxt.options.alias['#unhead/exports'] = exportPath + const realUnheadPath = await tryResolveModule('@unhead/vue', nuxt.options.modulesDir) || '@unhead/vue' + // Transpile @unhead/vue + nuxt.options.build.transpile.push(realUnheadPath) // for Nuxt v3 users we will alias `@unhead/vue` to our custom export path so that // import { useHead } from '@unhead/vue' @@ -48,10 +44,34 @@ export default defineNuxtModule({ // for Nuxt v4 user should import from #imports if (!isNuxtV4) { for (const subpath of ['legacy', 'types']) { - const subpathModule = `@unhead/vue/${subpath}` - nuxt.options.alias[subpathModule] = await tryResolveModule(subpathModule, nuxt.options.modulesDir) || subpathModule + nuxt.options.alias[`@unhead/vue/${subpath}`] = resolve(dirname(realUnheadPath), subpath) } - nuxt.options.alias['@unhead/vue'] = exportPath + addTemplate({ + filename: 'unhead-exports.mjs', + getContents () { + return ` +export { + injectHead, + useHead, + useHeadSafe, + useSeoMeta, + useServerHead, + useServerHeadSafe, + useServerSeoMeta, +} from '#app/composables/head' + +export { + createHeadCore, + resolveUnrefHeadInput, + unheadVueComposablesImports, +} from '${JSON.stringify(realUnheadPath)}' + +export * from '@unhead/vue/types' +` + }, + }) + + nuxt.options.alias['@unhead/vue'] = '#build/unhead-exports.mjs' } addTemplate({ diff --git a/test/nuxt/composables.test.ts b/test/nuxt/composables.test.ts index 86568528be..f105c805bb 100644 --- a/test/nuxt/composables.test.ts +++ b/test/nuxt/composables.test.ts @@ -120,6 +120,10 @@ describe('composables', () => { 'useSeoMeta', 'useServerSeoMeta', 'usePreviewMode', + 'injectHead', + 'useHeadSafe', + 'useServerHead', + 'useServerHeadSafe', ] expect(Object.keys(composables).sort()).toEqual([...new Set([...testedComposables, ...skippedComposables])].sort()) })