Skip to content
Branch: master
Find file History
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
src fix: use regex for loader tests Jan 17, 2020
.env Add preset-create-react-app Aug 9, 2019
README.md fix: fix README linting errors Jan 17, 2020
package.json @storybook/preset-create-react-app 1.5.2 Jan 17, 2020
tsconfig.json Add support for CRA without overrides Dec 16, 2019

README.md

Create React App preset for Storybook

One-line Create React App configuration for Storybook.

This preset is designed to use alongside @storybook/react.

Basic usage

First, install this preset to your project.

# Yarn
yarn add -D @storybook/preset-create-react-app

# npm
npm install -D @storybook/preset-create-react-app

Once installed, add this preset to the appropriate file:

  • ./.storybook/main.js (for Storybook 5.3.0 and newer)

    module.exports = {
      addons: ['@storybook/preset-create-react-app'],
    };
  • ./.storybook/presets.js (for all Storybook versions)

    module.exports = ['@storybook/preset-create-react-app'];

Advanced usage

Enabling docgen (for Storybook Docs)

You can optionally enable and configure react-docgen-typescript-loader with tsDocgenLoaderOptions.

If set to {}, it will be enabled with the default settings for Create React App. In most cases, this is all the configuration needed.

module.exports = {
  addons: [
    {
      name: '@storybook/preset-create-react-app',
      options: {
        tsDocgenLoaderOptions: {},
      },
    },
    {
      name: '@storybook/addon-docs',
      options: {
        configureJSX: true,
      },
    },
  ],
};

Alternatively, you can pass your own configuration:

const path = require('path');

module.exports = {
  addons: [
    {
      name: '@storybook/preset-create-react-app',
      options: {
        tsDocgenLoaderOptions: {
          tsconfigPath: path.resolve(__dirname, '../tsconfig.json'),
        },
      },
    },
    {
      name: '@storybook/addon-docs',
      options: {
        configureJSX: true,
      },
    },
  ],
};

CRA overrides

This preset uses CRA's Webpack/Babel configurations, so that Storybook's behavior matches your app's behavior.

However, there may be some cases where you'd rather override CRA's default behavior. If that is something you need, you can use the craOverrides object.

Option Default Behaviour Type Description
fileLoaderExcludes ['ejs', 'mdx'] Extends string[] Excludes file types (by extension) from CRA's file-loader configuration. The defaults are required by Storybook.

Here's how you might configure this preset to ignore PDF files so they can be processed by another preset or loader:

module.exports = {
  addons: [
    {
      name: '@storybook/preset-create-react-app',
      options: {
        craOverrides: {
          fileLoaderExcludes: ['pdf'],
        },
      },
    },
  ],
};

Custom react-scripts packages

In most cases, this preset will find your react-scripts package, even if it's a fork of the offical react-scripts.

In the event that it doesn't, you can set the package's name with scriptsPackageName.

module.exports = {
  addons: [
    {
      name: '@storybook/preset-create-react-app',
      options: {
        scriptsPackageName: '@my/react-scripts',
      },
    },
  ],
};

Warning for forks of 'react-scripts'

One of the tasks that this preset does is inject the storybook config directory (the default is .storybook) into the includes key of the webpack babel-loader config that react-scripts (or your fork) provides. This is nice because then any components/code you've defined in your storybook config directory will be run through the babel-loader as well.

The potential gotcha exists if you have tweaked the Conditions of the webpack babel-loader rule in your fork of react-scripts. This preset will make the include condition an array (if not already), and inject the storybook config directory. If you have changed the conditions to utilize an exclude, then BOTH conditions will need to be true (which isn't likely going to work as expected).

The steps to remedy this would be to follow the steps for customizing the webpack config within the storybook side of things. Details for storybook custom webpack config You'll have access to all of the rules in config.module.rules. You'll need to find the offending rule, and customize it how you need it to be to be compatible with your fork.

See Webpack Rule Conditions for more details concerning the conditions.

Resources

You can’t perform that action at this time.