What is @storybook/builder-vite?
The @storybook/builder-vite package is a Storybook builder that integrates Vite as the build tool for Storybook. It allows developers to leverage Vite's fast build times and features within their Storybook environment. This package is particularly useful for projects that are already using Vite and want to maintain consistency in their build tools.
What are @storybook/builder-vite's main functionalities?
Custom Vite Configuration
Allows customization of the Vite configuration used by Storybook. Developers can add aliases, plugins, and modify other settings to tailor the build process.
module.exports = {
stories: ['../stories/**/*.stories.@(js|jsx|ts|tsx|mdx)'],
addons: ['@storybook/addon-links', '@storybook/addon-essentials'],
core: {
builder: '@storybook/builder-vite'
},
async viteFinal(config, { configType }) {
// customize the Vite config here
config.resolve.alias = {
...config.resolve.alias,
'@components': '/path/to/components'
};
// return the customized config
return config;
}
};
Fast Build Times
By using Vite as the build tool, Storybook can take advantage of Vite's fast build times, leading to quicker startup and refresh times during development.
// There is no specific code sample for this feature as it is an inherent benefit of using Vite with Storybook.
Support for Vite Plugins
Enables the use of Vite plugins within the Storybook build process, allowing developers to extend functionality with Vite's rich plugin ecosystem.
module.exports = {
core: {
builder: '@storybook/builder-vite'
},
async viteFinal(config) {
config.plugins.push(myVitePlugin());
return config;
}
};
Other packages similar to @storybook/builder-vite
@storybook/builder-webpack5
This package is a Storybook builder that uses Webpack 5 as the build tool. It is similar to @storybook/builder-vite in that it integrates a modern build tool into Storybook, but it uses Webpack instead of Vite. Webpack is more established but generally has slower build times compared to Vite.
@storybook/builder-webpack4
Similar to the webpack5 builder, this package integrates Webpack 4 into Storybook. It is for projects that are still using Webpack 4 and have not yet upgraded to Webpack 5. It offers a different set of features and plugin compatibility compared to Vite.
@storybook/addon-storyshots
While not a builder, this addon provides snapshot testing for Storybook stories. It complements builders like @storybook/builder-vite by adding testing capabilities to the development workflow.
Storybook builder for Vite
Build your stories with vite for fast startup times and near-instant HMR.
Table of Contents
Installation
Requirements:
- Vite 3.0 or newer (4.X recommended)
When installing Storybook, use the --builder=vite
flag if you do not have a vite.config
file at your project root (if you do, the vite builder is chosen automatically).
Usage
The builder supports both development mode in Storybook and building a static production version.
Your vite.config
file will be used by Storybook. If you need to customize the vite config for Storybook, you have two choices:
- Set values in your
vite.config
conditionally, based on an environment variable, for example. - Add a
viteFinal
config to your .storybook/main.js
file. See Customize Vite config for details.
Getting started with Vite and Storybook (on a new project)
See https://vitejs.dev/guide/#scaffolding-your-first-vite-project,
npm create vite@latest # follow the prompts
npx storybook@latest init --builder vite && npm run storybook
Migration from webpack / CRA
- Install
vite
and @storybook/builder-vite
- Remove any explicit project dependencies on
webpack
, react-scripts
, and any other webpack plugins or loaders. - If you were previously using
@storybook/manager-webpack5
, you can remove it. Also remove @storybook/builder-webpack5
or @storybook/builder-webpack4
if they are installed. - Choose a vite-based Storybook "framework" to set in the
framework
option of your .storybook/main.js
file. - Remove storybook webpack cache (
rm -rf node_modules/.cache
) - Update your
/public/index.html
file for vite (be sure there are no %PUBLIC_URL%
inside it, which is a CRA variable) - Be sure that any files containing JSX syntax use a
.jsx
or .tsx
file extension, which vite requires. This includes .storybook/preview.jsx
if it contains JSX syntax. - If you are using
@storybook/addon-interactions
, for now you'll need to add a workaround for jest-mock relying on the node global
variable by creating a .storybook/preview-head.html
file containing the following:
<script>
window.global = window;
</script>
- Start up your storybook using the same
yarn storybook
or npm run storybook
commands you are used to.
For other details about the differences between vite and webpack projects, be sure to read through the vite documentation.
Customize Vite config
The builder will read your vite.config.js
file, though it may change some of the options in order to work correctly.
It looks for the Vite config in the CWD. If your config is located elsewhere, specify the path using the viteConfigPath
builder option:
const config = {
framework: {
name: '@storybook/react-vite',
options: {
builder: {
viteConfigPath: '.storybook/customViteConfig.js',
},
},
},
};
export default config;
You can also override the merged Vite config:
import { mergeConfig } from 'vite';
const config = {
async viteFinal(config, { configType }) {
return mergeConfig(config, {
resolve: {
alias: { foo: 'bar' },
},
});
},
};
export default config;
The viteFinal
function will give you config
which is the combination of your project's vite config and the builder's own Vite config.
You can tweak this as you want, for example to set up aliases, add new plugins etc.
The configType
variable will be either "DEVELOPMENT"
or "PRODUCTION"
.
The function should return the updated Vite configuration.
TypeScript
Configure your .storybook/main.ts
to use TypeScript:
import type { StorybookConfig } from '@storybook/react-vite';
const config: StorybookConfig = {
async viteFinal(config, options) {
},
};
export default config;
See Customize Vite config for details about using viteFinal
.
React Docgen
Docgen is used in Storybook to populate the props table in docs view, the controls panel, and for several other addons. Docgen is supported in Svelte, Vue, and React, and there are two docgen options when using react, react-docgen
and react-docgen-typescript
. You can learn more about the pros/cons of each in this gist. By default, if we find a typescript
dependency in your package.json
file, we will assume you're using typescript and will choose react-docgen-typescript
. You can change this by setting the typescript.reactDocgen
option in your .storybook/main.js
file:
export default {
typescript: {
reactDocgen: 'react-docgen`
}
}
If you're using TypeScript, we encourage you to experiment and see which option works better for your project.
Note about working directory
The builder will by default enable Vite's server.fs.strict
option, for increased security. The default project root
is set to the parent directory of the
storybook configuration directory. This can be overridden in viteFinal.
Known issues
- HMR: saving a story file does not hot-module-reload, a full reload happens instead. HMR works correctly when saving component files.
Contributing
The Vite builder cannot build itself.
Are you willing to contribute? We are especially looking for Vue and Svelte experts, as the current maintainers are react users.
Have a look at the GitHub issues with the vite
label for known bugs. If you find any new bugs,
feel free to create an issue or send a pull request!
Please read the How to contribute guide.