2021-09-27 12:49:36 +00:00
|
|
|
import { resolve } from 'pathe'
|
2023-08-14 19:33:00 +00:00
|
|
|
import { addComponent, addImportsSources, addPlugin, addTemplate, defineNuxtModule, tryResolveModule } from '@nuxt/kit'
|
2024-05-07 12:37:02 +00:00
|
|
|
import type { NuxtOptions } from '@nuxt/schema'
|
2021-08-11 21:26:47 +00:00
|
|
|
import { distDir } from '../dirs'
|
2021-07-15 11:28:04 +00:00
|
|
|
|
2022-11-16 02:26:35 +00:00
|
|
|
const components = ['NoScript', 'Link', 'Base', 'Title', 'Meta', 'Style', 'Head', 'Html', 'Body']
|
2022-10-13 15:54:06 +00:00
|
|
|
|
2024-05-07 12:37:02 +00:00
|
|
|
export default defineNuxtModule<NuxtOptions['unhead']>({
|
2022-01-05 18:09:53 +00:00
|
|
|
meta: {
|
2024-12-09 12:38:25 +00:00
|
|
|
name: 'nuxt:meta',
|
2024-07-11 06:05:47 +00:00
|
|
|
configKey: 'unhead',
|
2022-01-05 18:09:53 +00:00
|
|
|
},
|
2023-03-10 14:55:01 +00:00
|
|
|
async setup (options, nuxt) {
|
2023-03-10 08:01:21 +00:00
|
|
|
const runtimeDir = resolve(distDir, 'head/runtime')
|
2021-07-15 11:28:04 +00:00
|
|
|
|
2023-03-08 15:32:24 +00:00
|
|
|
// Transpile @unhead/vue
|
|
|
|
nuxt.options.build.transpile.push('@unhead/vue')
|
2021-08-11 20:28:38 +00:00
|
|
|
|
2022-10-13 15:54:06 +00:00
|
|
|
// Register components
|
|
|
|
const componentsPath = resolve(runtimeDir, 'components')
|
|
|
|
for (const componentName of components) {
|
|
|
|
addComponent({
|
|
|
|
name: componentName,
|
|
|
|
filePath: componentsPath,
|
|
|
|
export: componentName,
|
2023-03-06 11:33:40 +00:00
|
|
|
// built-in that we do not expect the user to override
|
|
|
|
priority: 10,
|
2022-10-13 15:54:06 +00:00
|
|
|
// kebab case version of these tags is not valid
|
2024-04-05 18:08:32 +00:00
|
|
|
kebabName: componentName,
|
2022-10-13 15:54:06 +00:00
|
|
|
})
|
|
|
|
}
|
2023-03-10 08:01:21 +00:00
|
|
|
|
2023-03-10 16:45:22 +00:00
|
|
|
// allow @unhead/vue server composables to be tree-shaken from the client bundle
|
2024-03-21 12:17:09 +00:00
|
|
|
if (!nuxt.options.dev) {
|
|
|
|
nuxt.options.optimization.treeShake.composables.client['@unhead/vue'] = [
|
2024-04-05 18:08:32 +00:00
|
|
|
'useServerHead', 'useServerSeoMeta', 'useServerHeadSafe',
|
2024-03-21 12:17:09 +00:00
|
|
|
]
|
|
|
|
}
|
2023-03-10 16:45:22 +00:00
|
|
|
|
2023-03-10 08:01:21 +00:00
|
|
|
addImportsSources({
|
|
|
|
from: '@unhead/vue',
|
|
|
|
// hard-coded for now we so don't support auto-imports on the deprecated composables
|
|
|
|
imports: [
|
|
|
|
'injectHead',
|
|
|
|
'useHead',
|
|
|
|
'useSeoMeta',
|
|
|
|
'useHeadSafe',
|
|
|
|
'useServerHead',
|
|
|
|
'useServerSeoMeta',
|
2024-04-05 18:08:32 +00:00
|
|
|
'useServerHeadSafe',
|
|
|
|
],
|
2023-03-10 08:01:21 +00:00
|
|
|
})
|
|
|
|
|
2023-03-08 15:32:24 +00:00
|
|
|
// Opt-out feature allowing dependencies using @vueuse/head to work
|
2023-10-22 07:39:56 +00:00
|
|
|
const unheadVue = await tryResolveModule('@unhead/vue', nuxt.options.modulesDir) || '@unhead/vue'
|
2022-10-13 15:54:06 +00:00
|
|
|
|
2023-08-14 19:33:00 +00:00
|
|
|
addTemplate({
|
|
|
|
filename: 'unhead-plugins.mjs',
|
|
|
|
getContents () {
|
|
|
|
if (!nuxt.options.experimental.headNext) {
|
|
|
|
return 'export default []'
|
|
|
|
}
|
2023-10-22 07:39:56 +00:00
|
|
|
return `import { CapoPlugin } from ${JSON.stringify(unheadVue)};
|
2023-12-14 13:58:25 +00:00
|
|
|
export default import.meta.server ? [CapoPlugin({ track: true })] : [];`
|
2024-04-05 18:08:32 +00:00
|
|
|
},
|
2023-08-14 19:33:00 +00:00
|
|
|
})
|
|
|
|
|
2024-05-07 12:37:02 +00:00
|
|
|
addTemplate({
|
|
|
|
filename: 'unhead.config.mjs',
|
|
|
|
getContents () {
|
|
|
|
return [
|
|
|
|
`export const renderSSRHeadOptions = ${JSON.stringify(options.renderSSRHeadOptions || {})}`,
|
|
|
|
].join('\n')
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2023-08-14 19:33:00 +00:00
|
|
|
// template is only exposed in nuxt context, expose in nitro context as well
|
|
|
|
nuxt.hooks.hook('nitro:config', (config) => {
|
2024-10-09 12:58:05 +00:00
|
|
|
config.virtual!['#internal/unhead-plugins.mjs'] = () => nuxt.vfs['#build/unhead-plugins.mjs']
|
|
|
|
config.virtual!['#internal/unhead.config.mjs'] = () => nuxt.vfs['#build/unhead.config.mjs']
|
2023-08-14 19:33:00 +00:00
|
|
|
})
|
2023-08-01 19:47:31 +00:00
|
|
|
|
2023-03-08 15:32:24 +00:00
|
|
|
// Add library-specific plugin
|
2023-03-10 08:01:21 +00:00
|
|
|
addPlugin({ src: resolve(runtimeDir, 'plugins/unhead') })
|
2024-04-05 18:08:32 +00:00
|
|
|
},
|
2021-07-15 11:28:04 +00:00
|
|
|
})
|