Skip to content

feat: support extendComponentMeta in component parser utility #87

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { isAbsolute, join } from "pathe"
import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'fs'
import { withBase } from "ufo"
import { hash } from "crypto"
import { defu } from 'defu'

export interface Options {
rootDir: string
Expand Down Expand Up @@ -66,7 +67,18 @@ function _getComponentMeta(fullPath: string, opts: Options) {
exclude: []
},
)
return refineMeta(

const baseMeta = refineMeta(
checker.getComponentMeta(fullPath)
)
}

// Apply extendComponentMeta(...) overrides from the component source
try {
const code = readFileSync(fullPath, 'utf-8')
const extendComponentMetaMatch = code.match(/extendComponentMeta\((\{[\s\S]*?\})\)/)
const extendedComponentMeta = extendComponentMetaMatch?.length ? eval(`(${extendComponentMetaMatch[1]})`) : null
return defu(baseMeta, extendedComponentMeta) as ComponentMeta
} catch {
return baseMeta
}
}
34 changes: 34 additions & 0 deletions test/fixtures/basic/components/ExtendMetaComponent.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<template>
<div>
<h3>Extended Meta Component</h3>
<p>This component has extended metadata</p>
<slot />
</div>
</template>

<script setup>

extendComponentMeta({
description: 'A component that demonstrates extendComponentMeta functionality',
version: '1.0.0',
tags: ['test', 'meta'],
customData: {
for: 'Test Suite',
category: 'utility'
}
})

defineProps({
title: {
type: String,
default: 'Default Title'
},
enabled: {
type: Boolean,
default: true
}
})

defineEmits(['updated'])

</script>
53 changes: 53 additions & 0 deletions test/get-component-meta.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,57 @@ describe("get-component-meta", () => {
expect(meta.props.length).toEqual(4);
expect((meta as unknown as Record<string, unknown>).cachedAt).toBeDefined();
});

test("parse ExtendMetaComponent with extendComponentMeta", { timeout: 10000 }, () => {
const meta = getComponentMeta("components/ExtendMetaComponent.vue", {
rootDir,
})

// Check basic component metadata from defineProps/defineEmits
expect(meta.props.length).toEqual(2); // title, enabled
expect(meta.props.find(p => p.name === 'title')).toBeDefined();
expect(meta.props.find(p => p.name === 'enabled')).toBeDefined();
expect(meta.events.length).toEqual(1);
expect(meta.events[0].name).toEqual('updated');

// Check that extendComponentMeta adds custom metadata fields
const extendedMeta = meta as unknown as Record<string, unknown>;
expect(extendedMeta.description).toEqual('A component that demonstrates extendComponentMeta functionality');
expect(extendedMeta.version).toEqual('1.0.0');
expect(extendedMeta.tags).toEqual(['test', 'meta']);
expect(extendedMeta.customData).toEqual({
for: 'Test Suite',
category: 'utility'
});
});

test("parse ExtendMetaComponent with extendComponentMeta (cached)", { timeout: 10000 }, () => {
const meta = getComponentMeta("components/ExtendMetaComponent.vue", {
rootDir,
cache: true
})

// Check that extendComponentMeta custom fields persist through caching
const extendedMeta = meta as unknown as Record<string, unknown>;
expect(extendedMeta.description).toEqual('A component that demonstrates extendComponentMeta functionality');
expect(extendedMeta.version).toEqual('1.0.0');
expect(extendedMeta.tags).toEqual(['test', 'meta']);
expect(extendedMeta.customData).toEqual({
for: 'Test Suite',
category: 'utility'
});
expect(extendedMeta.cachedAt).toBeUndefined();
});

test("parse ExtendMetaComponent cached retrieval", { timeout: 10000 }, () => {
const meta = getComponentMeta("components/ExtendMetaComponent.vue", {
rootDir,
cache: true
})

// Check that extendComponentMeta custom fields are preserved in cached version
const extendedMeta = meta as unknown as Record<string, unknown>;
expect(extendedMeta.description).toEqual('A component that demonstrates extendComponentMeta functionality');
expect(extendedMeta.cachedAt).toBeDefined();
});
});