Search code examples
create-react-appstorybookcraco

Storybook with craco - call a different verson of react-scripts


Storybook currently calls react-scripts. However, I've got some parts of the CRA config overriden with craco. It means my application is invoked with craco ..., rather than react-scripts ....

Is there a clean solution to have Storybook call craco instead?


Solution

  • The solution I came up with is this :

    .storybook/main.js :

    const path = require('path');
    
    module.exports = {
      stories: ['../src/**/*.stories.js'],
      addons: [
        '@storybook/preset-create-react-app',
        '@storybook/addon-actions',
        '@storybook/addon-links',
        '@storybook/addon-viewport/register',
        '@storybook/addon-knobs/register',
      ],
      webpackFinal(config, { configType }) {
        return {
          ...config,
          resolve: {
            alias: {
              ...config.resolve.alias,
              '~': path.resolve(__dirname, '../src/'),
            },
          },
        };
      },
    };
    

    I was only using the alias feature in my craco file, so here I override webpack config from storybook and only add the alias parameter. For your case, you'll need to add your own config.