diff --git a/build.config.ts b/build.config.ts new file mode 100644 index 00000000..31a1c7b5 --- /dev/null +++ b/build.config.ts @@ -0,0 +1,19 @@ +import { defineBuildConfig } from 'unbuild' + +export default defineBuildConfig({ + declaration: true, + rollup: { + emitCJS: true, + }, + entries: [ + { input: 'src/content', name: 'content' }, + ], + externals: [ + 'h3', + 'std-env', + 'nitropack', + 'consola', + '@nuxt/content', + 'zod' + ], +}) diff --git a/content.d.ts b/content.d.ts new file mode 100644 index 00000000..448dc96e --- /dev/null +++ b/content.d.ts @@ -0,0 +1 @@ +export * from './dist/content' diff --git a/docs/content/2.guides/3.content.md b/docs/content/2.guides/3.content.md index 063ecde5..93b90839 100644 --- a/docs/content/2.guides/3.content.md +++ b/docs/content/2.guides/3.content.md @@ -13,8 +13,8 @@ In Nuxt Content v3 we need to use the `asRobotsCollection()`{lang="ts"} function to be able to use the `robots` frontmatter key. ```ts [content.config.ts] -import { defineCollection, defineContentConfig, z } from '@nuxt/content' -import { asRobotsCollection } from '@nuxtjs/robots' +import { defineCollection, defineContentConfig } from '@nuxt/content' +import { asRobotsCollection } from '@nuxtjs/robots/content' export default defineContentConfig({ collections: { @@ -39,6 +39,7 @@ const route = useRoute() const { data: page } = await useAsyncData(`page-${route.path}`, () => { return queryCollection('content').path(route.path).first() }) +// Ensure the SEO meta tags are rendered useSeoMeta(page.value.seo) ``` diff --git a/package.json b/package.json index 8448cf4d..44926b1d 100644 --- a/package.json +++ b/package.json @@ -36,12 +36,29 @@ "./dist/runtime/util": { "types": "./dist/runtime/util.d.ts", "import": "./dist/runtime/util.js" + }, + "./content": { + "types": "./dist/content.d.ts", + "import": "./dist/content.mjs", + "require": "./dist/content.cjs" } }, "main": "./dist/module.cjs", "types": "./dist/types.d.ts", + "typesVersions": { + "*": { + "content": [ + "dist/content" + ], + "utils": [ + "dist/utils" + ] + } + }, "files": [ - "dist" + "content.d.ts", + "dist", + "util.d.ts" ], "scripts": { "lint": "eslint . --fix", @@ -100,12 +117,5 @@ }, "resolutions": { "typescript": "5.6.3" - }, - "build": { - "externals": [ - "h3", - "consola", - "@nuxt/content" - ] } } diff --git a/src/content.ts b/src/content.ts index 98417382..e851b970 100644 --- a/src/content.ts +++ b/src/content.ts @@ -1,20 +1,17 @@ +import type { Collection } from '@nuxt/content' +import type { TypeOf, ZodRawShape } from 'zod' import { z } from '@nuxt/content' -export function asRobotsCollection(collection: any) { - if (collection.type !== 'page') { - return - } - if (!collection.schema) { - collection.schema = z.object({ - robots: z.union([z.string(), z.boolean()]).optional(), - }) - } - else { - collection.schema = collection.schema.extend({ - robots: z.union([z.string(), z.boolean()]).optional(), - }) +export const schema = z.object({ + robots: z.union([z.string(), z.boolean()]).optional(), +}) + +export type RobotSchema = TypeOf + +export function asRobotsCollection(collection: Collection): Collection { + if (collection.type === 'page') { + // @ts-expect-error untyped + collection.schema = collection.schema ? collection.schema.extend(schema) : schema } - collection._integrations = collection._integrations || [] - collection._integrations.push('robots') return collection } diff --git a/src/module.ts b/src/module.ts index b529936d..a9045345 100644 --- a/src/module.ts +++ b/src/module.ts @@ -169,8 +169,6 @@ export interface ModulePublicRuntimeConfig { ['nuxt-robots']: ResolvedModuleOptions } -export * from './content' - export default defineNuxtModule({ meta: { name: '@nuxtjs/robots', diff --git a/test/fixtures/content-v3/content.config.ts b/test/fixtures/content-v3/content.config.ts index c431cc11..5191e1ca 100644 --- a/test/fixtures/content-v3/content.config.ts +++ b/test/fixtures/content-v3/content.config.ts @@ -1,5 +1,5 @@ import { defineCollection, defineContentConfig, z } from '@nuxt/content' -import { asRobotsCollection } from '../../../src/module' +import { asRobotsCollection } from '../../../src/content' export default defineContentConfig({ collections: { diff --git a/util.d.ts b/util.d.ts new file mode 100644 index 00000000..be263d47 --- /dev/null +++ b/util.d.ts @@ -0,0 +1 @@ +export * from './dist/runtime/util'