Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom webpack config when using react-native server #92

Closed
amalfra opened this issue Aug 9, 2020 · 22 comments
Closed

Custom webpack config when using react-native server #92

amalfra opened this issue Aug 9, 2020 · 22 comments

Comments

@amalfra
Copy link

amalfra commented Aug 9, 2020

Describe the bug
I have lerna monorepo with storybook and react native project in different packages. React native is in mobile project's node_modules. the storybook package has storybook react native and @storybook/addon-ondevice-actions. Is there any way to add custom webpack alias config for allowing the react-native from mobile project package to be loaded by @storybook/addon-ondevice-actions which is in different package?

To Reproduce
Steps to reproduce the behavior:

  1. Have react native project and storybook inside different packages in lerna monorepo
  2. add @storybook/addon-ondevice-actions/register in storybook for react native config
  3. try to run storybook server

Expected behavior

Custom webpack config can be applied like react storybook

@dannyhw
Copy link
Member

dannyhw commented Aug 11, 2020

I'm not sure I understand, is there some issue with storybook here or is this a question about configuring lerna? Also you didn't state what happens when you try to run the server, does it fail is there an error?

@amalfra
Copy link
Author

amalfra commented Aug 11, 2020

In react storybook I can give aliases/module resolve config like

  webpackFinal: config => ({
    ...config,
    resolve: {
      ...config.resolve,
      ...baseWebpackConfig.resolve,
    },
    module: {
      ...config.module,
      rules: [
        ...config.module.rules,
        ...baseWebpackConfig.module.rules.slice(1),
      ],
    },
  }),

But same can't be done in the case of react native storybook. I have folder structure like this:

  • mobile: has react-native code and the dependencies
  • storybook: has storybook code and some storybook related files
    The storybook package addon-ondevice looks for react-native which it's not able to find as it's installed inside mobile package. So I would like to apply an additional modules folder pointing to mobile folder. But the react native server does not provide me with an option to modify webpack config.

And regarding you question about running server, it will work fine if I don't enable addon-ondevice. In case I enable it then the plugin won't be able to find react-native package and it will fail to start.

@dannyhw
Copy link
Member

dannyhw commented Aug 11, 2020

Well its possible to have an alias but this isn't a storybook feature, in the react version you are using webpack to do that. In react native by default bundler is metro and webpack is not present. In order to achieve the same results with RN you can use the babel plugin for module resolution, try this one.

Or you can try using some metro config like this and just change the extra node modules to be the directory you want

@amalfra
Copy link
Author

amalfra commented Aug 11, 2020

But the error is not in mobile app. it's inside storybook-react-native server. So I don't think it's related to react native metro bundler. Below are the logs that I get when the server is started.

amal@test:storybook$ npm run start:mobile

> @sly-react/[email protected] start:mobile /Users/amal/test/react/packages/storybook
> node_modules/@storybook/react-native-server/bin/index.js start-storybook -p 9002 -c src/mobile

info => Loading presets
info => Loading custom manager config.
✖ 「wdm」: Hash: dddf23e6c4ca082a88b1
Version: webpack 4.44.1
Time: 7832ms
Built at: 08/11/2020 11:11:28 PM
                                      Asset      Size        Chunks                                Chunk Names
                                 index.html  1.34 KiB                [emitted]                     
        main.43839ad8c99a9f21f73e.bundle.js  2.29 KiB          main  [emitted] [immutable]         main
