Custom Webpack Config
You can customize Storybook’s webpack setup by providing a webpackFinal
field in main.js
file.
The value should be an async function that receives a webpack config and eventually returns a webpack config.
Storybook has its own Webpack setup and a dev server. The webpack config is configurable, and the default can depend on which framework you’re using and whether you’ve used a generator like Create React App or Angular CLI etc.
We’re trying to make storybook more zero-config over time, help to hook into the config of generators is very welcome.
This is what the config for storybook looks like when using CRA in dev-mode:
Debug the default webpack config
- Create a
.storybook/main.js
file. -
Edit its contents:
-
Then run storybook:
The console should log the entire config, for you to inspect.
Examples
The value should export a function
, it’s first argument, is the config that storybook would use, if you were to not customize it. The second argument is an options object from storybook, this will have information about where config came from, whether we’re in production of development mode etc.
For example, here’s a .storybook/main.js
to add Sass support:
Storybook uses the config returned from the above function to render your components in Storybook’s “preview” iframe. Note that Storybook has a completely separate webpack config for its own UI (also referred to as the “manager”), so the customizations you make only applies to the rendering of your stories, i.e. you can completely replace config.module.rules
if you want.
Nevertheless, edit config
with care. Make sure to preserve the following config options:
- entry
- output
Furthermore, 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 Issue #6020 for examples:
Finally, if your custom webpack config uses a loader that does not explicitly include specific file extensions via the test
property, it is necessary to exclude
the .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 make sure that it resolves to your project root.
Using Your Existing Config
If you have an existing webpack config for your project and want to reuse this app’s configuration, you can import your main webpack config into Storybook’s .storybook/main.js
and merge the 2 configs:
Example
replacing the loaders from storybook with the loaders from your app’s webpack.config.js