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

StoryBook does not work with GatsbyJS #17176

Closed
alfredoperez opened this issue Jan 9, 2022 · 5 comments
Closed

StoryBook does not work with GatsbyJS #17176

alfredoperez opened this issue Jan 9, 2022 · 5 comments

Comments

@alfredoperez
Copy link

Describe the bug
StoryBook doesnt work with GatsbyJS

To Reproduce

  • Create a new project using GatsbyJS
 npm install -g gatsby-cli
 gatsby new
Uncaught TypeError: $ is not a function
    at Object../node_modules/core-js/modules/es.global-this.js (es.global-this.js:6:1)
    at __webpack_require__ (bootstrap:24:1)
    at fn (hot module replacement:61:1)
    at Object../node_modules/core-js/internals/global.js (get-substitution.js:44:1)
    at __webpack_require__ (bootstrap:24:1)
    at fn (hot module replacement:61:1)
    at Object../node_modules/core-js/internals/export.js (export.js:1:1)
    at __webpack_require__ (bootstrap:24:1)
    at fn (hot module replacement:61:1)
    at Object../node_modules/core-js/modules/es.array.for-each.js (es.array.for-each.js:2:1)

System

Environment Info:

  System:
    OS: Windows 10 10.0.22000
    CPU: (12) x64 Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz
  Binaries:
    Node: 16.13.1 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.17 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 8.1.2 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.22000.120.0), Chromium (97.0.1072.55)
  npmPackages:
    @storybook/addon-actions: ^6.4.9 => 6.4.9 
    @storybook/addon-essentials: ^6.4.9 => 6.4.9 
    @storybook/addon-links: ^6.4.9 => 6.4.9 
    @storybook/builder-webpack5: ^6.4.9 => 6.4.9 
    @storybook/manager-webpack5: ^6.4.9 => 6.4.9 
    @storybook/react: ^6.4.9 => 6.4.9 

Additional context
You can use the following repo to reproduce it https://github.com/alfredoperez/gatsby-storybook-error

@jonniebigodes
Copy link
Contributor

Hi @alfredoperez thanks for reaching out to us with your issue we appreciate it. I've picked up on it and i was able to triage it and hopefully add a solution that will help you and possibly other Storybook users working with Gatsby. Here's the steps I took to triage it:

  • Cloned your reproduction

  • Installed the necessary dependencies

  • Ran npm run storybook and opened up a browser window to http://localhost:6006 and I was able to confirm the issue as you can see:
    gatsby_storybook_initial_error

  • Checked your .storybook/main.js and saw that you're using the community addon storybook-addon-gatsby and that is a part of the issue. It seems that there's a part of the configuration that is missing from that addon and is necessary to allow Storybook to function properly with Gatsby.

  • With the above in mind I disabled the addon from .storybook/main.js and updated the configuration object, turning it into the following:

// .storybook/main.js
module.exports = {
  "stories": [
    "../src/**/*.stories.mdx",
    "../src/**/*.stories.@(js|jsx|ts|tsx)"
  ],
  "addons": [
    "@storybook/addon-links",
    "@storybook/addon-essentials",
    //"storybook-addon-gatsby"
  ],
  "framework": "@storybook/react",
  "core": {
    "builder": "webpack5"
  },
  webpackFinal: async config => {
    // Transpile Gatsby module because Gatsby includes un-transpiled ES6 code.
    config.module.rules[0].exclude = [/node_modules\/(?!(gatsby)\/)/]

    // Remove core-js to prevent issues with Storybook
    config.module.rules[0].exclude= [/core-js/]
    // Use babel-plugin-remove-graphql-queries to remove static queries from components when rendering in storybook
    config.module.rules[0].use[0].options.plugins.push(
      require.resolve("babel-plugin-remove-graphql-queries")
    )

    config.resolve.mainFields=["browser", "module", "main"]
    return config
  },
}
  • Also, the .storybook/preview.js required some changes, as the configuration provided by the addon was no longer in effect, turning my .storybook/preview.js into the following:
// ./storybook/preview.js
import { action } from "@storybook/addon-actions"

// Gatsby's Link overrides:
// Gatsby Link calls the `enqueue` & `hovering` methods on the global variable ___loader.
// This global object isn't set in storybook context, requiring you to override it to empty functions (no-op),
// so Gatsby Link doesn't throw errors.
global.___loader = {
  enqueue: () => {},
  hovering: () => {},
}
// This global variable prevents the "__BASE_PATH__ is not defined" error inside Storybook.
global.__BASE_PATH__ = "/"

// Navigating through a gatsby app using gatsby-link or any other gatsby component will use the `___navigate` method.
// In Storybook, it makes more sense to log an action than doing an actual navigate. Check out the actions addon docs for more info: https://storybook.js.org/docs/react/essentials/actions

