Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I build my React Native Storybook to web?

I am running React Native Storybook which runs Storybook in the Native emulator.

In addition to the how React Native Storybook works currently, I would also like to build an instance of it for web as a reference companion to our app.

I am using "@storybook/react-native": "5.3.14". My stories are located at ./storybook.

like image 795
Elise Chant Avatar asked Oct 16 '22 05:10

Elise Chant


1 Answers

Install react-native-web, @storybook/react and babel-plugin-react-native-web from npm in your project root.

Add a new configuration directory for Storybook, say ./.storybook-website. Inside this directory, add main.js. This creation would otherwise be done by the Storybook installation wizard.

my-app
├── .storybook-website
│   └── main.js
└── // .... rest of your app

Add the following content to main.js:

module.exports = {
  stories: ['../storybook/stories/index.js'],
  webpackFinal: async (config) => {
    config.resolve.alias = {
      ...(config.resolve.alias || {}),
      // Transform all direct `react-native` imports to `react-native-web`
      'react-native$': 'react-native-web',
      // make sure we're rendering output using **web** Storybook not react-native
      '@storybook/react-native': '@storybook/react',
      // plugin-level react-native-web extensions
      'react-native-svg': 'react-native-svg/lib/commonjs/ReactNativeSVG.web',
      // ... 
    };
    // mutate babel-loader
    config.module.rules[0].use[0].options.plugins.push(['react-native-web', { commonjs: true }]);
    // console.dir(config, { depth: null });
    return config;
  },
};

Update the stories path in main.js to the location of your existing root story.

Finally add run scripts to your package.json:

"storybook:web": "start-storybook -p 6006 --config-dir ./.storybook-website",
"storybook-build:web": "build-storybook --config-dir ./.storybook-website --output-dir dist-storybook-website --quiet"

Presto! Run using yarn storybook:web. This will run storybook dev server, opening a browser showing what you usually would see in the device emulator.

like image 158
Elise Chant Avatar answered Oct 24 '22 03:10

Elise Chant