runtime~main.99691078705b39185f99.bundle.js  6.12 KiB  runtime~main  [emitted] [immutable]         runtime~main
vendors~main.8a0cc25dae04efc7f342.bundle.js   7.1 MiB  vendors~main  [emitted] [immutable]  [big]  vendors~main
Entrypoint main [big] = runtime~main.99691078705b39185f99.bundle.js vendors~main.8a0cc25dae04efc7f342.bundle.js main.43839ad8c99a9f21f73e.bundle.js
[0] multi ./node_modules/@storybook/core/dist/server/common/polyfills.js ./node_modules/@storybook/react-native-server/dist/client/manager/index.js ./src/mobile/addons.js 52 bytes {main} [built]
[../../node_modules/@storybook/addon-ondevice-actions/dist/index.js] /Users/amal/test/react/node_modules/@storybook/addon-ondevice-actions/dist/index.js 2.22 KiB {vendors~main} [built]
[../../node_modules/@storybook/addon-ondevice-actions/register.js] /Users/amal/test/react/node_modules/@storybook/addon-ondevice-actions/register.js 30 bytes {vendors~main} [built]
[./node_modules/@storybook/addons/dist/public_api.js] 2.23 KiB {vendors~main} [built]
[./node_modules/@storybook/api/dist/index.js] 16.4 KiB {vendors~main} [built]
[./node_modules/@storybook/channel-websocket/dist/index.js] 3.21 KiB {vendors~main} [built]
[./node_modules/@storybook/core/dist/server/common/polyfills.js] 120 bytes {vendors~main} [built]
[./node_modules/@storybook/react-native-server/dist/client/manager/index.js] 475 bytes {vendors~main} [built]
[./node_modules/@storybook/react-native-server/dist/client/manager/provider.js] 5.81 KiB {vendors~main} [built]
[./node_modules/@storybook/ui/dist/index.js] 3.26 KiB {vendors~main} [built]
[./node_modules/airbnb-js-shims/index.js] 40 bytes {vendors~main} [built]
[./node_modules/core-js/features/symbol/index.js] 359 bytes {vendors~main} [built]
[./node_modules/global/window.js] 232 bytes {vendors~main} [built]
[./node_modules/regenerator-runtime/runtime.js] 24 KiB {vendors~main} [built]
[./src/mobile/addons.js] 92 bytes {main} [built]
    + 1718 hidden modules

ERROR in /Users/amal/test/react/node_modules/@storybook/addon-ondevice-actions/dist/components/ActionLogger/index.js
Module not found: Error: Can't resolve 'react-native' in '/Users/amal/test/react/node_modules/@storybook/addon-ondevice-actions/dist/components/ActionLogger'
 @ /Users/amal/test/react/node_modules/@storybook/addon-ondevice-actions/dist/components/ActionLogger/index.js 14:19-42
 @ /Users/amal/test/react/node_modules/@storybook/addon-ondevice-actions/dist/containers/ActionLogger/index.js
 @ /Users/amal/test/react/node_modules/@storybook/addon-ondevice-actions/dist/index.js
 @ /Users/amal/test/react/node_modules/@storybook/addon-ondevice-actions/register.js
 @ ./src/mobile/addons.js
 @ multi ./node_modules/@storybook/core/dist/server/common/polyfills.js ./node_modules/@storybook/react-native-server/dist/client/manager/index.js ./src/mobile/addons.js

ERROR in /Users/amal/test/react/node_modules/@storybook/addon-ondevice-actions/dist/components/ActionLogger/Inspect.js
Module not found: Error: Can't resolve 'react-native' in '/Users/amal/test/react/node_modules/@storybook/addon-ondevice-actions/dist/components/ActionLogger'
 @ /Users/amal/test/react/node_modules/@storybook/addon-ondevice-actions/dist/components/ActionLogger/Inspect.js 54:19-42
 @ /Users/amal/test/react/node_modules/@storybook/addon-ondevice-actions/dist/components/ActionLogger/index.js
 @ /Users/amal/test/react/node_modules/@storybook/addon-ondevice-actions/dist/containers/ActionLogger/index.js
 @ /Users/amal/test/react/node_modules/@storybook/addon-ondevice-actions/dist/index.js
 @ /Users/amal/test/react/node_modules/@storybook/addon-ondevice-actions/register.js
 @ ./src/mobile/addons.js
 @ multi ./node_modules/@storybook/core/dist/server/common/polyfills.js ./node_modules/@storybook/react-native-server/dist/client/manager/index.js ./src/mobile/addons.js
Child HtmlWebpackCompiler:
                          Asset      Size               Chunks  Chunk Names
    __child-HtmlWebpackPlugin_0  6.34 KiB  HtmlWebpackPlugin_0  HtmlWebpackPlugin_0
    Entrypoint HtmlWebpackPlugin_0 = __child-HtmlWebpackPlugin_0
    [./node_modules/html-webpack-plugin/lib/loader.js!./node_modules/@storybook/core/dist/server/templates/index.ejs] 2.14 KiB {HtmlWebpackPlugin_0} [built]

I also have a module resolver config like

    ['module-resolver', {
      alias: {
        'react-native': path.join(__dirname, '..', 'mobile', 'node_modules', 'react-native'),
      },
    }, 'native-module-resolver'],

but don't think that will be applied to this file as it's from node_modules folder.

@dannyhw
Copy link
Member

dannyhw commented Aug 11, 2020

