The Vuedoc Parser
Generate a JSON documentation for a SFC Vue component.
Install
Features
- Extract the component name (from the name field or from the filename)
- Extract the component description
- Keywords Support: You can define your own keywords with the
@
symbol like
@author Jon Snow
- Extract component model
- Extract component props
- Extract component data
- Extract computed properties with dependencies
- Extract component events
- Extract component slots
- Extract component methods
- Class Component Support
- Vue Property Decorator Support
- JSDoc Support (
@param
and
@return
tags)
Options
name | description |
---|---|
filename | The filename to parse. Required unless filecontent is passed |
filecontent | The file content to parse. Required unless filename is passed |
encoding | The file encoding. Default is 'utf8' |
features | The component features to parse and extract. |
Default features: ['name', 'description', 'keywords', 'slots', 'model', 'props', 'data', 'computed', 'events', 'methods'] |
|
loaders | Use this option to define custom loaders for specific languages |
defaultMethodVisibility | Can be set to 'public' (default), 'protected' , or 'private' |
ignoredVisibilities | List of ignored visibilities. Default: ['protected', 'private'] |
stringify | Set to true to disable parsing of literal values and stringify literal values. Default: false |
Note for stringify
option
By default Vuedoc Parser parses literal values defined in the source code.
This means:
To preserve literal values, set the stringify
option to true
.
Usage
See test/fixtures/checkbox.vue
for an Vue Component decoration example.
This will print this JSON output:
See test/fixtures/checkbox-result.json for the complete result.
Syntax
Add component name
By default, Vuedoc Parser use the component's filename to generate the
component name.
To set a custom name, use the name
field like:
You can also use the @name
keyword to set the component name:
Add component description
To add a component description, just add a comment before the export default
statement like:
Annotate model, props, data and computed properties
To document props, data or computed properties, use comments like:
Vuedoc Parser will automatically extract required
and default
values for
properties and computed properties dependencies. It will also detect type for
each defined data field.
You can set a custom default value of an prop by using the keyword @default
.
To document a v-model
prop, a proper way is to use the Vue's
model field if you use Vue +2.2.0.
You can also use the @model
keyword on a prop if you use an old Vue version:
To document Vue array string props, just attach a Vuedoc comment to each prop:
By default, all extracted things have the public
visibility.
To change this for one entry, add @protected
or @private
keyword.
Annotate methods, events and slots
To document methods or events, just add comments before:
Vuedoc Parser automatically extracts events from component hooks:
Use the JSDoc @param and
@return tags to define parameters and
returning type:
Note:
@arg
is an alias of@param
The parser is also able to extract events and slots from template:
Usage with non primitive name
You can use special keywords @method
and @event
for non primitive name:
Annotate slots defined in Render Functions
To annotate slots defined in Render Functions, just attach the keyword @slot
to the component definition:
You can also use the keyword @slot
to define dynamic slots on template:
Keywords Extraction
You can attach keywords to any comment and then extract them using the parser.
Usage
Note that the description must alway appear before keywords definition
Parsing result:
Working with Mixins
Since Vuedoc Parser don't perform I/O operations, it completely ignores the
mixins
property.
To parse a mixin, you need to parse its file as a standalone component and
then merge the parsing result with the result of the initial component:
Using the keyword @mixin
You can use the special keyword @mixin
to force parsing named exported
component:
Parsing control with options.features
options.features
lets you select which Vue Features you want to parse and
extract.
The default value is defined by Vuedoc.Parser.SUPPORTED_FEATURES
array.
Usage
Only parse name
, props
, computed properties
, slots
and events
:
Parse all features except data
:
Language Processing
Loader API
Build-in loaders
Language | Load by default? | Package |
---|---|---|
HTML | Yes | @vuedoc/parser/loader/html |
JavaScript | Yes | @vuedoc/parser/loader/javascript |
Pug | No | @vuedoc/parser/loader/pug |
TypeScript | No | @vuedoc/parser/loader/typescript |
Vue | Yes | @vuedoc/parser/loader/vue |
TypeScript usage
The Vuedoc Parser package contains a loader for TypeScript. To use it, you need
to:
- install
typescript
and@types/node
dependencies according the
official documentation - import and load the loader
@vuedoc/parser/loader/typescript
Create a custom loader
The example below uses the abstract Vuedoc.Loader
class to create a
specialized class to handle a template with the CoffeeScript
language. It uses the Pug language for templating:
Output