Storybook with craco - call a different verson of react-scripts
Asked Answered
A

3

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?

Arrogance answered 13/4, 2020 at 17:55 Comment(1)
Did you find a solution to your problem ?Charpoy
C
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.

Charpoy answered 17/5, 2020 at 13:29 Comment(0)
N
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/'),
        },
      },
    };
  },
};
Nogging answered 4/1, 2022 at 14:52 Comment(0)
U
-2

I've successfully used storybook-preset-craco with @[email protected] and [email protected] and @craco/[email protected] in a new CRA TypeScript project.

Unaccustomed answered 24/7, 2021 at 23:47 Comment(1)
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.Grossularite

© 2022 - 2024 — McMap. All rights reserved.