2025-01-22 04:48:27 +00:00
|
|
|
import { resolve } from 'pathe'
|
|
|
|
import { addBuildPlugin, addComponent, 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'
|
2025-01-22 04:48:27 +00:00
|
|
|
import { UnheadImportsPlugin } from './plugins/unhead-imports'
|
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
|
|
|
},
|
2025-01-22 06:11:38 +00:00
|
|
|
async setup (options, nuxt) {
|
2023-03-10 08:01:21 +00:00
|
|
|
const runtimeDir = resolve(distDir, 'head/runtime')
|
2025-01-21 09:38:51 +00:00
|
|
|
|
|
|
|
// Transpile @unhead/vue
|
|
|
|
nuxt.options.build.transpile.push('@unhead/vue')
|
|
|
|
|
2025-01-22 04:48:27 +00:00
|
|
|
const isNuxtV4 = nuxt.options._majorVersion === 4 || nuxt.options.future?.compatibilityVersion === 4
|
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
|
|
|
|
2025-01-22 04:48:27 +00:00
|
|
|
nuxt.options.alias['#unhead/composables'] = resolve(runtimeDir, 'composables', isNuxtV4 ? 'v4' : 'v3')
|
|
|
|
addBuildPlugin(UnheadImportsPlugin({
|
|
|
|
sourcemap: !!nuxt.options.sourcemap.server,
|
|
|
|
rootDir: nuxt.options.rootDir,
|
|
|
|
}))
|
2022-10-13 15:54:06 +00:00
|
|
|
|
2025-02-14 07:04:03 +00:00
|
|
|
const unheadPlugins = await tryResolveModule('@unhead/vue/plugins', nuxt.options.modulesDir) || '@unhead/vue/plugins'
|
2023-08-14 19:33:00 +00:00
|
|
|
addTemplate({
|
2025-01-14 09:03:48 +00:00
|
|
|
filename: 'unhead-options.mjs',
|
2025-01-22 06:11:38 +00:00
|
|
|
getContents () {
|
2025-02-12 01:44:19 +00:00
|
|
|
if (isNuxtV4 && !options.legacy) {
|
2025-01-14 09:03:48 +00:00
|
|
|
return `export default {}`
|
2023-08-14 19:33:00 +00:00
|
|
|
}
|
2025-01-14 09:03:48 +00:00
|
|
|
// v1 unhead legacy options
|
|
|
|
const disableCapoSorting = !nuxt.options.experimental.headNext
|
2025-02-14 07:04:03 +00:00
|
|
|
return `import { DeprecationsPlugin, PromisesPlugin, TemplateParamsPlugin, AliasSortingPlugin } from ${JSON.stringify(unheadPlugins)};
|
2025-01-14 09:03:48 +00:00
|
|
|
export default {
|
2025-01-22 06:11:38 +00:00
|
|
|
disableCapoSorting: ${Boolean(disableCapoSorting)},
|
2025-02-14 07:04:03 +00:00
|
|
|
plugins: [DeprecationsPlugin, PromisesPlugin, TemplateParamsPlugin, AliasSortingPlugin],
|
2025-01-14 09:03:48 +00:00
|
|
|
}`
|
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) => {
|
2025-01-14 09:03:48 +00:00
|
|
|
config.virtual!['#internal/unhead-options.mjs'] = () => nuxt.vfs['#build/unhead-options.mjs']
|
2024-10-09 12:58:05 +00:00
|
|
|
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
|
|
|
})
|