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

Something in Storybook's default Babel config causes ReferenceError (tdz) errors #10250

Closed
vdh opened this issue Mar 30, 2020 · 8 comments
Closed

Comments

@vdh
Copy link
Contributor

vdh commented Mar 30, 2020

With some Typescript code like this example:

const Column: FC<ColumnProps> = ({
	cards = [],
  /* … */
}) => {
  /* … */
  return (
    {/* … */}
      <CardList
        cards={cards}
      />
    {/* … */}
  );
};

Will get compiled normally by Babel into:

const Column = (_ref) => {
  let {
    cards = [],
    /* … */
  } = _ref;
  /* … */
  react__WEBPACK_IMPORTED_MODULE_1___default.a.createElement(_CardList__WEBPACK_IMPORTED_MODULE_24__["default"], {
    cards: cards,
    /* … */
  })
};

But under the default Storybook babel setup, gets compiled into:

const Column = (_ref) => {
  var _ref3 =
  /*#__PURE__*/
  react__WEBPACK_IMPORTED_MODULE_1___default.a.createElement(_CardList__WEBPACK_IMPORTED_MODULE_24__["default"], {
    cards: cards,
    /* … */
  });

  let {
    cards = [],
    /* … */
  } = _ref;
  /* … */
};

And creates a ReferenceError error because the React.createElement call is moved higher than the let, so it's trying to use cards too early.

But I can't figure out how to debug this to find out which Babel plugin or config is doing this broken var / let interaction.

@shilman
Copy link
Member

shilman commented Mar 30, 2020

Can you create a minimal repro?

@vdh
Copy link
Contributor Author

vdh commented Apr 15, 2020

@shilman Sorry, I haven't had time to put one together yet. Until I have time to create a full repro, I suspect this is due to @babel/plugin-transform-react-constant-elements, because the error is happening when JSX is being hoisted above let statements.

@vdh
Copy link
Contributor Author

vdh commented Apr 15, 2020

After patching Storybook's Webpack config for babel-loader to exclude @babel/plugin-transform-react-constant-elements, the code runs normally again. I had to jump through some hoops of assumptions about the config to patch it out though, so my patch isn't robust.

I couldn't find an exact issue in the Babel repo that relates directly to what I encountered, but the plugin seems to have a history of being buggy, e.g.: facebook/create-react-app#553

The closest I could find was babel/babel#4419 which seems to have a PR, but that PR hasn't been shipped as far as I can tell (and isn't referenced from any release notes I checked).

@stale
Copy link

stale bot commented May 6, 2020

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!

@stale stale bot added the inactive label May 6, 2020
@vdh
Copy link
Contributor Author

vdh commented May 26, 2020

@shilman So what exactly is the reason why Storybook includes @babel/plugin-transform-react-constant-elements by default anyway? Why is an optimisation plugin bundled by default? It changes the way the user's code is executed, and has plenty of demonstrable flaws.

@shilman
Copy link
Member

shilman commented May 27, 2020

@vdh i'm not sure, but maybe @ndelangen knows

@ndelangen
Copy link
Member

Let's remove the plugin!

Would anyone be able to open a PR removing it?

@Pyrolistical
Copy link
Contributor

the plugin was removed in #11086

@vdh vdh closed this as completed Jun 9, 2020
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