Skip to content

Commit 927f1b4

Browse files
committedJan 21, 2025·
fix(core,vue): missing subpath exports
1 parent 7b47439 commit 927f1b4

20 files changed

+186
-142
lines changed
 

‎packages/unhead/package.json

+5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@
4444
"types": "./dist/client.d.ts",
4545
"import": "./dist/client.mjs",
4646
"require": "./dist/client.cjs"
47+
},
48+
"./legacy": {
49+
"types": "./dist/legacy.d.ts",
50+
"import": "./dist/legacy.mjs",
51+
"require": "./dist/legacy.cjs"
4752
}
4853
},
4954
"main": "dist/index.cjs",

‎packages/vue/build.config.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ export default defineBuildConfig({
99
externals: ['vue'],
1010
entries: [
1111
{ input: 'src/index', name: 'index' },
12-
{ input: 'src/components/index', name: 'components' },
12+
{ input: 'src/components', name: 'components' },
1313
{ input: 'src/server', name: 'server' },
1414
{ input: 'src/client', name: 'client' },
1515
{ input: 'src/legacy/index', name: 'legacy' },
16+
{ input: 'src/types', name: 'types' },
1617
],
1718
})

‎packages/vue/legacy.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from './dist/server'
1+
export * from './dist/legacy'

‎packages/vue/package.json

+19-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@
4040
"types": "./dist/client.d.ts",
4141
"import": "./dist/client.mjs",
4242
"require": "./dist/client.cjs"
43+
},
44+
"./types": {
45+
"types": "./dist/types.d.ts",
46+
"import": "./dist/types.mjs",
47+
"require": "./dist/types.cjs"
48+
},
49+
"./legacy": {
50+
"types": "./dist/legacy.d.ts",
51+
"import": "./dist/legacy.mjs",
52+
"require": "./dist/legacy.cjs"
4353
}
4454
},
4555
"main": "dist/index.cjs",
@@ -55,13 +65,21 @@
5565
],
5666
"client": [
5767
"dist/client"
68+
],
69+
"types": [
70+
"dist/types"
71+
],
72+
"legacy": [
73+
"dist/legacy"
5874
]
5975
}
6076
},
6177
"files": [
6278
"client.d.ts",
6379
"dist",
64-
"server.d.ts"
80+
"server.d.ts",
81+
"types.d.ts",
82+
"legacy.d.ts"
6583
],
6684
"scripts": {
6785
"build": "unbuild .",

‎packages/vue/src/VueHeadMixin.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getCurrentInstance } from 'vue'
2-
import { useHead } from './composables/useHead'
2+
import { useHead } from './composables'
33

