Writing Presets

Storybook presets are grouped collections of babel, webpack, and addons configurations that support specific use cases in Storybook, such as typescript or MDX support.

This doc covers the presets API and how to use the presets mechanism for advanced configuration.

Presets API

A preset is a set of hooks that is called by Storybook on initialization and can override configurations for babel, webpack, addons, and entries.

Each configuration has a similar signature, accepting a base configuration object and options, as in this webpack example:

  1. export async function webpack(baseConfig, options) {
  2. // Modify or replace config. Mutating the original reference object can cause unexpected bugs,
  3. // so in this example we replace.
  4. const { module = {} } = baseConfig;
  5. return {
  6. ...baseConfig,
  7. module: {
  8. ...module,
  9. rules: [
  10. ...(module.rules || []),
  11. {
  12. /* some new loader */
  13. },
  14. ],
  15. },
  16. };
  17. }

Babel

The babel functions babel, babelDefault, and managerBabel all configure babel in different ways.

All functions take a Babel configuration object as their argument and can modify it or return a new object.

For example, Storybook’s Vue support uses presets internally and here’s how it configures babel:

  1. export function babelDefault(config) {
  2. return {
  3. ...config,
  4. presets: [...config.presets, require.resolve('babel-preset-vue')],
  5. };
  6. }
  • babel is applied to the preview config, after it has been initialized by storybook
  • babelDefault is applied to the preview config before any user presets have been applied
  • managerBabel is applied to the manager.

Webpack

The webpack functions webpack, webpackFinal, and managerWebpack configure webpack.

All functions take a webpack4 configuration object.

For example, here is how Storybook automatically adopts create-react-app’s configuration if it’s installed, where applyCRAWebpackConfig is a set of smart heuristics for modifying the input config.

  1. export function webpackFinal(config, { configDir }) {
  2. if (!isReactScriptsInstalled()) {
  3. logger.info('=> Using base config because react-scripts is not installed.');
  4. return config;
  5. }
  6. logger.info('=> Loading create-react-app config.');
  7. return applyCRAWebpackConfig(config, configDir);
  8. }
  • webpack is applied to the preview config after it has been initialized by storybook
  • webpackFinal is applied to the preview config after all user presets have been applied
  • webpackManager is applied to the manager config

Addons

The addon config addons allows you to add addons to Storybook from within a preset. For addons that require custom webpack/babel configuration, it is easier to simply install the preset, and it will take care of everything.

For example, the Storysource preset contains the following code:

  1. export function addons(entry = []) {
  2. return [...entry, require.resolve('@storybook/addon-storysource/register')];
  3. }

This is equivalent to registering the addon manually in addons.js:

  1. import '@storybook/addon-storysource/register';

Entries

Entries are the place to register entry points for the preview. For example it could be used to make a basic configure-storybook preset that loads all the *.stories.js files into SB, instead of forcing people to copy-paste the same thing everywhere.

Advanced Configuration

The presets API is also more powerful than the standard configuration options available in Storybook, so it’s also possible to use presets for more advanced configuration without actually publishing a preset yourself.

For example, some users want to configure the webpack for Storybook’s UI and addons (issue), but this is not possible using standard webpack configuration (it used to be possible before SB4.1). However, you can achieve this with a private preset.

First, create a file my-preset.js in your storybook folder:

  1. export async function managerWebpack(config, options) {
  2. // update config here
  3. return config;
  4. }
  5. export async function managerBabel(config, options) {
  6. // update config here
  7. return config;
  8. }
  9. export async function webpack(config, options) {
  10. return config;
  11. }
  12. export async function babel(config, options) {
  13. return config;
  14. }

Then, load that preset in your presets.js file:

  1. module.exports = [path.resolve('./my-preset')];