-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Importing Link from gatsby breaks Storybook #10668
Comments
For now I've added a quick fix to the documentation in #10669 |
…k config (#10669) The steps in the current documentation for using Storybook with Gatsby (https://www.gatsbyjs.org/docs/visual-testing-with-storybook/) don't work because importing Link from gatsby in any component breaks. This is because several internals get required as a result of requiring anything from the default commonjs build (which would have been tree shaken before #9123) and eventually require a non-existent `pages.json`. More details are at #10668 This pull request updates the documentation with a quick fix (setting resolve.mainFields in the storybook webpack config override thereby _preferring_ es6 gatsby over commonjs) Fixes #10662
I applied the fix but now I get this error:
|
@stephanschubert could you provide a reproduction? Sorry you're experiencing that! |
@DSchau A full reproduction is hard (custom flavored monorepo setup here) but I could narrow the issue down: config.module.rules[0].use[0].options.presets = [
require.resolve("@babel/preset-react"),
[
require.resolve("@babel/preset-env"),
{
shippedProposals: true,
useBuiltIns: "entry", // <-- was "usage"
},
],
]; Switching from |
…k config (gatsbyjs#10669) The steps in the current documentation for using Storybook with Gatsby (https://www.gatsbyjs.org/docs/visual-testing-with-storybook/) don't work because importing Link from gatsby in any component breaks. This is because several internals get required as a result of requiring anything from the default commonjs build (which would have been tree shaken before gatsbyjs#9123) and eventually require a non-existent `pages.json`. More details are at gatsbyjs#10668 This pull request updates the documentation with a quick fix (setting resolve.mainFields in the storybook webpack config override thereby _preferring_ es6 gatsby over commonjs) Fixes gatsbyjs#10662
Hiya! This issue has gone quiet. Spooky quiet. 👻 We get a lot of issues, so we currently close issues after 30 days of inactivity. It’s been at least 20 days since the last update here. If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open! Thanks for being a part of the Gatsby community! 💪💜 |
Hey again! It’s been 30 days since anything happened on this issue, so our friendly neighborhood robot (that’s me!) is going to close it. Please keep in mind that I’m only a robot, so if I’ve closed this issue in error, I’m Thanks again for being part of the Gatsby community! |
I am seeing this issue and changing to 'entry' fixed it for me as well. This should probably be re-opened. |
I'm still getting an error on ERROR in my-gatsby/node_modules/gatsby/cache-dir/gatsby-browser-entry.js 17:2
Module parse failed: Unexpected token (17:2)
You may need an appropriate loader to handle this file type.
|
| const StaticQuery = props => (
> <StaticQueryContext.Consumer>
| {staticQueryData => {
| if (
@ ./src/baseComponents/Link/Link.stories.jsx 4:0-30
@ . sync \.stories\.(js|jsx)$
@ ./.storybook/config.js
@ multi my-gatsby/node_modules/@storybook/core/dist/server/common/polyfills.js /my-gatsby/node_modules/@storybook/core/dist/server/preview/globals.js ./.storybook/config.js (webpack)-hot-middleware/client.js?reload=true I've done all the steps in this tutorial: https://www.gatsbyjs.org/docs/visual-testing-with-storybook/ Update: I spun up a gatsby starter with the same versions as my project and importing My project that it isn't working on is a monorepo, using gatsby-themes so there may be something about its setup that is causing this issue but I still haven't located it. |
For anyone having the same issue as me, I found a solution and was given an alternate solution over in the storybook github issues here: storybookjs/storybook#5949 (comment) |
Reopen. |
Same issue here. In my environment, I can workaround to use // import { Link } from "gatsby" // error
import Link from "gatsby-link" // not error |
@terrierscript's solution works nicely. but this is still an issue |
This workaround also solves my problem. |
I'm not sure why this is closed since it is still relevant. I just started a new Gatsby projet with a storybook and I got a lot of trouble finding a solution to this issue. Which in the end is just a patch in the meantime. The following line (source) is causing issues with core-js versions differing in Gatsby and Storybook. // Transpile Gatsby module because Gatsby includes un-transpiled ES6 code.
config.module.rules[0].exclude = [/node_modules\/(?!(gatsby)\/)/] Not importing |
Using link from @reach/router solved my problem, like this
|
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
|
@AaronFeledy could you share your folders structure, and note where you have storybook and in which node_modules you have gatsby? |
Tried this solution but got:
|
|
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 Webpack:
|
As far as I understand the issue, webpack config for storybook should be aware of gatsby package. I've fixed the issue by adding additional line of code to storybook webpack config: which allows webpack to find gatsby package. The path can be different for your case, I have a monorepo, storybook is a part of one package and gatsby is placed in the root node-modules folder. The reason why changing import from gatsby to gatsby-link works is proably because gatsby-link is installed in the folder which was already included in webpack config. Hope this helps. |
I guess using yarn and "nohoist" for gatsby could also do the trick. |
@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..
|
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 But otherwise it was mostly copy and paste. |
@bostrom The problem exposed in this issue is not about the outdated Storybook config documentation, but about a dependency version mismatch between Storybook and Gatsby, making it impossible to import anything from the If you're not importing It looks realated to #15601 |
Ok, maybe I misunderstood the original issue, but I faced the same error as @pdeka when importing components from
But maybe this is about that very issue, i.e. that we have to do a workaround for it, and it doesn't work out of the box. But nevertheless, that piece of documentation helped me get Storybook running while still being able to |
@bostrom maybe it's fixed now? I didn't had the chance to try it since January. |
Having the exact same issue and neither solutions worked.
it works on Storybook but fail build on Gatsby, but if I have
it will work on Gatsby but not Storybook. I ended up doing something a tad dirty
Hoping for a fix soon because I really don't like that hack |
I was having the issue where I was receiving the Though the docs doesn't specifically mention
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 :) |
* move old components ini DEPRECATED_components folder * update packages * set storybook in production mode so the graphql queries work * move images into assets * change images to assets routes * fix storybook error when using Gatsby components (e.g. Link) gatsbyjs/gatsby#10668 (comment) * update gatsby config * disable including fontawesome to fix gatsby svg importer * configure storybook * move logo into images folder and add svg version * adapt logo path * storybook fails on missing folder * temporary fix storybook: logo and footer Co-authored-by: Michael Zoidl <[email protected]> Co-authored-by: lowsky <[email protected]>
I have similar issue, because I am using Yarn Workspaces / monorepo setup. The solution suggested by @AaronFeledy in #10668 (comment) is working for me. I've created a small fix which checks if the Gatsby is not in your project's The code you can add in await new Promise((resolve, reject) => {
const gatsbyBinPath = require("path").resolve(
"./node_modules/.bin/gatsby"
)
require("fs").lstat(gatsbyBinPath, (err, stats) => {
if (stats.isSymbolicLink()) {
if (err) {
reject(err)
} else {
const gatsbyResolvedLink = require("fs").readlinkSync(gatsbyBinPath)
if (gatsbyResolvedLink !== "../gatsby/cli.js") {
config.module.rules[0].include = require("path").resolve(
gatsbyResolvedLink.replace("/../node_modules/gatsby/cli.js", "")
)
}
}
}
resolve()
})
}) If people think it is useful I can create a PR for it to update the docs. Thanks |
It would be awesome to have a fix for |
See here for inspo: gatsbyjs/gatsby#10668 (comment)
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 @tevla I think it should be |
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 If I had a vanilla Storybook webpack configuration, using the latest v5 ( module: {
rules: [
{
test: /\.(mjs|jsx?)$/,
use: [
{
loader: 'babel-loader',
options: babelOptions,
},
],
include: [path.resolve('./')],
exclude: [path.resolve('./node_modules')],
},
{
test: /\.md$/,
use: [
{
loader: require.resolve('raw-loader'),
},
],
},
],
} However, after module: {
rules: [
{
test: /\.md$/,
use: ['raw-loader'],
},
{ parser: { requireEnsure: false } },
{
test: /\.(js|mjs|jsx|ts|tsx)$/,
enforce: 'pre',
use: ['eslint-loader'],
include: ['~/src', '~/.storybook'],
},
{
oneOf: [
{
test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
loader: 'url-loader',
},
{
test: /\.(js|mjs|jsx|ts|tsx)$/,
include: ['~/src', '~/.storybook'],
loader: 'babel-loader',
options: { /* ... */ }
},
{
test: /\.(js|mjs)$/,
exclude: /@babel(?:\/|\\{1,2})runtime/,
loader: 'babel-loader',
options: { /* ... */ },
include: ['~/.storybook'],
},
{ test: /\.css$/, /* ... */ },
{ test: /\.module\.css$/, /* ... */ },
{ test: /\.(scss|sass)$/, /* ... */ },
{ test: /\.module\.(scss|sass)$/, /* ... */ },
{
exclude: [
/\.(js|mjs|jsx|ts|tsx)$/,
/\.html$/,
/\.json$/,
/\.(ejs|md|mdx)$/,
],
loader: 'file-loader',
options: { /* ... */ },
},
],
},
{
test: /\.js$/,
include: /node_modules\/acorn-jsx/,
use: ['babel-loader'],
},
{ test: /\.(stories|story).mdx$/, /* ... */ },
{
test: /\.mdx$/,
exclude: /\.(stories|story).mdx$/,
// ...
},
{
test: /\.(stories|story)\.[tj]sx?$/,
loader: '@storybook/source-loader',
// ...
},
],
}
Importantly, So, when making modifications to the // Transpile Gatsby module because Gatsby includes un-transpiled ES6 code.
config.module.rules[0].exclude = [/node_modules\/(?!(gatsby)\/)/]
// use installed babel-loader which is v8.0-beta (which is meant to work with @babel/core@7)
config.module.rules[0].use[0].loader = require.resolve("babel-loader")
// use @babel/preset-react for JSX and env (instead of staged presets)
config.module.rules[0].use[0].options.presets = [
require.resolve("@babel/preset-react"),
require.resolve("@babel/preset-env"),
]
config.module.rules[0].use[0].options.plugins = [
// use @babel/plugin-proposal-class-properties for class arrow functions
require.resolve("@babel/plugin-proposal-class-properties"),
// use babel-plugin-remove-graphql-queries to remove static queries from components when rendering in storybook
require.resolve("babel-plugin-remove-graphql-queries"),
]
I'm not 100% sure how to change the |
I created a new issue since this is still happening in the latest versions of Gatsby/Storybook: #29702 |
thanks for this |
Same issue and it was fixed using instructions here However it does seem to have take my build time running |
Storybook breaks (#10662) if a component includes
import { Link } from 'gatsby'
This was working fine before #9123 because
Now that main is a commonjs build and the ES6 build is at module, it breaks since
gatsby-browser-entry.js
requirespublic-page-renderer
pages.json
public-page-renderer.js
looks likeI assumed that this would work since
BUILD_STAGE
is not set but it seems webpack wants to resolve both calls torequire
The text was updated successfully, but these errors were encountered: