Storybook displays your components in a custom web application built using Webpack. Webpack is a complex tool, but our default configuration is intended to cover most use cases. Addons are also available that extend the configuration for other everyday use cases.
You can customize Storybook's Webpack setup by providing a
webpackFinal field in
The value should be an async function that receives a Webpack config and eventually returns a Webpack config.
By default, Storybook's Webpack configuration will allow you to:
You can import images and other local files and have them built into the Storybook:
You can import
If you want to know the exact details of the Webpack config, the best way is to run either of the following commands:
For development mode:
For production mode:
Starting with Storybook 6.4, code splitting is supported through a configuration flag. Update your Storybook configuration and add the
Storybook builds your project with Webpack 4 by default. If your project uses Webpack 5, you can opt into the Webpack 5 builder by installing the required dependencies (i.e.,
@storybook/manager-webpack5) and update your Storybook configuration as follows:
Once you are using Webpack 5, you can further opt into some features to optimize your build:
Storybook supports Webpack's experimental lazy compilation feature, via the
lazyCompilation builder flag:
This feature applies in development mode, and will mean your Storybook will start up faster, at the cost of slightly slower browsing time when you change stories.
Storybook supports Webpack's filesystem caching feature, via the
fsCache builder flag:
This feature will mean build output is cached between runs of Storybook, speeding up subsequent startup times.
To extend the above configuration, use the
webpackFinal field of
The value should export a
function, which will receive the default config as its first argument. The second argument is an options object from Storybook, and this will have information about where config came from, whether we're in production or development mode, etc.
For example, if you wanted to add Sass support, you can adjust your configuration as such:
Storybook uses the config returned from the above function to render your components in Storybook's "preview" iframe. Note that Storybook has an entirely separate Webpack config for its UI (also referred to as the "manager"), so the customizations you make only apply to the rendering of your stories, i.e., you can completely replace
config.module.rules if you want.
config with care. Make sure to preserve the following config options:
config requires the
HtmlWebpackplugin to generate the preview page, so rather than overwriting
config.plugins you should probably append to it (or overwrite it with care), see the following issue for examples on how to handle this:
Finally, if your custom Webpack config uses a loader that does not explicitly include specific file extensions via the
test property, in that case, it is necessary to
.ejs file extension from that loader.
If you're using a non-standard Storybook config directory, you should put
main.js there instead of
.storybook and update the
include path to ensure it resolves to your project root.
Suppose you have an existing Webpack config for your project and want to reuse this app's configuration. In that case, you can import your main Webpack config into Storybook's
.storybook/main.js and merge both:
The following code snippet shows how you can replace the loaders from Storybook with the ones from your app's
💡 Projects initialized via generators (e.g, Vue CLI) may require that you import their own Webpack config file (i.e.,
/projectRoot/node_modules/@vue/cli-service/webpack.config.js) to use a certain feature with Storybook. For other generators, make sure to check the documentation for instructions.
When working with TypeScript projects, the default Webpack configuration may fail to resolve module aliases defined in your
tsconfig file. To work around this issue you may use
tsconfig-paths-webpack-plugin while extending Storybook's Webpack config like: