• Stars
    star
    884
  • Rank 49,679 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 3 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A builder plugin to run and build Storybooks with Vite

Storybook builder for Vite

Build your stories with vite for fast startup times and near-instant HMR.

Note: This Repository is for Storybook 6.4 and 6.5. In Storybook 7, the Vite builder was brought into the main Storybook monorepo (https://github.com/storybookjs/storybook). See https://storybook.js.org/blog/first-class-vite-support-in-storybook/ for details.

Table of Contents

Installation

Requirements:

npm install @storybook/builder-vite --save-dev

or

yarn add --dev @storybook/builder-vite

or

pnpm add --save-dev @storybook/builder-vite

Note: when using pnpm, you may need to enable shamefully-hoist, until #55 can be fixed.

Usage

In your main.js configuration file, set core: { builder: "@storybook/builder-vite" }.

For autoreload of react stories to work, they need to have a .stories.tsx or .stories.jsx file suffix. See also #53

The builder supports both development mode in Storybook, and building a static production version.

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 sb init --builder @storybook/builder-vite && npm run storybook

Migration from webpack / CRA

  1. Install vite and @storybook/builder-vite
  2. Remove any explicit project dependencies on webpack, react-scripts, and any other webpack plugins or loaders.
  3. If you were previously using @storybook/manager-webpack5, you'll need to remove it, since currently the vite builder only works with manager-webpack4, which is the default and does not need to be installed manually. Also remove @storybook/builder-webpack5 or @storybook/builder-webpack4 if they are installed.
  4. Set core: { builder: "@storybook/builder-vite" } in your .storybook/main.js file.
  5. Remove storybook webpack cache (rm -rf node_modules/.cache)
  6. Update your /public/index.html file for vite (be sure there are no %PUBLIC_URL% inside it, which is a CRA variable)
  7. 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.
  8. 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>
  1. 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 not read your vite.config.js file by default.

In .storybook/main.js (or whatever your Storybook config file is named) you can override the Vite config:

// use `mergeConfig` to recursively merge Vite options
const { mergeConfig } = require('vite');

module.exports = {
  async viteFinal(config, { configType }) {
    // return the customized config
    return mergeConfig(config, {
      // customize the Vite config here
      resolve: {
        alias: { foo: 'bar' },
      },
    });
  },
  // ... other options here
};

The viteFinal function will give you config which is 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.

Svelte Customization

When using this builder with Svelte, your svelte.config.js file will be used automatically.

If you need to make overrides for Storybook, your .storybook/main.js (or equivalent) can contain a svelteOptions object to pass custom options to vite-plugin-svelte:

const preprocess = require('svelte-preprocess');

module.exports = {
  svelteOptions: {
    preprocess: preprocess({
      typescript: true,
      postcss: true,
      sourceMap: true,
    }),
  },
};

TypeScript

Configure your .storybook/main.ts to use TypeScript:

import type { StorybookViteConfig } from '@storybook/builder-vite';

const config: StorybookViteConfig = {
  // other storybook options...,
  async viteFinal(config, options) {
    // modify and return config
  },
};

export default config;

Or alternatively, you can use named exports:

import type { ViteFinal } from '@storybook/builder-vite';

export const viteFinal: ViteFinal = async (config, options) => {
  // modify and return 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 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:

module.exports = {
  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.

Migration from storybook-builder-vite

This project has moved from storybook-builder-vite to @storybook/builder-vite as part of a larger effort to improve Vite support in Storybook. To automatically migrate your existing project, you can run

npx sb@next automigrate

To manually migrate:

  1. Remove storybook-builder-vite from your package.json dependencies
  2. Install @storybook/builder-vite
  3. Update your core.builder setting in .storybook/main.js to @storybook/builder-vite.

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.

#11

Have a look at the GitHub issues 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.

About this codebase

The code is a monorepo with the core @storybook/builder-vite package, and examples (like examples/react) to test the builder implementation.

Similar to the main storybook monorepo, you need yarn to develop this builder, because the project is organized as yarn workspaces. This lets you write new code in the core builder package, and instantly use them from the example packages.

More Repositories

1

storybook

Storybook is a frontend workshop for building UI components and pages in isolation. Made for UI development, testing, and documentation.
TypeScript
82,383
star
2

design-system

🗃 Storybook Design System
TypeScript
1,862
star
3

react-native

📓 Storybook for React Native!
TypeScript
942
star
4

addon-designs

A Storybook addon that embeds Figma, websites, or images in the addon panel.
TypeScript
851
star
5

react-inspector

🔍 Power of Browser DevTools inspectors right inside your React app
TypeScript
740
star
6

testing-react

Testing utilities that allow you to reuse your Storybook stories in your React unit tests!
TypeScript
589
star
7

presets

🧩 Presets for Storybook
TypeScript
424
star
8

marksy

📑 A markdown to custom VDOM components library
JavaScript
354
star
9

vue-cli-plugin-storybook

Vue CLI plugin for Storybook
JavaScript
280
star
10

addon-jsx

This Storybook addon show you the JSX / template of the story
TypeScript
237
star
11

eslint-plugin-storybook

🎗Official ESLint plugin for Storybook
TypeScript
228
star
12

frontpage

🌐 The website for storybook.js.org
TypeScript
209
star
13

test-runner

🚕 Turn stories into executable tests
TypeScript
202
star
14

storybook-addon-console

storybook-addon. Redirects console output into action logger panel
TypeScript
198
star
15

native

📱 Storybook for Native: iOS, Android, Flutter
TypeScript
176
star
16

babel-plugin-react-docgen

📝 Babel plugin to add react-docgen info into your code.
JavaScript
162
star
17

telejson

🛰 JSON parse & stringify with support for cyclic objects, functions, dates, regex, infinity, undefined, null, NaN, Classes, Instances
TypeScript
158
star
18

vs-code-plugin

Aesop: a VSCode Extension to stage Storybook stories inside your IDE.
TypeScript
130
star
19

addon-kit

Everything you need to build a Storybook addon
TypeScript
122
star
20

brand

🎨 Materials for your articles and talks about storybook
89
star
21

addon-svelte-csf

[Incubation] CSF using Svelte components.
TypeScript
86
star
22

desktop

💻 Desktop app for all your Storybooks
JavaScript
71
star
23

addon-react-native-web

Build react-native-web projects in Storybook for React
TypeScript
71
star
24

testing-library

Instrumented version of Testing Library for Storybook Interactions
TypeScript
56
star
25

require-context.macro

🖇 A Babel macro needed for some advanced Storybook setups. Used to mock webpack's context function in test environments.
JavaScript
48
star
26

addon-styling

A base addon for configuring popular styling tools
TypeScript
44
star
27

ember-cli-storybook

📒 Ember storybook adapter
JavaScript
36
star
28

solidjs

SolidJS integration for Storybook, maintained by the community
TypeScript
35
star
29

aem

Adobe Experience Manager Storybook app with events, knobs, docs, addons, and more
JavaScript
33
star
30

paths.macro

👣 Babel plugin that returns an object containing paths like __dirname and __filename as static values
JavaScript
32
star
31

shout-out-kit

An image generation API template
JavaScript
30
star
32

linter-config

📐 ESlint config, Prettier config, Remark config for storybook
JavaScript
27
star
33

SBNext

A future SB
JavaScript
26
star
34

sandboxes

MDX
25
star
35

addon-measure

JavaScript
20
star
36

addon-postcss

This Storybook addon can be used to run the PostCSS preprocessor against your stories.
JavaScript
20
star
37

addon-styling-webpack

Successor to @storybook/addon-styling. Configure the styles of your webpack storybook with ease!
TypeScript
18
star
38

jest

Instrumented version of Jest for Storybook Interactions
TypeScript
18
star
39

addon-coverage

TypeScript
18
star
40

nextjs-example

Example app showing Storybook integrated with Next.js (v11) pages
JavaScript
17
star
41

testing-vue3

Testing utilities that allow you to reuse your stories in your unit tests
TypeScript
16
star
42

bench

Storybook benchmark
TypeScript
15
star
43

testing-angular

TypeScript
13
star
44

mdx2-csf

MDX to CSF compiler using MDXv2
TypeScript
13
star
45

addon-knobs

Storybook addon prop editor component
TypeScript
10
star
46

testing-vue

TypeScript
10
star
47

jest-storybook

Test storybook stories automagically using Jest. 🧙🏻‍♀️
JavaScript
9
star
48

addon-google-analytics

Storybook addon for google analytics
JavaScript
9
star
49

expect

Browser-compatible version of Jest's `expect`
JavaScript
7
star
50

storybook-nuxt

Storybook integration for Nuxt Framework - Unleashing the power of Storybook in Nuxt/Vue Land!
TypeScript
6
star
51

raycast-extension-sandboxes

A Raycast Extension to quickly create sandboxes online or locally
TypeScript
6
star
52

icons

Library of icons used in apps and marketing sites
TypeScript
6
star
53

vue3-code-examples

it is a repo to show Vuetify implementation with new @storybook/vue3 reactive mode
TypeScript
6
star
54

marko

Storybook framework support for marko
TypeScript
6
star
55

addon-graphql

Storybook addon to display the GraphiQL IDE
TypeScript
6
star
56

addon-onboarding

TypeScript
6
star
57

action

🚢 WIP, storybook github action - build your storybook from github
JavaScript
6
star
58

addon-queryparams

parameter addon for storybook
TypeScript
6
star
59

storybook-day

Storybook day website
TypeScript
5
star
60

addon-ie11

JavaScript
5
star
61

addon-design-assets

Design asset preview for storybook
JavaScript
5
star
62

addon-cssresources

A storybook addon to switch between css resources at runtime for your story
TypeScript
5
star
63

create-svelte-with-args

A small CLI wrapper around the create-svelte package that enables you to replace the interactive prompts with CLI arguments.
JavaScript
5
star
64

addon-events

Add events to your Storybook stories.
TypeScript
4
star
65

governance

⚖️ Storybook governance and community policies
4
star
66

community

🙌 Storybook community resources: monthly meetings, meetups, conferences
4
star
67

mdx1-csf

MDX to CSF compiler using MDXv1
TypeScript
4
star
68

addon-react-native-server

A replacement for @storybook/react-native-server which will enable multiple devices to sync over websockets
TypeScript
4
star
69

docs-mdx

TypeScript
3
star
70

addon-angular-ivy

TypeScript
3
star
71

babel-plugin-named-exports-order

Babel plugin for preserving exports order across transforms
JavaScript
3
star
72

nextjs-server

Embedded Storybook in Next.js
TypeScript
3
star
73

addon-bench

Storybook benchmarking helper
JavaScript
3
star
74

repro-react-cra

Reproduction template for Create React App
JavaScript
3
star
75

react-native-demo

Storybook for React Native monorepo
2
star
76

ember

Storybook for Ember
TypeScript
2
star
77

docs2-prototype

TypeScript
2
star
78

components-marketing

TypeScript
2
star
79

repro-templates

WIP
JavaScript
2
star
80

eslint-config-storybook

🔗 wrapper around our @storybook/linter-config package
JavaScript
2
star
81

.github

Repo community health files
1
star
82

regression-test-other

1
star
83

e2e-testing-starter

JavaScript
1
star
84

vue3-autogen-controls

TypeScript
1
star
85

stencil-builder-test

TypeScript
1
star
86

auto-config

TypeScript
1
star
87

external-sandboxes

MDX
1
star
88

create-webpack5-react

JavaScript
1
star
89

playwright-ct

Playwright component testing against a Storybook instance
TypeScript
1
star
90

regression-test-preact

1
star
91

addon-webpack5-compiler-babel

Adds babel as a Webpack5 compiler to Storybook
TypeScript
1
star
92

web

TypeScript
1
star