2.0.0 • Published 6 months ago

web-component-analyzer v2.0.0

Weekly downloads
15,812
License
MIT
Repository
github
Last release
6 months ago

web-component-analyzer is a CLI that makes it possible to easily analyze web components. It analyzes your code and jsdoc in order to extract properties, attributes, methods, events, slots, css shadow parts and css custom properties. Works with both javascript and typescript.

Try the online playground here

In addition to vanilla web components this tool supports web components built with the following libraries:

-----------------------------------------------------

➤ Installation

$ npm install -g web-component-analyzer

or

$ npx web-component-analyzer src

-----------------------------------------------------

➤ Usage

$ wca analyze
$ wca analyze src --format markdown
$ wca analyze "src/**/*.{js,ts}" --outDir components
$ wca analyze my-element.js --outFile custom-elements.json
$ wca analyze --outFiles {dir}/custom-element.json

The analyze command analyses an optional <input glob> and emits the output to the console as default. When the <input glob> is omitted it will find all components excluding node_modules. The default format is markdown.

Options

OptionTypeDescription
--format <format>markdown | json | vscodeSpecify output format. Default is markdown.
--outDir <path>directory pathDirect output to a directory where each file corresponds to a web component.
--outFile <path>file pathConcatenate and emit output to a single file.
--outFiles <path>file path with patternEmit output to multiple files using a pattern. Available substitutions:{dir}: The directory of the component{filename}: The filename (without ext) of the component{tagname}: The element's tag name
--visibility <visibility>public | protected | privateThe mininmum member visibility to output. Default is public.
--features <features>member | method | cssproperty | csspart | event | slotChoose specific features to output. Multiple features are given seperated by a space. All features are enabled as default.Example: --features member slot event
--drybooleanDon't write any files

-----------------------------------------------------

➤ Output Formats

json

wca analyze src --format json --outFile custom-elements.json

Try the online playground here

This json format is for experimental and demo purposes, and is still being actively discussed. You can expect changes to this format. Please follow and contribute to the discussion at:

markdown

wca analyze src --format markdown --outDir readme

Try the online playground here

Web Component Analyzer can output markdown documentation of your web components. This can either be output into a single file using --outFile or into multiple files using --outDir.

vscode

wca analyze src --format vscode --outFile vscode-html-custom-data.json

VSCode supports a JSON format called vscode custom data for the built in html editor which is set using html.customData vscode setting. Web Component Analyzer can output this format.

-----------------------------------------------------

-----------------------------------------------------

➤ How to document your components using JSDoc

In addition to analyzing the code of your components, this library also use JSDoc to construct the documentation. It's especially a good idea to use JSDoc for documenting slots, events, css custom properties and css shadow parts as these not analyzed statically by this tool as of now (except when constructing a CustomEvent within your component).

Here's an example including all supported JSDoc tags. All JSDoc tags are on the the form @tag {type} name - comment and @tag {type} [name=default] - comment.

/**
 * Here is a description of my web component.
 * 
 * @element my-element
 * 
 * @fires change - This jsdoc tag makes it possible to document events.
 * @fires submit
 * 
 * @attr {Boolean} disabled - This jsdoc tag documents an attribute.
 * @attr {on|off} switch - Here is an attribute with either the "on" or "off" value.
 * @attr [my-attr=default value]
 * 
 * @prop {String} myProp - You can use this jsdoc tag to document properties.
 * @prop value
 * 
 * @slot - This is an unnamed slot (the default slot)
 * @slot start - This is a slot named "start".
 * @slot end
 * 
 * @cssprop --main-bg-color - This jsdoc tag can be used to document css custom properties.
 * @cssprop [--main-color=red]

 * @csspart container 
 */
class MyElement extends HTMLElement {

 /**
  * This is a description of a property with an attribute with exactly the same name: "color".
  * @type {"red"|"green"|"blue"}
  * @attr
  */
  color = "red";

  /**
   * This is a description of a property with an attribute called "my-prop".
   * @type {number}
   * @deprecated
   * @attr my-prop
   */
  myProp = 10

  static get observedAttributes () {
    return [
      /**
       * The header text of this element
       */
      "header"
    ];
  }

}

Overview of supported JSDoc tags

