Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nuxt): allow generating metadata for nuxt components #26204

Merged
merged 3 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/3.api/5.kit/5.components.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ interface ComponentsDir {
transpile?: 'auto' | boolean
}

// You can augment this interface (exported from `@nuxt/schema`) if needed
interface ComponentMeta {
[key: string]: unknown
}

interface Component {
pascalName: string
kebabName: string
Expand All @@ -54,6 +59,7 @@ interface Component {
island?: boolean
mode?: 'client' | 'server' | 'all'
priority?: number
meta?: ComponentMeta
}
```

Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export async function addComponent (opts: AddComponentOptions) {
mode: 'all',
shortPath: opts.filePath,
priority: 0,
meta: {},
...opts
}

Expand Down
6 changes: 5 additions & 1 deletion packages/nuxt/src/components/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Component, ComponentsDir, ComponentsOptions } from 'nuxt/schema'

import { distDir } from '../dirs'
import { clientFallbackAutoIdPlugin } from './client-fallback-auto-id'
import { componentNamesTemplate, componentsIslandsTemplate, componentsPluginTemplate, componentsTypeTemplate } from './templates'
import { componentNamesTemplate, componentsIslandsTemplate, componentsMetadataTemplate, componentsPluginTemplate, componentsTypeTemplate } from './templates'
import { scanComponents } from './scan'
import { loaderPlugin } from './loader'
import { TreeShakeTemplatePlugin } from './tree-shake'
Expand Down Expand Up @@ -127,6 +127,10 @@ export default defineNuxtModule<ComponentsOptions>({
addTemplate({ filename: 'components.islands.mjs', getContents: () => 'export const islandComponents = {}' })
}

if (componentOptions.generateMetadata) {
addTemplate(componentsMetadataTemplate)
}

const unpluginServer = createTransformPlugin(nuxt, getComponents, 'server')
const unpluginClient = createTransformPlugin(nuxt, getComponents, 'client')

Expand Down
6 changes: 6 additions & 0 deletions packages/nuxt/src/components/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,9 @@ export const componentNames: string[]
`
}
}

export const componentsMetadataTemplate: NuxtTemplate = {
filename: 'components.json',
write: true,
getContents: ({ app }) => JSON.stringify(app.components, null, 2)
}
10 changes: 10 additions & 0 deletions packages/schema/src/types/components.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export interface ComponentMeta {
[key: string]: unknown
}

export interface Component {
pascalName: string
kebabName: string
Expand All @@ -9,6 +13,7 @@ export interface Component {
preload: boolean
global?: boolean | 'sync'
island?: boolean
meta?: ComponentMeta
mode?: 'client' | 'server' | 'all'
/**
* This number allows configuring the behavior of overriding Nuxt components.
Expand Down Expand Up @@ -114,6 +119,11 @@ export interface ComponentsOptions {
* @default false
*/
global?: boolean
/**
* Whether to write metadata to the build directory with information about the components that
* are auto-registered in your app.
*/
generateMetadata?: boolean
loader?: boolean

transform?: {
Expand Down