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

cem-plugin-custom-jsdoc-tags

v1.1.2

Published

Translates custom JSDoc tags to the Custom Elements Manifest

Downloads

619

Readme

CEM Analyzer Custom JSDoc Plugin

This is a plugin maps custom JSDoc tags on your component classes to properties in Custom Elements Manifest using the Custom Element Manifest Analyzer.

// my-component.ts

/**
 *
 * My custom element does some amazing things
 *
 * @tag my-element
 *
 * @dependency icon
 * @dependency button
 *
 * @since 1.2.5
 *
 */
export class MyElement extends HTMLElement {
{
  "kind": "class",
  "description": "My custom element does some amazing things",
  "name": "MyElement",
  "tagName": "my-element",
  "since": {
    "name": "1.2.5",
    "description": ""
  },
  "dependencies": [
    {
      "name": "icon",
      "description": ""
    },
    {
      "name": "button",
      "description": ""
    }
  ]
}

How It Works

When the analyzer encounters a specified tag, it will add it to a corresponding property in the Custom Elements Manifest for the class with the following data (depending on what you have specified in your tag).

// my-component.ts

/**
 *
 * @custom tag1 - This tag does something incredible
 *
 */
export class MyElement extends HTMLElement {
{
  "custom": {
    "name": "tag1",
    "description": "This tag does something incredible"
  }
}

For you TypeScript fans out there...

[key: string]: {
  name?: string;
  type?: {
    text: string;
  };
  default?: string;
  description?: string;
}

If it encounters another tag by the same name, it will convert the value to an array and add the new tag information to it.

// my-component.ts

/**
 *
 * @custom tag1 - This tag does something incredible
 * @custom tag2 - This tag also does something incredible
 *
 */
export class MyElement extends HTMLElement {
{
  "custom": [
    {
      "name": "tag1",
      "description": "This tag does something incredible"
    },
    {
      "name": "tag2",
      "description": "This tag also does something incredible"
    }
  ]
}

The tag accepts the standard JSDoc syntax so you can specify a name, type, default value, and description.

/**
 * @custom {string | undefined} [tag1=Hello world!] - This tag does something incredible
 */
{
  "custom": {
    "name": "tag1",
    "type": {
      "text": "string | undefined"
    },
    "default": "Hello world!",
    "description": "This tag does something incredible"
  }
}

Usage

Pre-installation

Ensure the following steps have been taken in your component library prior to using this plugin:

Install

npm i -D cem-plugin-custom-jsdoc-tags

Implementation

// custom-elements-manifest.config.js

import { customJSDocTagsPlugin } from "cem-plugin-custom-jsdoc-tags";

export default {
  ...
  /** Provide custom plugins */
  plugins: [
    customJSDocTagsPlugin({
      tags: {
        since: {},
        dependency: {
          mappedName: 'dependencies',
          isArray: true,
        },
      }
    }),
  ],
};

Configuration

The plugin takes the following options:

interface Options {
  /** The custom JSDoc tags you want mapped */
  tags?: {
    /** The name of the JSDoc tag to include in the Custom Elements Manifest */
    [key: string]: {
      /** Alternative name to use in the Custom Elements Manifest */
      mappedName?: string;
      /** Set to true to always make the property value an array */
      isArray?: boolean;
    };
  };
  /** Hides logs produced by the plugin */
  hideLogs: boolean;
  /** Prevents plugin from executing */
  skip: boolean;
}

Tags

The property used to identify the configuration for your JSDoc is the name of the tag you are using in your component. If you don't have any configurations for the tag, you can just add an empty object as the property value.

// my-component.ts

/**
 *
 * @since 1.2.5
 *
 */
export class MyElement extends HTMLElement {
// custom-elements-manifest.config.js

export default {
  ...
  plugins: [
    customJSDocTagsPlugin({
      tags: {
        since: {},
      }
    }),
  ],
};
{
  "name": "MyElement",
  "since": {
    "name": "1.2.5",
    "description": ""
  }
}

Mapped Name

If you need the property name in the custom Elements Manifest to differ from the JSDoc name, you can use the mappedName setting. This is helpful for tags that are singular, but will be accumulated into an array in the manifest, so you want to make the property name plural.

// my-component.ts

/**
 *
 * @dependency icon
 * @dependency button
 *
 */
export class MyElement extends HTMLElement {
// custom-elements-manifest.config.js

export default {
  ...
  plugins: [
    customJSDocTagsPlugin({
      tags: {
        dependency: {
          mappedName: 'dependencies',
        },
      }
    }),
  ],
};
{
  "name": "MyElement",
  "dependencies": [
    {
      "name": "icon",
      "description": ""
    },
    {
      "name": "button",
      "description": ""
    }
  ]
}

Is Array

If you would always like the property value in the manifest to always be an array - even if there is a single value, setting the isArray value to true will convert the value to an array.

// custom-elements-manifest.config.js

export default {
  ...
  plugins: [
    customJSDocTagsPlugin({
      tags: {
        dependency: {
          mappedName: 'dependencies',
          isArray: true,
        },
      }
    }),
  ],
};
// my-component.ts

/**
 *
 * @dependency icon
 *
 */
export class MyElement extends HTMLElement {
{
  "name": "MyElement",
  "dependencies": [
    {
      "name": "icon",
      "description": ""
    }
  ]
}