The action addon should probably not be the on device version for the server though, on device is the version that shows on the phone. I'm not sure about the server setup because I don't use it, but maybe try the regular action addon.

@amalfra
Copy link
Author

amalfra commented Aug 12, 2020

// import '@storybook/addon-ondevice-actions/register';
import '@storybook/addon-actions/register';

When I use the normal addons plugins in my addons file as above, the metro bundler will throw this error:

[Wed Aug 12 2020 15:01:34.950]  ERROR    ReferenceError: Can't find variable: document
[Wed Aug 12 2020 15:01:34.950]  ERROR    Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication)
[Wed Aug 12 2020 15:01:35.156]  ERROR    Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication)

@dannyhw
Copy link
Member

dannyhw commented Aug 13, 2020

OK I'm not really sure without having an example project to test things, I might be able to try it out next week. Let me know if you find a solution in the mean time.

@zlanich
Copy link

zlanich commented Sep 1, 2020

I have this issue too, with the following dependencies in a fresh Expo SDK 38 project:

"dependencies": {
    "@react-native-community/async-storage": "~1.11.0",
    "@storybook/addon-actions": "^6.0.21",
    "@storybook/addon-knobs": "^6.0.21",
    "@storybook/addon-ondevice-actions": "^5.3.21",
    "@storybook/addon-ondevice-knobs": "^5.3.21",
    "@storybook/react-native": "^5.3.21",
    "expo": "~38.0.8",
    "expo-status-bar": "^1.0.2",
    "react": "~16.11.0",
    "react-dom": "~16.11.0",
    "react-native": "https://github.com/expo/react-native/archive/sdk-38.0.2.tar.gz",
    "react-native-web": "~0.11.7"

Error: Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplciation)

The given expo snack with different dependencies still works for me, though: https://snack.expo.io/@dannyhw/expo-storybook-example

@dannyhw
Copy link
Member

dannyhw commented Sep 1, 2020

@zlanich Not sure without seeing your app entrypoint but it seems like you might be using app registry in expo. When using expo you don't need to use AppRegistry.registerComponent(appName, () => StorybookUIRoot) instead you just export default StorybookUIRoot.

Also with react native storybook you should only use storybook dependencies with version 5.3.x since we don't support 6.0 yet.

@zlanich
Copy link

zlanich commented Sep 1, 2020

@dannyhw I saw that in the docs and took care of that. Here's my App.tsx:

import React from 'react';
import AsyncStorage from '@react-native-community/async-storage';
import {
  getStorybookUI,
  configure,
  addDecorator,
} from '@storybook/react-native';

import { register as registerKnobs, withKnobs } from '@storybook/addon-ondevice-knobs';
import { register as registerActions } from '@storybook/addon-ondevice-actions';

registerKnobs();
registerActions();
addDecorator(withKnobs);

configure(() => {
  require('./src/stories');
}, module);

const StorybookUIRoot = getStorybookUI({
  asyncStorage: AsyncStorage,
});

export default StorybookUIRoot;

it appears this error pops up immediately, just by having the addons libraries installed at all. Even if I completely comment out all imports and related code within my project, it still throws the error until I uninstall the addon packages. :-/

@dannyhw
Copy link
Member

dannyhw commented Sep 1, 2020

@zlanich in your package json you had 6.0 dependencies, please revert them to 5.3.21 and see if that helps.

 "@storybook/addon-actions": "^6.0.21",
 "@storybook/addon-knobs": "^6.0.21",

should be

 "@storybook/addon-actions": "5.3.21",
 "@storybook/addon-knobs": "5.3.21",

If you are unable to fix this then please open a new issue explaining the specific issue and if possible with a reproduction branch as explained in the contributing docs.

@impactro
Copy link

impactro commented Sep 4, 2020

I have a same problem, storybook not working, shows only loading forever.
even after starting the android connection

steps to reproduce:

Terminal 1>npx react-native init test1
Terminal 1>cd test1
Terminal 1>npx -p @storybook/cli@^5 sb init --type react_native -y
Terminal 1>npm remove @storybook/addons @storybook/addon-actions @storybook/addon-links
Terminal 1>npm install --save-dev @storybook/addons@^5 @storybook/addon-actions@^5 @storybook/addon-links@^5
Terminal 1>npm run android
Terminal 2>npm run storybook

image

@dannyhw
Copy link
Member

dannyhw commented Sep 4, 2020

@impactro this does not relate to the issue explained in the initial issue description. There are other issues more relevant to the issue you are describing.

