chore: use internal apis

This commit is contained in:
harlan 2025-01-21 17:28:10 +11:00
parent 46499f7049
commit 88cc6ba9b8
8 changed files with 42 additions and 18 deletions

View File

@ -1,8 +1,8 @@
import type { defineAsyncComponent } from 'vue' import type { defineAsyncComponent } from 'vue'
import { createVNode, defineComponent, onErrorCaptured } from 'vue' import { createVNode, defineComponent, onErrorCaptured } from 'vue'
import { injectHead } from '@unhead/vue'
import { createError } from '../composables/error' import { createError } from '../composables/error'
import { injectHead } from '../composables/head'
// @ts-expect-error virtual file // @ts-expect-error virtual file
import { islandComponents } from '#build/components.islands.mjs' import { islandComponents } from '#build/components.islands.mjs'

View File

@ -1,8 +1,8 @@
import { getCurrentInstance, reactive, toRefs } from 'vue' import { getCurrentInstance, reactive, toRefs } from 'vue'
import type { DefineComponent, defineComponent } from 'vue' import type { DefineComponent, defineComponent } from 'vue'
import { useHead } from '@unhead/vue'
import type { NuxtApp } from '../nuxt' import type { NuxtApp } from '../nuxt'
import { useNuxtApp } from '../nuxt' import { useNuxtApp } from '../nuxt'
import { useHead } from './head'
import { useAsyncData } from './asyncData' import { useAsyncData } from './asyncData'
import { useRoute } from './router' import { useRoute } from './router'
import { createError } from './error' import { createError } from './error'

View File

@ -1,12 +1,12 @@
import type { H3Event } from 'h3' import type { H3Event } from 'h3'
import { setResponseStatus as _setResponseStatus, appendHeader, getRequestHeader, getRequestHeaders, getResponseHeader, removeResponseHeader, setResponseHeader } from 'h3' import { setResponseStatus as _setResponseStatus, appendHeader, getRequestHeader, getRequestHeaders, getResponseHeader, removeResponseHeader, setResponseHeader } from 'h3'
import { computed, getCurrentInstance, ref } from 'vue' import { computed, getCurrentInstance, ref } from 'vue'
import { useServerHead } from '@unhead/vue'
import type { H3Event$Fetch } from 'nitro/types' import type { H3Event$Fetch } from 'nitro/types'
import type { NuxtApp } from '../nuxt' import type { NuxtApp } from '../nuxt'
import { useNuxtApp } from '../nuxt' import { useNuxtApp } from '../nuxt'
import { toArray } from '../utils' import { toArray } from '../utils'
import { useServerHead } from './head'
/** @since 3.0.0 */ /** @since 3.0.0 */
export function useRequestEvent (nuxtApp: NuxtApp = useNuxtApp()) { export function useRequestEvent (nuxtApp: NuxtApp = useNuxtApp()) {

View File

@ -9,7 +9,7 @@ import type { EventHandlerRequest, H3Event } from 'h3'
import type { AppConfig, AppConfigInput, RuntimeConfig } from 'nuxt/schema' import type { AppConfig, AppConfigInput, RuntimeConfig } from 'nuxt/schema'
import type { RenderResponse } from 'nitro/types' import type { RenderResponse } from 'nitro/types'
import type { LogObject } from 'consola' 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' import type { NuxtAppLiterals } from 'nuxt/app'

View File

@ -1,6 +1,6 @@
import { ref } from 'vue' import { ref } from 'vue'
import { useHead } from '@unhead/vue'
import { defineNuxtPlugin } from '../nuxt' import { defineNuxtPlugin } from '../nuxt'
import { useHead } from '../composables/head'
const SUPPORTED_PROTOCOLS = ['http:', 'https:'] const SUPPORTED_PROTOCOLS = ['http:', 'https:']

View File

@ -1,4 +1,4 @@
import type { UseHeadInput } from '@unhead/vue' import type { UseHeadInput } from '@unhead/vue/types'
import type { NuxtApp, useNuxtApp } from '../nuxt.js' import type { NuxtApp, useNuxtApp } from '../nuxt.js'
declare global { declare global {

View File

@ -1,7 +1,6 @@
import { resolve } from 'pathe' import { dirname, resolve } from 'pathe'
import { addComponent, addImportsSources, addPlugin, addTemplate, defineNuxtModule, tryResolveModule } from '@nuxt/kit' import { addComponent, addPlugin, addTemplate, defineNuxtModule, tryResolveModule } from '@nuxt/kit'
import type { NuxtOptions } from '@nuxt/schema' import type { NuxtOptions } from '@nuxt/schema'
import { unheadVueComposablesImports } from '@unhead/vue'
import { distDir } from '../dirs' import { distDir } from '../dirs'
const components = ['NoScript', 'Link', 'Base', 'Title', 'Meta', 'Style', 'Head', 'Html', 'Body'] const components = ['NoScript', 'Link', 'Base', 'Title', 'Meta', 'Style', 'Head', 'Html', 'Body']
@ -14,10 +13,6 @@ export default defineNuxtModule<NuxtOptions['unhead']>({
async setup (options, nuxt) { async setup (options, nuxt) {
const runtimeDir = resolve(distDir, 'head/runtime') const runtimeDir = resolve(distDir, 'head/runtime')
const isNuxtV4 = nuxt.options.future?.compatibilityVersion === 4 const isNuxtV4 = nuxt.options.future?.compatibilityVersion === 4
// Transpile @unhead/vue
nuxt.options.build.transpile.push('@unhead/vue')
// Register components // Register components
const componentsPath = resolve(runtimeDir, 'components') const componentsPath = resolve(runtimeDir, 'components')
for (const componentName of components) { for (const componentName of components) {
@ -39,8 +34,9 @@ export default defineNuxtModule<NuxtOptions['unhead']>({
] ]
} }
const exportPath = resolve(runtimeDir, 'exports', isNuxtV4 ? 'v4' : 'v3') const realUnheadPath = await tryResolveModule('@unhead/vue', nuxt.options.modulesDir) || '@unhead/vue'
nuxt.options.alias['#unhead/exports'] = exportPath // 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 // for Nuxt v3 users we will alias `@unhead/vue` to our custom export path so that
// import { useHead } from '@unhead/vue' // import { useHead } from '@unhead/vue'
@ -48,10 +44,34 @@ export default defineNuxtModule<NuxtOptions['unhead']>({
// for Nuxt v4 user should import from #imports // for Nuxt v4 user should import from #imports
if (!isNuxtV4) { if (!isNuxtV4) {
for (const subpath of ['legacy', 'types']) { for (const subpath of ['legacy', 'types']) {
const subpathModule = `@unhead/vue/${subpath}` nuxt.options.alias[`@unhead/vue/${subpath}`] = resolve(dirname(realUnheadPath), subpath)
nuxt.options.alias[subpathModule] = await tryResolveModule(subpathModule, nuxt.options.modulesDir) || subpathModule
} }
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({ addTemplate({

View File

@ -120,6 +120,10 @@ describe('composables', () => {
'useSeoMeta', 'useSeoMeta',
'useServerSeoMeta', 'useServerSeoMeta',
'usePreviewMode', 'usePreviewMode',
'injectHead',
'useHeadSafe',
'useServerHead',
'useServerHeadSafe',
] ]
expect(Object.keys(composables).sort()).toEqual([...new Set([...testedComposables, ...skippedComposables])].sort()) expect(Object.keys(composables).sort()).toEqual([...new Set([...testedComposables, ...skippedComposables])].sort())
}) })