Generating Documentation in JSON format
Stencil supports automatically generating README files in
your project which pull in JSDoc comments and provide a
straightforward way to document your components.
If you need more flexibility, Stencil can also write documentation to a JSON file which you could use for a custom downstream documentation website.
You can try this out is using the --docs-json CLI flag like so:
stencil build --docs-json path/to/docs.json
You can also add the docs-json output target to your project's configuration
file in order to auto-generate this file every time you build:
import { Config } from '@stencil/core';
export const config: Config = {
outputTargets: [
{
type: 'docs-json',
file: 'path/to/docs.json'
}
]
};
The JSON file output by Stencil conforms to the JsonDocs interface in
Stencil's public TypeScript
declarations.
supplementalPublicTypes
As of Stencil v4 the JSON documentation generation functionality in Stencil
supports a new configuration option, supplementalPublicTypes.
This functionality makes it easy to automatically document types and interfaces
which otherwise wouldn't be included in the documentation that Stencil
generates. By default, Stencil includes extensive information about the types
used in the public APIs of all your components, meaning the properties on your
components decorated with @Watch, @Event, @Prop and so on. This makes it
easy to document your components' APIs; however, if your project uses other
types which aren't found in the public API of a component then those types
won't be included.
The new supplementalPublicTypes option fills in this gap by allowing you to
designate a file of types which should be included in the output of the
docs-json output target.
This information will be found in a top-level property called typeLibrary on
the JSON output and will conform to the JsonDocsTypeLibrary interface in
Stencil's public TypeScript
declarations.
Using this option could look something like this:
import { Config } from '@stencil/core';
export const config: Config = {
outputTargets: [
{
type: 'docs-json',
file: 'path/to/docs.json',
supplementalPublicTypes: 'src/public-interfaces.ts',
}
]
};
CSS Variables
Stencil can document CSS variables if you annotate them with JSDoc-style comments in your CSS/SCSS files. If, for instance, you had a component with a CSS file like the following:
:host {
/**
* @prop --background: Background of the button
* @prop --background-activated: Background of the button when activated
* @prop --background-focused: Background of the button when focused
*/
--background: pink;
--background-activated: aqua;
--background-focused: fuchsia;
}
Then you'd get the following in the JSON output:
"styles": [
{
"name": "--background",
"annotation": "prop",
"docs": "Background of the button"
},
{
"name": "--background-activated",
"annotation": "prop",
"docs": "Background of the button when activated"
},
{
"name": "--background-focused",
"annotation": "prop",
"docs": "Background of the button when focused"
}
]