Docs
Storybook Docs

Storybook for Svelte & Webpack

Storybook for Svelte & Webpack is a framework that makes it easy to develop and test UI components in isolation for applications using Svelte built with Webpack.

Requirements

  • Svelte ≥ 4.0
  • Webpack ≥ 5.0
  • Storybook ≥ 8.0

Getting started

In a project without Storybook

Follow the prompts after running this command in your Svelte project's root directory:

npm create storybook@latest

More on getting started with Storybook.

In a project with Storybook

This framework is designed to work with Storybook 7+. If you’re not already using v7, upgrade with this command:

npx storybook@latest upgrade

Automatic migration

When running the upgrade command above, you should get a prompt asking you to migrate to @storybook/svelte-webpack5, which should handle everything for you. In case that auto-migration does not work for your project, refer to the manual migration below.

Manual migration

First, install the framework:

npm install --save-dev @storybook/svelte-webpack5

Next, install and register your appropriate compiler addon, depending on whether you're using SWC (recommended) or Babel:

npx storybook@latest add @storybook/addon-webpack5-compiler-swc

or

npx storybook@latest add @storybook/addon-webpack5-compiler-babel

More details can be found in the Webpack builder docs.

Finally, update your .storybook/main.js|ts to change the framework property:

.storybook/main.ts
import { StorybookConfig } from '@storybook/svelte-webpack5';
 
const config: StorybookConfig = {
  // ...
  framework: '@storybook/svelte-webpack5', // 👈 Add this
};
 
export default config;

API

Options

You can pass an options object for additional configuration if needed:

.storybook/main.ts
import { StorybookConfig } from '@storybook/svelte-webpack5';
 
const config: StorybookConfig = {
  // ...
  framework: {
    name: '@storybook/svelte-webpack5',
    options: {
      // ...
    },
  },
};
 
export default config;

The available options are:

builder

Type: Record<string, any>

Configure options for the framework's builder. For this framework, available options can be found in the Webpack builder docs.

Troubleshooting

Svelte CSF addon compatibility

In version 5 of the Svelte CSF addon, support for Webpack was removed to align with Svelte's ecosystem. If you're using this framework and you want to write stories using Svelte's native templating syntax with the Svelte CSF addon, we encourage you to downgrade to version 4 of the addon.