npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

kdu-component-meta

v1.2.0

Published

`kdu-component-meta` allows you to extract the meta-data like props, slots, events, etc from your components via static code analysis. You can even generate description for your props from your source code. This helps document your components via automati

Downloads

19

Readme

kdu-component-meta

kdu-component-meta allows you to extract the meta-data like props, slots, events, etc from your components via static code analysis. You can even generate description for your props from your source code. This helps document your components via automation.

Guide 📗

First of all, you need to create a component meta checker using createComponentMetaChecker:

import * as url from 'url'
import path from 'path'

import type { MetaCheckerOptions } from 'kdu-component-meta'
import { createComponentMetaChecker } from 'kdu-component-meta'

const __dirname = url.fileURLToPath(new URL('.', import.meta.url))

const checkerOptions: MetaCheckerOptions = {
  forceUseTs: true,
  schema: { ignore: ['MyIgnoredNestedProps'] },
  printer: { newLine: 1 },
}

const tsconfigChecker = createComponentMetaChecker(
  // Write your tsconfig path
  path.join(__dirname, 'path-to-tsconfig'),
  checkerOptions,
)

Now, you can extract the component meta using getComponentMeta method of checker:

import * as url from 'url'
import path from 'path'

const __dirname = url.fileURLToPath(new URL('.', import.meta.url))

const componentPath = path.join(__dirname, 'path-to-component');
const meta = checker.getComponentMeta(componentPath);

This meta contains really useful stuff like component props, slots, events and more.

Extracting prop meta

kdu-component-meta will automatically extract the prop details like its name, default value, is required or not, etc. Additionally, you can even write prop description in source code via JSDoc comment for that prop.

/**
 * Hide/Show alert based on k-model value
 */
modelValue: {
  type: Boolean,
  default: null,
},

When you extract the component meta and extract the description property of that prop it will be "Hide/Show alert based on k-model value" 😍

Warning

Do note that meta.props will be array of props so you can't access it via meta.props.<prop-name>. Moreover, meta.props will also contain some global prop which you can identify via prop.global property.

You can use it to document your component as you build your project without writing additional documentation.

Pitfalls 👀

As kdu-component-meta uses static code analysis, it can't extract the dynamic prop definition.

default value

kdu-component-meta won't be able to extract default value for prop as props can't be analyzed.

props: {
  // Props definition by function execution
  ...useLayerProps({
    color: {
      default: 'primary',
    },
    variant: {
      default: 'light',
    },
  }),
}

In this scenario, to get the correct default value you can let kdu-component-meta know it by writing them explicitly:

props: {
  // let kdu-component-meta found it
  color: { default: 'primary' },
  variant: { default: 'light' },

  // Props definition by function execution
  ...useLayerProps({
    color: {
      default: 'primary',
    },
    variant: {
      default: 'light',
    },
  }),
}

description

Same as above scenario you might have issue with description not generating when prop definition is dynamic. In this case writing prop description can be tricky.

When it's function execution, write prop description in function definition:

export const useLayerProp = (...) => {
  const props = {
       /**
        * Layer variant
        */
       variant: {
         type: String,
         default: 'text',
       },
  }

  export { props }
}

required

For generating the correct required value for props like below:

// @/composables/useProps.ts
export const disabled = {
  type: Boolean,
  default: false,
}
import { disabled } from '@/composables/useProps'

export default defineComponent({
  props: {
    disabled,
  },
})

You need to add as const to variable definition:

 export const disabled = {
   type: Boolean,
   default: false,
- }
+ } as const