Anyway you should start the server before the mobile app since it usually works better this way in my experience.

npm run storybook
npm run android

Then in the device try refreshing the device with the shake gesture or ctrl/cmd + R. Usually after refreshing the app once things should start to appear on the server.

If you continue to experience issues then create a reproduction branch as described in the contributors documentation and make an issue with a link to the branch.

@Brendon127
Copy link

I believe this is related to the initial question.

My idea is to create an internal component library for our organisation.
The file structure would look like this:

+-- root
    +-- packages
    |   +-- components
    |    |   +-- Button
    |    |    |    button.stories.jsx
    |    |   +-- OtherComponent
    |    |    |    other.stories.jsx
    |    |   +-- index.js
    |    |   +-- package.json
    |   +-- example
    |    |    ├── android
    |    |    ├── app.json
    |    |    ├── index.js
    |    |    ├── ios
    |    |    ├── metro.config.js
    |    |    ├── package.json
    |    |    ├── react-native.config.js
    |    |    ├── src
+-- package.json
+-- lerna.json
+-- node_modules

The packages directory contains two workspaces, components and example (It could possibly contain more in the future).
The example sub directory is a standard react-native app that would be used to preview the stories defined in the components package.
The reasoning behind separating the two is that we want to publish the components package to an npm registry for easy importing within our projects.
I have tried importing the stories from components in example, but it complains that the modules can't be found.
My question is if there is a way to display the stories defined in components in its sibling workspace, example?
If it can be done, could you point me in the right direction to achieve this or if I'm going about it all wrong and there is a better way to do this. It would be much appreciated.

@dannyhw
Copy link
Member

dannyhw commented Sep 8, 2020

@Brendon127 the issue here is describing a issue with webpack. Metro is the react native bundler and webpack is only used for react native web so it probably doesn't relate to your problem. Also your issue isn't really a storybook issue, it's more of a react-native or metro one. Don't worry about it though :). If you have general questions about storybook usage like this the discord might be a better place for it, I'm active there in the react-native channel.

Anyway you should be able to acheive what you want using lerna and yarn workspaces. You just need to list the components package with the version from the repo in your package.json, this repo uses this approach to be able to import a local versions of the react native storybook packages.

@impactro
Copy link

impactro commented Sep 9, 2020

Hello,
I managed to evolve a bit, but making the navigation work only inside the Android device, but I couldn't via the browser even with the device working

I put all my code in this repository, in case you want to download and evaluate what is wrong
https://github.com/impactro/ReactNativeStorybookExample

@dannyhw
Copy link
Member

dannyhw commented Sep 9, 2020

@impactro are you using expo and a real android device? if so I've made comments in other issues regarding this see here #55 (comment)

@dannyhw
Copy link
Member

dannyhw commented Nov 16, 2020

Most of the discussion here is off topic and the actual issue has no activity for some time. I'm working on a guide for using storybook in a mono repo which could help with these issues. However I believe the issues experienced are more related to react native and metro rather than storybook itself.

I'll be closing this issue due to inactivity, let me know if you want to reopen. Thanks.

@dannyhw dannyhw closed this as completed Nov 16, 2020
@expelledboy
Copy link

expelledboy commented Jan 23, 2021

@dannyhw Did you ever manage to create that guide?

I want to achieve the same as @Brendon127, basically

  • Yarn workspaces with Typescript through out the project
  • Utility and Model packages which are dependencies everywhere
  • Separate package with React Native components
  • Components are testing with storybook
  • Another separate package for an expo app
  • Storybook can be run both in the browser and on the simulator

But I am just going around in circles as this point. I think there are major version clashes in this scenario, and as soon as I fix one thing some other issue pops up...

@dannyhw
Copy link
Member

dannyhw commented Jan 23, 2021

@expelledboy I didn't make the guide yet but if you want you can message me on discord I can try and help you with your project. You can find me in the react-native channel of the storybook discord.

@AichaK1
Copy link

AichaK1 commented Mar 2, 2021

@dannyhw I am having the same problem with the sidebar not loading I did everything but still can't solve it

@dannyhw
Copy link
Member

dannyhw commented Mar 2, 2021

@AichaK1 hey, whilst I understand this issue can be frustrating, I would really appreciate it if you could keep discussion relating to the sidebar not loading in the relevant issue: #55 🙏

@storybookjs storybookjs locked as off-topic and limited conversation to collaborators Mar 2, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants