Détail du package

nuxt-component-meta

nuxtlabs139.8kMIT0.13.1

[![npm version][npm-version-src]][npm-version-href] [![npm downloads][npm-downloads-src]][npm-downloads-href]

vue, nuxt, vue component, nuxt component

readme

Nuxt Component Meta

npm version npm downloads

Gather components metadata on build time and make them available on production. This module is developed to give a visual Markdown Editor with Vue Components in it for Nuxt Studio.

Quick Setup

  1. Add nuxt-component-meta dependency to your project:
# Using PNPM
pnpm add nuxt-component-meta

# Using NPM
npm install nuxt-component-meta
  1. Add nuxt-component-meta to the modules section of your nuxt.config.ts
export default defineNuxtConfig({
  modules: ['nuxt-component-meta']
})

Usage

In Nuxt Applications

<template>
  <div>
    <h2>`MyComponent` metadata</h2>
    <pre>
      {{ meta }}
    </pre>
  </div>
</template>

<script script>
const { data: meta } = await useAsyncData('my-component', () => $fetch('/api/component-meta/my-component'))
</script>

Standalone Usage with getComponentMeta

You can also use the getComponentMeta utility directly to extract component metadata programmatically:

import { getComponentMeta } from 'nuxt-component-meta/parser'

// Basic usage
const meta = getComponentMeta('components/MyComponent.vue')

// With options
const meta = getComponentMeta('components/MyComponent.vue', {
  rootDir: '/path/to/project',
  cache: true,
  cacheDir: '.component-meta-cache'
})

// Access component metadata
console.log(meta.props)    // Component props
console.log(meta.slots)    // Component slots  
console.log(meta.events)   // Component events
console.log(meta.exposed)  // Exposed properties

Options

  • rootDir - Project root directory (defaults to process.cwd())
  • cache - Enable caching to improve performance (defaults to false)
  • cacheDir - Directory for cache files (defaults to .data/nuxt-component-meta)

Schema Generation with propsToJsonSchema

The propsToJsonSchema utility converts Vue component props metadata into JSON Schema format, enabling validation and type checking:

import { getComponentMeta } from 'nuxt-component-meta/parser'
import { propsToJsonSchema } from 'nuxt-component-meta/utils'

// Get component metadata
const meta = getComponentMeta('components/MyComponent.vue')

// Convert props to JSON Schema
const jsonSchema = propsToJsonSchema(meta.props)

console.log(jsonSchema)
// Output:
// {
//   "type": "object",
//   "properties": {
//     "title": { "type": "string", "description": "Component title" },
//     "count": { "type": "number", "default": 0 },
//     "enabled": { "type": "boolean", "default": true }
//   },
//   "required": ["title"]
// }

Integration with Validation Libraries

The generated JSON Schema can be used with popular validation libraries:

import { jsonSchemaToZod } from 'json-schema-to-zod'
import Ajv from 'ajv'

// With Zod
const zodString = jsonSchemaToZod(jsonSchema)
const zodSchema = eval(zodString)
const result = zodSchema.safeParse(componentProps)

// With AJV
const ajv = new Ajv()
const validate = ajv.compile(jsonSchema)
const isValid = validate(componentProps)

Nightly Builds

This module uses pkg.pr.new for continuous releases. Each commit to the main branch automatically publishes a new version with its own unique URL, allowing you to test the latest changes before they're officially released.

You can install a specific commit using its unique URL:

npm i https://pkg.pr.new/nuxtlabs/nuxt-component-meta@<commit-hash>

Development

  1. Clone this repository
  2. Install dependencies using pnpm install
  3. Start dev server using pnpm dev

License

MIT License

Copyright (c) NuxtLabs

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

0.3.7 (2022-12-06)

Features

  • content: remove rest in content transformer (4a3d063)

0.3.6 (2022-12-06)

Bug Fixes

  • module: update transformers (2f57524)

0.3.5 (2022-11-27)

Features

  • transformers: improve transformers feature ; add @nuxt/content support by default (93d176c)

0.3.4 (2022-11-16)

0.3.3 (2022-11-01)

Features

  • unplugin: move parsing steps to unplugin (#43) (ae5ae18)

Bug Fixes

  • hmr: fix hmr; avoid relying on transformers (75e192b)

0.2.3 (2022-09-20)

0.2.2 (2022-08-31)

Bug Fixes

  • add tsconfig path to vue-component-meta (#40) (a32212b)

0.2.1 (2022-08-29)

Bug Fixes

0.2.0 (2022-08-29)

⚠ BREAKING CHANGES

  • use vue-component-meta (#34)

Features

Bug Fixes

0.1.9 (2022-07-21)

Features

Bug Fixes

0.1.8 (2022-07-20)

Features

  • detect ts as expression and Vue's PropType (4d2388d)

Bug Fixes

  • check for object properties (0f20817)

0.1.7 (2022-07-19)

Bug Fixes

  • prevent undefined error (5a403f4)

0.1.6 (2022-07-19)

Features

  • add flag for global components (#27) (6e70ad3)
  • basic support for typescript defineProps (#25) (60cfbaf)
  • detect $slots usages in template (#26) (ec35351)
  • extract props info from defineProps (#21) (8f89275)

0.1.5 (2022-05-11)

Bug Fixes

  • api: fix single component query (5f967c5)
  • lint: fix linting (237b16f)
  • use addServerHandler and correct method (3d247cc)

0.1.2 (2022-04-19)

0.1.0 (2022-04-15)

Features

0.0.7 (2021-11-22)

Bug Fixes

0.0.6 (2021-11-22)

Bug Fixes

  • resolve components path (9b071d3)

0.0.5 (2021-11-16)

Features

0.0.4 (2021-11-09)

Bug Fixes

0.0.3 (2021-10-13)

Bug Fixes

0.0.2 (2021-10-13)

Bug Fixes

  • rename props key to name (8781217)

0.0.1 (2021-10-13)

Features

  • provide slots, required, value and description meta (81b2a5c)