gatsby: Importing Link from gatsby breaks Storybook
Storybook breaks (https://github.com/gatsbyjs/gatsby/issues/10662) if a component includes import { Link } from 'gatsby'
This was working fine before https://github.com/gatsbyjs/gatsby/pull/9123 because
- main was an es6 build
- webpack would treeshake when Link was imported
Now that main is a commonjs build and the ES6 build is at module, it breaks since
- the commonjs entry point at
gatsby-browser-entry.jsrequirespublic-page-renderer - which tries to require
pages.json - which doesn’t exist because since presumably a gatsby build hasn’t been run yet
public-page-renderer.js looks like
if (process.env.BUILD_STAGE === `develop`) {
module.exports = preferDefault(require(`./public-page-renderer-dev`))
} else if (process.env.BUILD_STAGE === `build-javascript`) {
module.exports = preferDefault(require(`./public-page-renderer-prod`))
} else {
module.exports = () => null
}
I assumed that this would work since BUILD_STAGE is not set but it seems webpack wants to resolve both calls to require
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 36 (6 by maintainers)
Commits related to this issue
- fix(www): update storybook docs to prefer Gatsby ES6 module in webpack config (#10669) The steps in the current documentation for using Storybook with Gatsby (https://www.gatsbyjs.org/docs/visual-tes... — committed to gatsbyjs/gatsby by sidharthachatterjee 6 years ago
- fix(www): update storybook docs to prefer Gatsby ES6 module in webpack config (#10669) The steps in the current documentation for using Storybook with Gatsby (https://www.gatsbyjs.org/docs/visual-tes... — committed to gpetrioli/gatsby by sidharthachatterjee 6 years ago
- fix storybook error when using Gatsby components (e.g. Link) https://github.com/gatsbyjs/gatsby/issues/10668#issuecomment-639014099 — committed to lowsky/JSCraftCamp-site by lowsky 4 years ago
- Redesign base (#881) * move old components ini DEPRECATED_components folder * update packages * set storybook in production mode so the graphql queries work * move images into assets * ch... — committed to jscraftcamp/website by lowsky 4 years ago
- fix: header not showing in storybook fixed by https://github.com/gatsbyjs/gatsby/issues/10668#issuecomment-639014099 — committed to paultibbetts/dark-mode--archived by paultibbetts 4 years ago
- WEBREF-121 :bug: Import gatsby link from native lib See here for inspo: https://github.com/gatsbyjs/gatsby/issues/10668#issuecomment-546596273 — committed to PrideInLondon/pride-london-web by egmcdonald 4 years ago
I was having the issue where I was receiving the
__BASE_PATH__error when using theLinkcomponent fromgatsbyinside another component inside that component’s storyThough the docs doesn’t specifically mention
__BASE_PATH__, I was able to resolve this by adding:Similar to how in the docs they mention to add:
https://www.gatsbyjs.org/docs/visual-testing-with-storybook/#storybook-version-5
Hope that helps someone else 😃
Same issue here.
In my environment, I can workaround to use
gatsby-linkThis workaround also solves my problem.
@Levino I tried nohoist and I have gatsby in the child’s (common component’s) node_modules.
I still get the following when I import gatsby…
Tried this solution but got:
For me the issue was related to using Yarn Workspaces to develop my Gatsby theme and nesting it within the Gatsby package. The Webpack config runs in the root of the current package, in this case my Gatsby theme directory/ Yarn Workspace. My
node_modules/gatsbyfiles that need transpiling are actually in a parent directory. The code in the guide doesn’t consider this scenario, so I needed to update the config to point to the correct directory. Adding the following to my Storybook webpack config did the trick:Having this same issue with Storybook v6 and the recommended settings from Gatsby’s tutorial.
Unfortunately, I was only able to make it work switching to
gatsby-link, what I find not that great 😦@tevla I think it should be
import { navigate } from "gatsby-link". It works fine here both for Storybook and Gatsby.Gatsby has some useful information on how to tweak the Storybook config to make it work https://www.gatsbyjs.org/docs/visual-testing-with-storybook/.
However, since Storybook’s config files have changed in version 5.3, and Gatsby’s documentation isn’t up to date yet, the stuff they say should go into
./storybook/config.jsshould actually go into./storybook/preview.js. And the things they say should go into.storybook/webpack.config.jsshould actually go into thewebpackFinaloption of.storybook/main.js.But otherwise it was mostly copy and paste.
Why is this issue closed? Its is an issue related to creating a common lib, adding gatsby to it and importing ‘Link’ (or anything else). Its quite simple to reproduce as well. Am I missing something? It is not a Story book related issue. Its for all shared libs where gatsby is used.
It effectively stops us from using gatsby in a common lib. We have 3 templates and common/shared libs across the templates. https://share.getcloudapp.com/p9uKLEkK
Importing ‘gatsby-link’ also does not work for the reasons mentioned above by @salatielq .
Env Lerna: 3.20.2 gatsby-cli: 2.10.10 gatsby: 2.19.41 Node: 12.16.1 - ~/.nvm/versions/node/v12.16.1/bin/node Yarn: 1.22.4 - ~/.yarn/bin/yarn npm: 6.14.2 - ~/.nvm/versions/node/v12.16.1/bin/npm
Webpack:
Using link from @reach/router solved my problem, like this
import { Link } from "@reach/router";I applied the fix but now I get this error:
I think these lines in Visual Testing with Storybook - Storybook version 5 are problematic because it makes an assumption about the order of the Webpack rules.
Namely, it doesn’t take into consideration if you have other add-ons that also change the rules. In my case, this is happening because I use
@storybook/preset-create-react-app@2.1.2, which modifies these rules before thewebpackFinalcallback runs.If I had a vanilla Storybook webpack configuration, using the latest v5 (
v5.3.21), the config should output the followingmodule.rules:However, after
@storybook/preset-create-react-appexecutes, mymodule.ruleswebpack config looks like this (several items below have been pruned / edited so it fits better in this comment):Importantly,
@storybook/preset-create-react-appmoves all “non JS” rules to the front when it finally returns a webpack config. Since we only have two rules before (the/\.(mjs|jsx?)$/and the/\.md$/one), this effectively moves the/\.md$/rule to the front of our array.So, when making modifications to the
rulesfirst item via:config.module.rules[0]is not the rule concerning how JS-related files are loaded, and instead we are making changes to how Markdown files are loaded!I’m not 100% sure how to change the
webpackFinal(config)callback to address this, but either way, if you use@storybook/preset-create-react-app, the instructions given will not work as they are intended!@turistua
@terrierscript’s solution works nicely. but this is still an issue