Vue Styleguidist
Vue Styleguidist is a living style guide generator and isolated development environment for Vue components. It enables developers to document components with live, editable usage examples based on Markdown files, fostering a component-driven development workflow. Currently at version 4.72.4, it generally maintains a frequent patch and minor release cadence, often addressing compatibility with newer Vue versions or fixing parsing issues in its underlying `vue-docgen-api` dependency. Its key differentiators include its heritage from React Styleguidist, providing a similar interactive documentation experience, and its specific tooling for parsing and compiling `.vue` single-file components. It supports both Vue 2 and Vue 3 environments (requiring appropriate `vue-loader` and `@vue/compiler-sfc` peer dependencies respectively) and integrates with webpack for asset loading.
Common errors
-
[Vue Styleguidist] Error: Cannot find module 'vue-template-compiler' or '@vue/compiler-sfc'
cause Missing the appropriate Vue compiler package for your Vue version.fixFor Vue 2: `npm install vue-template-compiler`. For Vue 3: `npm install @vue/compiler-sfc`. Also, ensure `styleguide.config.js` has `compiler: 'vue-styleguidist/lib/loaders/vue-standalone-compiler'` for Vue 3. -
Webpack config: 'vue-loader' is not found (or similar 'Module not found' for vue-loader)
cause The `vue-loader` package is not installed or the webpack rule for `.vue` files is incorrect or missing.fixInstall `vue-loader`: `npm install vue-loader`. Then, ensure your `styleguide.config.js` `webpackConfig.module.rules` includes a rule like `{ test: /\.vue$/, loader: 'vue-loader' }`. -
Module not found: Error: Can't resolve 'vue' in '...'
cause Webpack is unable to correctly resolve the 'vue' package, often due to an incorrect or missing alias, especially in Vue 3 setups where 'vue' might point to a runtime-only build.fixAdd a `resolve.alias` entry in your `styleguide.config.js` `webpackConfig`: `alias: { 'vue': path.resolve(__dirname, 'node_modules/vue') }`. -
[Vue Styleguidist] Error: No components found. Make sure you have specified the components option correctly.
cause The `components` glob pattern in `styleguide.config.js` does not match any existing `.vue` files in your project.fixVerify the `components` array/string in `styleguide.config.js` (e.g., `components: 'src/components/**/*.vue'`) precisely matches the actual file paths of your Vue components.
Warnings
- gotcha Vue Styleguidist requires specific peer dependencies (`vue`, `vue-loader`, `webpack`, `@vue/compiler-sfc` for Vue 3, or `vue-template-compiler` for Vue 2) to be installed and correctly configured. Incompatibility or missing packages are a common source of errors.
- breaking Migrating from Vue 2 to Vue 3 often requires significant changes to the `styleguide.config.js` file, especially regarding the Vue compiler and `vue-loader` setup, and potentially `resolve.alias` for 'vue'.
- gotcha The documentation examples and Node API predominantly use CommonJS (`require`). While modern projects favor ESM, sticking to CommonJS for configuration files (`styleguide.config.js`) is safer to avoid tooling conflicts.
- gotcha Webpack configuration within `styleguide.config.js` is crucial and can be complex. Incorrect `module.rules`, `resolve.alias`, or missing loaders can prevent components from being parsed or rendered correctly.
Install
-
npm install vue-styleguidist -
yarn add vue-styleguidist -
pnpm add vue-styleguidist
Imports
- styleguidist
import styleguidist from 'vue-styleguidist';
const styleguidist = require('vue-styleguidist')(config); - Configuration
import { Configuration } from 'vue-styleguidist';import type { Configuration } from 'vue-styleguidist'; - styleguide.config.js export
export default { /* ...config */ };module.exports = { /* ...config */ };
Quickstart
npm install --save-dev vue-styleguidist vue@^3 @vue/compiler-sfc webpack@^5 vue-loader@^17 style-loader css-loader babel-loader @babel/core @babel/preset-env
# Create styleguide.config.js
// styleguide.config.js
const path = require('path');
module.exports = {
title: 'My Vue Component Style Guide',
// Adjust components glob to match your project structure
components: 'src/components/**/*.vue',
defaultExample: true,
// Required for Vue 3 setup, or if you have specific compilation needs
compiler: 'vue-styleguidist/lib/loaders/vue-standalone-compiler',
webpackConfig: {
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
},
{
test: /\.(js|ts)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
],
},
resolve: {
alias: {
// Essential for Vue 3 to resolve the correct Vue instance
'vue': path.resolve(__dirname, 'node_modules/vue'),
},
extensions: ['.vue', '.js', '.ts', '.json'],
},
},
styleguideDir: 'dist-styleguide',
};
# Create src/components/MyButton.vue
// src/components/MyButton.vue
<template>
<button :style="{ backgroundColor: color }" @click="onClick">
{{ label }}
</button>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent({
name: 'MyButton',
props: {
label: {
type: String,
required: true,
},
color: {
type: String,
default: 'blue',
},
},
emits: ['click'],
setup(props, { emit }) {
const onClick = () => {
console.log(`Button '${props.label}' clicked!`);
emit('click');
};
return {
onClick,
};
},
});
</script>
<style scoped>
button {
padding: 10px 20px;
border: none;
border-radius: 4px;
color: white;
cursor: pointer;
}
</style>
<docs>
This is a simple button component with customizable label and color.
```vue
<MyButton label="Click Me!" />
<MyButton label="Submit" color="#4CAF50" />
<MyButton label="Danger" color="#f44336" @click="alert('Danger button clicked!')" />
```
</docs>
# Run Styleguidist
npx styleguidist server
# or to build a static style guide
npx styleguidist build