7

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?

John Smith
  • 3,863
  • 3
  • 24
  • 42

3 Answers3

4

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.

FR073N
  • 2,011
  • 4
  • 29
  • 42
1

The @FR073N solution is good, but since the lasts versions, this throw an error.

One line was missing to fully override correctly the webpack config.

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: {
        ...config.resolve, // <= HERE
        alias: {
          ...config.resolve.alias,
          '~': path.resolve(__dirname, '../src/'),
        },
      },
    };
  },
};
-2

I've successfully used storybook-preset-craco with @storybook@6.3.5 and react-scripts@4.0.3 and @craco/craco@6.2.0 in a new CRA TypeScript project.

Nick Ribal
  • 1,959
  • 19
  • 26
  • This library is completely broken, it doesn't work. It can only work when `.storybook` is inside `/src`, but storybook only works when `.storybook` is in the root directory. – Chris Vilches Aug 27 '22 at 17:59