44
export const VueHeadMixin = {
55
created() {

‎packages/vue/src/autoImports.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import { composableNames } from '@unhead/shared'
22

3-
const coreComposableNames = [
4-
'injectHead',
5-
]
6-
73
export const unheadVueComposablesImports = {
8-
'@unhead/vue': [...coreComposableNames, ...composableNames],
4+
'@unhead/vue': ['injectHead', ...composableNames],
95
}

‎packages/vue/src/components/Head.ts ‎packages/vue/src/components.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { DefineComponent, Ref, VNode } from 'vue'
2-
import type { ReactiveHead } from '../types'
2+
import type { ReactiveHead } from './types'
33
import { defineComponent, onBeforeUnmount, ref, watchEffect } from 'vue'
4-
import { useHead } from '../composables/useHead'
4+
import { useHead } from './composables'
55

66
function addVNodeToHeadObj(node: VNode, obj: ReactiveHead) {
77
const nodeType = node.type

‎packages/vue/src/components/index.ts

-1
This file was deleted.

‎packages/vue/src/composables.ts

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import type { ActiveHeadEntry, HeadEntryOptions, MergeHead } from '@unhead/schema'
2+
import type {
3+
Ref,
4+
} from 'vue'
5+
import type {
6+
ReactiveHead,
7+
UseHeadInput,
8+
UseHeadOptions,
9+
UseHeadSafeInput,
10+
UseSeoMetaInput,
11+
VueHeadClient,
12+
} from './types'
13+
import { unpackMeta, whitelistSafeInput } from '@unhead/shared'
14+
import {
15+
getCurrentInstance,
16+
hasInjectionContext,
17+
inject,
18+
onActivated,
19+
onBeforeUnmount,
20+
onDeactivated,
21+
ref,
22+
watch,
23+
watchEffect,
24+
} from 'vue'
25+
import { headSymbol } from './install'
26+
import { resolveUnrefHeadInput } from './utils'
27+
28+
export function injectHead() {
29+
if (hasInjectionContext()) {
30+
// fallback to vue context
31+
const instance = inject<VueHeadClient<MergeHead>>(headSymbol)
32+
if (!instance) {
33+
throw new Error('useHead() was called without provide context, ensure you call it through the setup() function.')
34+
}
35+
return instance
36+
}
37+
throw new Error('useHead() was called without provide context, ensure you call it through the setup() function.')
38+
}
39+
40+
export function useHead<T extends MergeHead>(input: UseHeadInput<T>, options: UseHeadOptions = {}): ActiveHeadEntry<UseHeadInput<T>> {
41+
const head = options.head || injectHead()
42+
return head.ssr ? head.push(input, options as HeadEntryOptions) : clientUseHead(head, input, options as HeadEntryOptions)
43+
}
44+
45+
function clientUseHead<T extends MergeHead>(head: VueHeadClient<T>, input: UseHeadInput<T>, options: HeadEntryOptions = {}): ActiveHeadEntry<UseHeadInput<T>> {
46+
const deactivated = ref(false)
47+
48+
const resolvedInput: Ref<ReactiveHead> = ref({})
49+
watchEffect(() => {
50+
resolvedInput.value = deactivated.value
51+
? {}
52+
: resolveUnrefHeadInput(input)
53+
})
54+
const entry: ActiveHeadEntry<UseHeadInput<T>> = head.push(resolvedInput.value, options)
55+
watch(resolvedInput, (e) => {
56+
entry.patch(e)
57+
})
58+
59+
const vm = getCurrentInstance()
60+
if (vm) {
61+
onBeforeUnmount(() => {
62+
entry.dispose()
63+
})
64+
onDeactivated(() => {
65+
deactivated.value = true
66+
})
67+
onActivated(() => {
68+
deactivated.value = false
69+
})
70+
}
71+
return entry
72+
}
73+
74+
export function useHeadSafe(input: UseHeadSafeInput, options: UseHeadOptions = {}): ActiveHeadEntry<any> {
75+
// @ts-expect-error untyped
76+
return useHead(input, { ...options, transform: whitelistSafeInput })
77+
}
78+
79+
export function useSeoMeta(input: UseSeoMetaInput, options?: UseHeadOptions): ActiveHeadEntry<any> {
80+
const { title, titleTemplate, ...meta } = input
81+
return useHead({
82+
title,
83+
titleTemplate,
84+
// @ts-expect-error runtime type
85+
_flatMeta: meta,
86+
}, {
87+
...options,
88+
transform(t) {
89+
// @ts-expect-error runtime type
90+
const meta = unpackMeta({ ...t._flatMeta })
91+
// @ts-expect-error runtime type
92+
delete t._flatMeta
93+
return {
94+
// @ts-expect-error runtime type
95+
...t,
96+
meta,
97+
}
98+
},
99+
})
100+
}
101+
102+
export function useServerHead<T extends MergeHead>(input: UseHeadInput<T>, options: UseHeadOptions = {}): ActiveHeadEntry<any> {
103+
return useHead<T>(input, { ...options, mode: 'server' })
104+
}
105+
106+
export function useServerHeadSafe(input: UseHeadSafeInput, options: UseHeadOptions = {}): ActiveHeadEntry<any> {
107+
return useHeadSafe(input, { ...options, mode: 'server' })
108+
}
109+
110+
export function useServerSeoMeta(input: UseSeoMetaInput, options?: UseHeadOptions): ActiveHeadEntry<UseSeoMetaInput> {
111+
return useSeoMeta(input, { ...options, mode: 'server' })
112+
}

‎packages/vue/src/composables/injectHead.ts

-15
This file was deleted.

‎packages/vue/src/composables/useHead.ts

-41
This file was deleted.

‎packages/vue/src/composables/useHeadSafe.ts

-9
This file was deleted.

‎packages/vue/src/composables/useSeoMeta.ts

-27
This file was deleted.

‎packages/vue/src/composables/useServerHead.ts

-7
This file was deleted.

‎packages/vue/src/composables/useServerHeadSafe.ts

-7
This file was deleted.

‎packages/vue/src/composables/useServerSeoMeta.ts

-7
This file was deleted.

‎packages/vue/src/index.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,7 @@ export {
1919

2020
// composables
2121
export * from './autoImports'
22-
export * from './composables/injectHead'
23-
export * from './composables/useHead'
24-
25-
export * from './composables/useHeadSafe'
26-
export * from './composables/useSeoMeta'
27-
export * from './composables/useServerHead'
28-
export * from './composables/useServerHeadSafe'
29-
export * from './composables/useServerSeoMeta'
22+
export * from './composables'
3023
// types
3124
export * from './types'
3225
export * from './VueHeadMixin'

0 commit comments

Comments
 (0)
Please sign in to comment.