Preact-cli: How to use Storybook with preact-cli?

Created on 4 Dec 2017  路  8Comments  路  Source: preactjs/preact-cli

I wanna use storybook with preact-cli, however, I can't find a way to do this.

need more info question

Most helpful comment

I used to have Storybook set up with preact-cli. Just tell storybook to use cra webpack setup. add babel plugin to transform react => preact-compat

All 8 comments

Is it possible to provide a custom webpack configuration to storybook?

I used to have Storybook set up with preact-cli. Just tell storybook to use cra webpack setup. add babel plugin to transform react => preact-compat

we should totally add this to the recipes section of the wiki

I would also like to know how to do this, will figure it out and post back. Not an expert in babel :+1:.

The recipe:
Just add this webpack.config.js in your Storybook config directory (.storybook by default).

const defaults = require('@storybook/react/dist/server/config/defaults/webpack.config');
const webpack  = require('webpack');

module.exports = (base, env) => {
  const config = defaults(base, env);
  return Object.assign({}, config, {
    resolve: Object.assign({}, config.resolve, {
      alias: Object.assign({}, (config.resolve || {}).alias, {
        react: 'preact-compat',
        'react-dom': 'preact-compat'
      })
    }),
    plugins: [
      new webpack.ProvidePlugin({
        Component: ['preact', 'Component'],
        React: ['preact-compat']
      })
    ].concat(config.plugins)
  })
};

馃槂

I used @russiann as a starting point, eventually I landed on this:

webpack.config.js

const webpack  = require('webpack');
const path = require("path");

module.exports = (baseConfig, env, defaultConfig) => {
  // we are extending the base alias config here, adding preact as an alias
  defaultConfig.resolve.alias = {
    ...defaultConfig.resolve.alias,
    'react': 'preact-compat',
    'react-dom': 'preact-compat'
  };

  // adding new plugins to the default config.
  defaultConfig.plugins.push(
    new webpack.ProvidePlugin({
      Component: ['preact', 'Component'],
      React: ['preact-compat']
    })
  );

  return defaultConfig;
};

This uses the recommended way to extend the default config

I wrote a blog post about this, for those interested.

https://dev.to/nickytonline/setting-up-storybook-for-preact-p5a

Thanks for this. Will be adding to the Wiki pages.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hardcoar picture hardcoar  路  3Comments

higimo picture higimo  路  3Comments

AlStar01 picture AlStar01  路  3Comments

oren picture oren  路  3Comments

jpoo90 picture jpoo90  路  4Comments