Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Oct 29, 2024
2 parents 4169dd6 + 52eee5c commit ce12e78
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { Component } from '@nuxt/schema'
import { resolvePathSync } from 'mlly'
import type { ModuleOptions } from './options'
import type { NuxtComponentMeta } from './types'
import { defu } from 'defu'

export type ComponentMetaParserOptions = Omit<ModuleOptions, 'components' | 'metaSources'> & {
components: Component[]
Expand Down Expand Up @@ -210,6 +211,12 @@ export function useComponentMetaParser (
component.meta.exposed = component.meta.exposed.map((sch: any) => stripeTypeScriptInternalTypesSchema(sch, true))
component.meta.events = component.meta.events.map((sch: any) => stripeTypeScriptInternalTypesSchema(sch, true))


const content = await readFile(component.fullPath, 'utf-8')
const extendComponentMetaMatch = content.match(/extendComponentMeta\((\{[\s\S]*?\})\)/);
const extendedComponentMeta = extendComponentMetaMatch?.length ? eval(`(${extendComponentMetaMatch[1]})`) : null
component.meta = defu(component.meta, extendedComponentMeta)

// Remove descriptional fileds to reduce chunk size
removeFields(component.meta, ['declarations'])

Expand Down Expand Up @@ -260,7 +267,6 @@ function removeFields(obj: Record<string, any>, fieldsToRemove: string[]): any {
}
}
}

return obj;
}

Expand Down
1 change: 1 addition & 0 deletions src/runtime/composables/extendComponentMeta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export function extendComponentMeta(_meta: Record<string, any>) { /* Placeholder for extending component meta */ }
6 changes: 6 additions & 0 deletions test/fixtures/basic/components/TestComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
</template>

<script setup>
extendComponentMeta({
hello: 'world',
foo: 'bar'
})
defineProps({
hello: {
type: String,
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/basic/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import nuxtMetaModule from 'nuxt-component-meta'
import nuxtMetaModule from '../../../src/module'

export default defineNuxtConfig({
components: {
Expand Down
5 changes: 5 additions & 0 deletions test/module.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,9 @@ describe('fixtures:basic', async () => {
expect(component.meta.slots[0].name).toBe('title')
expect(component.meta.slots[1].name).toBe('default')
})

test('Test extendComponentMeta', async () => {
const component = await $fetch('/api/component-meta/TestComponent')
expect(component.meta).toMatchObject({ hello: 'world', foo: 'bar' })
})
})

0 comments on commit ce12e78

Please sign in to comment.