JSDoc TagDescription
@elementGives your component a tag name. This JSDoc tag is useful if your 'customElements.define` is called dynamically eg. using a custom function.
@firesDocuments events.
@slotDocuments slots. Using an empty name here documents the unnamed (default) slot.
@attr or @attributeDocuments an attribute on your component.
@prop or @propertyDocuments a property on your component.
@cssprop or @csspropertyDocuments a css custom property on your component.
@csspartDocuments a css shadow part on your component.

-----------------------------------------------------

➤ How does this tool analyze my components?

This tool extract information about your components by looking at your code directly and by looking at your JSDoc comments.

Code: Web Component Analyzer supports multiple libraries. Click here for an overview of how each library is analyzed.

JSDoc: Read next section to learn more about how JSDoc is analyzed.

➤ API

You can also directly use the underlying functionality of this tool if you don't want to use the CLI. Web Component Analyzer analyzes Typescript source files, so you will have to include the Typescript parser. Here are some examples of how to use the API.

Analyze Typescript source file

import { analyzeSourceFile } from "web-component-analyzer";

const result = analyzeSourceFile(sourceFile, { checker });

Analyze text

import { analyzeText } from "web-component-analyzer";

const code = `class MyElement extends HTMLElement {

}

customElements.define("my-element", MyElement);
`;


const { results, program } = analyzeText(code);
// or
const { results, program } = analyzeText([
  { fileName: "file1.js", text: code },
  { fileName: "file2.js", text: "..." }, // these files can depend on each other
  { fileName: "file3.js", text: "...", analyze: false }
]);
// each result in "results" is the result of analyzing the corresponding text where "analyze" is not false

Transform the result

import { transformAnalyzerResult } from "web-component-analyzer";

const result = // the result of analyzing the component using one of the above functions

const format = "markdown"; // or "json"

const output = transformAnalyzerResult(format, result, program);

// "output" is now a string containing the result of the "markdown" transformer

-----------------------------------------------------

➤ Contributors

Rune Mehlsen

-----------------------------------------------------

➤ License

Licensed under MIT.

2.0.0

6 months ago

2.0.0-next.5

11 months ago

1.1.7

1 year ago

2.0.0-next.4

2 years ago

2.0.0-next.2

3 years ago

2.0.0-next.3

3 years ago

2.0.0-next.1

3 years ago

2.0.0-next.0

3 years ago

1.2.0-next.0

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.1

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.0

4 years ago

1.1.0-next.3

4 years ago

1.1.0-next.2

4 years ago

1.1.0-next.1

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

1.0.0-next.18

4 years ago

1.0.0-next.17

4 years ago

1.0.0-next.16

4 years ago

1.0.0-next.15

4 years ago

1.0.0-next.14

4 years ago

1.0.0-next.13

4 years ago

1.0.0-next.11

4 years ago

1.0.0-next.10

4 years ago

1.0.0-next.12

4 years ago

1.0.0-next.2

4 years ago

1.0.0-next.3

4 years ago

1.0.0-next.4

4 years ago

1.0.0-next.5

4 years ago

1.0.0-next.6

4 years ago

1.0.0-next.7

4 years ago

1.0.0-next.8

4 years ago

1.0.0-next.9

4 years ago

1.0.0-next.1

4 years ago

1.0.0-next.0

4 years ago

0.1.21

4 years ago

0.1.20

4 years ago

0.1.19

4 years ago

0.1.18

4 years ago

0.1.17

5 years ago

0.1.16

5 years ago

0.1.15

5 years ago

0.1.14

5 years ago

0.1.13

5 years ago

0.1.12

5 years ago

0.1.11

5 years ago

0.1.10

5 years ago

0.1.9

5 years ago

0.1.8

5 years ago

0.1.7

5 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.43

5 years ago

0.0.42

5 years ago

0.0.41

5 years ago

0.0.40

5 years ago

0.0.39

5 years ago

0.0.38

5 years ago

0.0.37

5 years ago

0.0.36

5 years ago

0.0.35

5 years ago

0.0.34

5 years ago

0.0.33

5 years ago

0.0.32

5 years ago

0.0.31

5 years ago

0.0.30

5 years ago

0.0.29

5 years ago

0.0.27

5 years ago

0.0.26

5 years ago

0.0.25

5 years ago

0.0.24

5 years ago

0.0.22

5 years ago

0.0.21

5 years ago

0.0.20

5 years ago

0.0.19

5 years ago

0.0.18

5 years ago

0.0.17

5 years ago

0.0.16

5 years ago

0.0.15

5 years ago

0.0.14

5 years ago

0.0.13

5 years ago

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago