Skip to content

Commit fa5bf4b

Browse files
authored
feat: introduce overrides option to modify metadata (#99)
1 parent 7f246fc commit fa5bf4b

File tree

5 files changed

+53
-2
lines changed

5 files changed

+53
-2
lines changed

playground/nuxt.config.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ export default defineNuxtConfig({
2323
componentMeta: {
2424
debug: 2,
2525
exclude: [/node_modules/i],
26+
overrides: {
27+
TestComponent: {
28+
props: {
29+
name: {
30+
name: 'name',
31+
type: 'number'
32+
}
33+
}
34+
}
35+
},
2636
metaSources: [
2737
{
2838
TestExternalMeta: {

src/module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ export default defineNuxtModule<ModuleOptions>({
138138
components: [],
139139
metaSources: {},
140140
transformers,
141+
overrides: options.overrides || {},
141142
beforeWrite: async (schema: NuxtComponentMeta) => {
142143
return await nuxt.callHook('component-meta:schema' as any, schema) || schema
143144
}

src/parser/meta-parser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export function useComponentMetaParser (
1717
componentDirs = [],
1818
checkerOptions,
1919
exclude = [],
20+
overrides = {},
2021
transformers = [],
2122
debug = false,
2223
metaFields,
@@ -201,7 +202,7 @@ export function useComponentMetaParser (
201202

202203
Object.assign(
203204
component.meta,
204-
refineMeta(meta, metaFields),
205+
refineMeta(meta, metaFields, overrides[component.pascalName] || {}),
205206
{
206207
hash: codeHash
207208
}

src/parser/utils.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { camelCase } from "scule"
22
import type { ComponentMeta } from 'vue-component-meta'
33
import type { ModuleOptions } from '../types/module'
44

5-
export function refineMeta(meta: ComponentMeta, fields: ModuleOptions['metaFields'] = { type: true, props: true, slots: true, events: true, exposed: true }): ComponentMeta {
5+
export function refineMeta(meta: ComponentMeta, fields: ModuleOptions['metaFields'] = { type: true, props: true, slots: true, events: true, exposed: true }, overrides: ModuleOptions['overrides'][string] = {}): ComponentMeta {
66
const eventProps = new Set<string>(meta.events.map((event :any) => camelCase(`on_${event.name}`)))
77
const props = (fields.props ? meta.props : [])
88
.filter((prop: any) => !prop.global && !eventProps.has(prop.name as string))
@@ -49,6 +49,19 @@ export function refineMeta(meta: ComponentMeta, fields: ModuleOptions['metaField
4949
removeFields(refinedMeta.props, ['schema'])
5050
}
5151

52+
for (const meta in overrides) {
53+
const metaOverrides = overrides[meta as keyof typeof overrides]
54+
const metaFields = refinedMeta[meta as keyof ComponentMeta]
55+
if (Array.isArray(metaFields)) {
56+
for (const fieldName in metaOverrides) {
57+
const override = metaOverrides[fieldName]
58+
const index = metaFields.findIndex((field: any) => field.name === fieldName)
59+
if (index !== -1) {
60+
metaFields[index] = override
61+
}
62+
}
63+
}
64+
}
5265
return refinedMeta
5366
}
5467

src/types/module.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { MetaCheckerOptions } from 'vue-component-meta'
22
import type { ComponentsDir, ComponentsOptions } from '@nuxt/schema'
33
import type { TransformersHookData, ExtendHookData, NuxtComponentMeta } from '.'
4+
import type { JsonSchema } from './schema'
45

56
export interface ModuleOptions {
67
/**
@@ -51,6 +52,31 @@ export interface ModuleOptions {
5152
* Filter all components that are not global.
5253
*/
5354
globalsOnly?: boolean,
55+
overrides: {
56+
[componentName: string]: {
57+
props?: {
58+
[propName: string]: {
59+
"name": string,
60+
"global"?: boolean,
61+
"description"?: string,
62+
"tags"?: Array<{ "name": string, "text": string }>,
63+
"required"?: boolean,
64+
"type": string,
65+
"schema"?: JsonSchema,
66+
"default"?: string
67+
}
68+
}
69+
slots?: {
70+
[slotName: string]: any
71+
}
72+
events?: {
73+
[eventName: string]: any
74+
}
75+
exposed?: {
76+
[exposedName: string]: any
77+
}
78+
}
79+
}
5480
/**
5581
* Filter meta properties to be included in the output.
5682
*/

0 commit comments

Comments
 (0)