nuxtlabs / nuxt-component-meta

Gather Nuxt components metadata on build time and make them available on production.
69 stars 6 forks source link

feat: extract props info from `defineProps` #21

Closed farnabaz closed 1 year ago

farnabaz commented 2 years ago

Extract props info from script setup

stafyniaksacha commented 2 years ago

Hi there!

I have a use case where components share some props:

First, let's assume we have a component responsible to display images:

<script setup lang="ts">
export type ImageComponentSize = 'small' | 'medium' | 'large'
export interface ImageComponentProps {
  src: string
  srcDark?: string
  size?: ImageComponentSize
}

const props = withDefaults(defineProps<ImageComponentProps>(), {
  srcDark: undefined,
  size: 'medium',
})
</script>

Then we want to create a simple Gallery that reuse this component (without slots):

<script setup lang="ts">
import type { ImageComponentProps } from './ImageComponent.vue'

export interface GalleryProps {
  images: ImageComponentProps[]
}

const props = withDefaults(defineProps<GalleryProps>(), {})
</script>

I was trying to get them by simply parse ASTs (either with compileScript from vue sfc compiler or createSourceFile from typescript) but to handle this case we have to resolve the import type, which can be really memory consuming.

Maybe we can afford this time/memory because we are performing those operations only once at the built time, What do you think?

Atinux commented 2 years ago

Agree we don’t mind about the memory since it is only for build time