storybook: Decorators syntax usage is broken
Describe the bug When trying to write a story that contains code using decorators syntax (In this case Mobx - the decorators works totally fine in the project) it throws an error:
index.js:39 Error: Decorating class property failed. Please ensure that proposal-class-properties is enabled and set to use loose mode. To use proposal-class-properties in spec mode with decorators, wait for the next major version of decorators in stage 2.
The babel configuration in .babelrc is correct, the file is read:
{
"plugins": [
"@babel/plugin-syntax-dynamic-import",
["@babel/plugin-proposal-decorators", { "legacy": true }],
["@babel/plugin-proposal-class-properties", { "loose": true }],
"babel-plugin-styled-components",
"@babel/plugin-proposal-export-default-from",
"@babel/plugin-transform-async-to-generator",
"@babel/plugin-transform-classes",
"@babel/plugin-transform-runtime"
],
"presets": [
[
"@babel/preset-env",
{
"useBuiltIns": "entry",
"modules": false
}
],
"@babel/preset-react"
],
}
To Reproduce Steps to reproduce the behavior:
- Create story using the decorator
- Try open it - error in the console
Expected behavior Decorators can be used.
System:
- OS: Ubuntu
- Framework: react
- Addons: [actions, viewport]
- Version: 5.0.1
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 24 (5 by maintainers)
I’m also using create-react-app, mobx and storybook and having issues with decorators.
I managed to get around this by having duplicate
.babelrcfiles. The firstbabelrcis in myrootdirectory contianing:The second
.babelrcfile I put in my.storybookdirectory looks like:I had to give everything alias name so it didn’t clash with the root level babelrc.
So far things look good…
Well 😄 Actually what I post in the first commit is IMHO the minimal
babelrcconfig that reproduces the problem.In the repo is only ejected CRA, initiated storybook with https://github.com/storybooks/storybook/tree/next/app/react and added babel config to
package.jsonThe actual code that reproduces it is in
src/stories/index.jsand it is simply:There have to be something with storybook internals, I guess. Probably MobX itself is doing nothing wrong here. I ping you after @pgoforth comment because I thought that maybe you’ve seen similar case before. Let’s wait for some maintainers answer @shilman
This likely has to do with legacy implementation of
@babel/plugin-proposal-decorators. I’m working on a project using the latest decorator proposal and everything works fine:@darkowic When I use this in your babel config in that test repo, the error is gone:
I realize you need to use legacy decorators, but maybe there’s a different legacy config that will work for you.
@darkowic I can’t tell if you found the solution to your issue, but I was experiencing the same problem.
After struggling for a while, I found this comment https://github.com/babel/babel/issues/8577#issuecomment-439579410 which helped lead me to the solution that worked for me.
Adding the plugins to
.babelrcwas not enough. I needed to create a custom.storybook/webpack.config.jsand add the plugins there. Once I had the custom webpack config, the error was gone and the stories were working properly.@darkowic this is not what maintainers typically call a minimal reproduction 😉. Can you link to the babel config in that repo?
Edit: or is a blank storybook that large? Sorry, zero experience with storybook, just wanted to peek at the babel config to see if there is something obvious
@richardwardza Tried every solution around and yours is the only one that worked for me, Thaks a Lot!!
Just met the same issue with
create-react-appcreated React projects.The issue is about the order of overriding options passed to
babel-loader. If it is acreate-react-appproject,@storybook/reactwould detect ifreact-scriptsis present. If yes, it will load the options fromreact-scriptspackage. As you might know,react-scriptsfromcreate-react-apphas configured babel options as below.The default option for
@babel/plugin-proposal-decoratorsis withlegacyflagfalse.The babel configuration from
react-scriptscome after customized storybook .babelrc. That is why .babelrc from storybook is not picking the legacy flag.The work-round for me is to eject
create-react-appproject for now. After ejection,react-scriptsis no longer there.@shilman @darkowic @mweststrate Seems this is an issue with Babel https://github.com/babel/babel/issues/7373
@darkowic note that error in the screenshot actually signals that it should be in loose mode, which is also in your config, which seems to suggest the config isn’t actually picked up? MobX requires loose mode, that is correct. It seems that that is what storybook expects as well from the above message.
(loose mode is also the typescript behavior, and with loose mode it is easier to change descriptors with decorators, which is harder in strict mode, which seems to be a problem with the spec that sadly not everybody agrees upon, but it’s on hold now anyway)
@darkowic I don’t know what to tell you. I can use decorators on static properties in my codebase and everything works fine in Storybook. However, I am not using Mobx and I am using the latest decorators spec. This seems like more of an incompatibility between Mobx (or a dependency thereof) and Storybook.
I did notice one thing. When using
@babel/plugin-proposal-class-propertiesin loose mode, it does not useObject.defineProperty. That may have some bearing on the effectiveness of decorators, since they are used for modifying property descriptors. Hopefully this leads you in the right direction,but like @mweststrate implied, without code to test against, we can only guess what’s causing the issue.[Update] Looking through your repo nowLooks superficially fine (assuming it is using babel 7, not 6). Maybe the config is not picked up at all from that place? Anyway, leaving it to the storybook maintainers for now… 😃
legacyshould betrue. Beyond that, share setup and example or didn’t happen.