chore: code rabbit suggestions

This commit is contained in:
harlan 2025-01-22 17:11:38 +11:00
parent ad28cee299
commit e17f89b5f6
3 changed files with 12 additions and 7 deletions

View File

@ -11,7 +11,7 @@ export default defineNuxtModule<NuxtOptions['unhead']>({
name: 'nuxt:meta',
configKey: 'unhead',
},
setup (options, nuxt) {
async setup (options, nuxt) {
const runtimeDir = resolve(distDir, 'head/runtime')
// Transpile @unhead/vue
@ -45,18 +45,18 @@ export default defineNuxtModule<NuxtOptions['unhead']>({
rootDir: nuxt.options.rootDir,
}))
const unheadPlugins = await tryResolveModule('unhead/plugins', nuxt.options.modulesDir) || 'unhead/plugins'
addTemplate({
filename: 'unhead-options.mjs',
async getContents () {
getContents () {
if (isNuxtV4) {
return `export default {}`
}
const unheadPlugins = await tryResolveModule('unhead/plugins', nuxt.options.modulesDir) || 'unhead/plugins'
// v1 unhead legacy options
const disableCapoSorting = !nuxt.options.experimental.headNext
return `import { DeprecationsPlugin, PromisesPlugin } from ${JSON.stringify(unheadPlugins)};
export default {
disableCapoSorting: ${disableCapoSorting}
disableCapoSorting: ${Boolean(disableCapoSorting)},
plugins: [DeprecationsPlugin, PromisesPlugin],
}`
},

View File

@ -58,10 +58,10 @@ export const UnheadImportsPlugin = (options: UnheadImportsPluginOptions) => crea
if (!id.includes('node_modules')) {
logger.warn(`You are importing from \`${UnheadVue}\` in \`./${relative(options.rootDir, id)}\`. Please import from \`#app\` instead for full type safety.`)
}
s.prepend(`import { ${toImports(importsFromUnhead).join(', ')} } from '#app/composables/head'\n`)
s.prepend(`import { ${toImports(importsFromUnhead).join(', ')} } from "#app/composables/head"\n`)
}
if (importsFromHead.length) {
s.prepend(`import { ${toImports(importsFromHead).join(', ')} } from ${JSON.stringify(UnheadVue)}'\n`)
s.prepend(`import { ${toImports(importsFromHead).join(', ')} } from "${UnheadVue}"\n`)
}
if (s.hasChanged()) {

View File

@ -19,7 +19,12 @@ export function injectHead (nuxtApp?: NuxtApp): VueHeadClient<MergeHead> {
const nuxt = nuxtApp || useNuxtApp()
return nuxt.ssrContext?.head || nuxt.runWithContext(() => {
if (hasInjectionContext()) {
return inject<VueHeadClient<MergeHead>>(headSymbol)!
const head = inject<VueHeadClient<MergeHead>>(headSymbol)
// should not be possible
if (!head) {
throw new Error('[nuxt] [unhead] Missing Unhead instance.')
}
return head
}
}) as VueHeadClient<MergeHead>
}