storybook: Unable to use Storybook as expected in a monorepo
Describe the bug
I am working in a lerna-managed monorepo with Typescript. Originally, we had a separate storybook configuration in each package, which worked nicely. We decided we wanted a root-level storybook that pulled from the *.stories.tsx files inside each package.
Here is an example of the desired file structure:
root
.storybook
packages
+-- package1
+-- Button.stories.tsx
+-- package2
+-- FancyButton.stories.tsx
After following the fix in this issue to solve our original exports not found error, we had no more error messages, but also no stories appeared.
After digging into the devtools for some time, we found that the stories were being read, and the correct paths were being generated. It looked like at some point they were unexpectedly cleared from the StoryStore.
Is this expected behavior? Is it possible to create a top-level storybook that reads from stories inside of packages in a lerna-managed monorepo?
Expected behavior I expected to see a working storybook with stories from all of my packages in it.
Code snippets
webpack.config.js
module.exports = (baseConfig, env, config) => {
// There is a bug in storybook for monorepo users.
// Lines 5 - 12 are a temporary fix for this problem found in this issue:
// https://github.com/storybooks/storybook/issues/3346#issuecomment-425516669
config.module.rules = config.module.rules.filter(
rule =>
!(
rule.use &&
rule.use.length &&
rule.use.find(({ loader }) => loader === 'babel-loader')
)
);
config.module.rules.push({
test: /\.(ts|tsx)$/,
loader: require.resolve('babel-loader'),
options: {
presets: [
[
'react-app',
{
flow: false,
typescript: true,
},
],
],
},
});
config.resolve.extensions.push('.ts', '.tsx');
return config;
};
config.ts
import { configure } from '@storybook/react';
// automatically import all files ending in *.stories.tsx
const req = require.context('../packages', true, /.stories.tsx$/);
function loadStories() {
req.keys().forEach(req);
}
configure(loadStories, module);
System:
- MacOS
- Macbook Pro mid-2015
- chrome
- react
- @storybook/react@4.1.6
Additional context
I ended up just creating a top-level /stories folder and moving all of my packages into it. That works nicely. I’d just like to understand why the structure I was originally aiming for doesn’t work.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 24
- Comments: 26 (12 by maintainers)
This is important.
"@storybook/react": "^6.3.4",doesn’t have it. Here is the main file. Only stories at root folder works and not inside any of packages/**@shilman This is closed. Not sure why. What would be the workaround?
@claudio-moya Could you please post your patch?
Whoopee!! I just released https://github.com/storybookjs/storybook/releases/tag/v6.0.0-alpha.0 containing PR #8822 that references this issue. Upgrade today to try it out!
You can find this prerelease on the
@nextNPM tag.Closing this issue. Please re-open if you think there’s still more to do.
I am also unable to get SB5 working with our Lerna monorepo. We have a custom Webpack config which I haven’t had any success updating to the single signature format. Initially it was blowing up on the .css files (we use PostCSS), and after some trial and error I progressed to it blowing up on the JSX parsing.
Storybook will run with above config if I only include a trivial example, however when I attempt to load a story with some of our monorepo components, I get a bunch of errors like:
and
For comparison, my working SB4 custom Webpack config is
PR is open addressing this, will be a 6.0.0 feature
this should not be closed. This issue will only be more important with the rise of Nrwl NX
@ndelangen Ok, no problem. I’m pretty sure it is a very specific problem to monorepos with Gatsby and storybook not at the root of the project. Take care!
@Jimmydalecleveland removing that include is on my todo list, but I’m afraid of it messing up people’s setup, So I want to do it for the next major (6.0) I think.
My solution works because it simply removes all include filters.
Yours work, because… I have no freaking clue why. 😭 not enough context on the project file structure to really judge.
@ndelangen Thank you for the quick response. I solved this problem last night differently, and now that I’ve tried your suggestion and it also works, I’m not understanding why.
I pushed a separate include rule:
path.resolve('../../node_modules/gatsby')because my understanding was that Gatsby has un-transpiled ES6 code and I saw from the Error I previously posted that it was failing when encountering JSX in my project’s root/node_modules/gatsby. That worked, but I’m not sure why your suggestion works, since it is removing the include for the inner yarn workspace package. If you don’t mind, could you explain that a bit?If you need more context, here is the before and after log of what that config looks like after applying your suggestion:
And this is the line I was previously using that worked in
./my-project/themes/gatsby-theme-storybook/.storybook/webpack.config.js:config.module.rules[0].include.push(path.resolve('../../node_modules/gatsby'))Thanks again, I really appreciate it.
thanks @Jimmydalecleveland, your fix worked for me! I also have a gatsby app inside a monorepo with yarn workspaces and kept getting an ambiguous error from nodemodules/gatsby when running
yarn storybook@Jimmydalecleveland run storybook with the
--debug-webpackflag and it will print the webpack config for you.Look at the loaders defined and you’ll see there are exclude & include rules for the loaders responsible for transpiling code.
I think if you just remove the include property, it should start to work for you.
Just noticed this import is not working when using in a Lerna monorepository
import '@fortawesome/fontawesome-svg-core/styles.css';. When copied @fortawesome folder inside storybook folder’s node_modules folder, the import started working (although can’t get FA icons to show up yet)…Sorry I should add I guess what I did to work around #3346 is:
Which is perhaps a little simpler and less disruptive that what you posted…
We don’t have a lerna tag –
compatibility with other toolsis our catchall. If I see a lot more lerna-related issues I’ll def create one.