window.___navigate = pathname => {
  action("NavigateTo:")(pathname)
}

export const parameters = {
  actions: { argTypesRegex: "^on[A-Z].*" },
  controls: {
    matchers: {
      color: /(background|color)$/i,
      date: /Date$/,
    },
  },
}
  • Created a small component and story to test it a bit more and see if the issue presented itself:
// src/components/ExampleComponent.js

import React from 'react';
import PropTypes from 'prop-types'
export default function ExampleComponent({someProp, someOtherProp}) {
    return (
        <div>
            <h1>I'm a example component with the following props and values</h1>
            <p>someProp: {someProp}</p>
            <p>someProp: {someOtherProp}</p>
        </div>
    )
}

ExampleComponent.propTypes= {
    someProp:PropTypes.string,
    someOtherProp:PropTypes.number
}
// src/components/ExampleComponent.stories.js
import React from "react"
import ExampleComponent from "./ExampleComponent"
export default {
  component: ExampleComponent,
  title: "Components/ExampleComponent",
}
const Template = args => <ExampleComponent {...args} />
export const Default = Template.bind({})
Default.args = {
  someProp: "Something",
  someOtherProp:33,
};
  • Ran npm run storybook once again and i was presented with the following:
    gatsby_storybook_working_dev_mode

  • To make sure everything is working i've added http-server to emulate a production environment. Ran npm run build-storybook , followed by http-server storybook-static and opened up a browser window to the provided URL (http://localhost:8080) and i was able to see the following:
    gatsby_storybook_working_prod_mode

All of this triage process was done on the following set up:

Environment Info:

  System:
    OS: Windows 10 10.0.19042
    CPU: (4) x64 Intel(R) Core(TM) i7-6500U CPU @ 2.50GHz
  Binaries:
    Node: 16.13.1 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.15 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
    npm: 8.1.2 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.1266.0), Chromium (97.0.1072.55)
  npmPackages:
    @storybook/addon-actions: ^6.4.9 => 6.4.9
    @storybook/addon-essentials: ^6.4.9 => 6.4.9
    @storybook/addon-links: ^6.4.9 => 6.4.9
    @storybook/builder-webpack5: ^6.4.9 => 6.4.9
    @storybook/manager-webpack5: ^6.4.9 => 6.4.9
    @storybook/react: ^6.4.9 => 6.4.9

Could you check on your end and see if the changes mentioned helped you solve your issue and follow up with us so that we can close this issue or continue to work on it until we find a suitable solution.

Have a great day!

Stay safe

@shilman
Copy link
Member

shilman commented Jan 14, 2022

@jonniebigodes Thanks so much for hunting down a workaround!!! Might this be a better issue for storybook-addon-gatsby?

@jonniebigodes
Copy link
Contributor

@shilman no need to thank! And indeed you're correct. I've created an issue in the addon repository to address this one and mentioned the workaround so that it can be followed up !

With that @alfredoperez if you don't mind I'm closing this issue here, as it's already being tracked in the linked issue in the addon repository. If you wouldn't mind continuing the conversation there I'd appreciate it. Once again thanks for bringing this to our attention and I'm hoping this issue is fixed soon and you'll be able to use both tools without any additional issues.

Have a great weekend.

Stay safe

@alfredoperez
Copy link
Author

Thanks @jonniebigodes I was going to give it a try this weekend to your manual setup in my personal repo. I don't mind not using the addon, but I want to use StoryBook with Gatsby.

@sh4d1t0
Copy link

sh4d1t0 commented Jan 16, 2022

Thank you @jonniebigodes with this changes works perfectly

Enviroment Info

System:
OS: Windows 11, MacOS Monterey
Browsers:
Edge, Chrome, Safari
Packages:
Gatsby: 4.5.2
Storybook: 6.4.13

chrisj-skinner pushed a commit to chrisj-skinner/gatsby that referenced this issue Sep 16, 2022
This section needs to be update - working `main.js` contents taken from storybookjs/storybook#17176 (comment)

It is clear these docs need updating as the above issue was created on 12 Jan 2022. There has only been one commit to this file since then, which doesn't address the above issue. [commit](gatsbyjs@d3e0cd9).

It appears the community plugin needs some work so until that time I would suggest removing that from the docs
chrisj-skinner pushed a commit to chrisj-skinner/gatsby that referenced this issue Sep 16, 2022
- Update docs as per solution for issue: storybookjs/storybook#17176 (comment)
- Community plugin https://storybook.js.org/addons/storybook-addon-gatsby is not up to date which is causing issues - removing this section

The above mentioned issue was created on the 12 Jan and there has only been one commit to this file since then (gatsbyjs@d3e0cd9) which does not address this issue.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants