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

Optional Chaining Breaks Without No Compress Flag #694

Closed
pkrawc opened this issue Aug 10, 2020 · 4 comments · Fixed by #702
Closed

Optional Chaining Breaks Without No Compress Flag #694

pkrawc opened this issue Aug 10, 2020 · 4 comments · Fixed by #702
Assignees
Labels

Comments

@pkrawc
Copy link

pkrawc commented Aug 10, 2020

Running microbundle --jsx React.createElement will break on the following component unless --no-compress is passed

import * as React from "react"
import { createPortal } from "react-dom"

export default function Portal({ children, type = "avail-portal" }) {
  const portalNode = React.useRef(null)
  const mountNode = React.useRef(null)
  const [s, update] = React.useState({})
  React.useLayoutEffect(() => {
    if (typeof window === "undefined" || !mountNode.current) return
    const owner = mountNode?.current?.ownerDocument
    portalNode.current = owner.createElement(type)
    owner.body.appendChild(portalNode.current)
    update({})
    return () => {
      if (portalNode?.current?.ownerDocument) {
        portalNode?.current?.ownerDocument?.body?.removeChild(
          portalNode.current,
        )
      }
    }
  }, [mountNode])
  return portalNode.current ? (
    createPortal(children, portalNode.current)
  ) : (
    <aside ref={mountNode} />
  )
}
@developit
Copy link
Owner

developit commented Aug 10, 2020

I believe this is because we're using @babel/preset-modules for the modern builds, which does not currently include stage-4 transforms like Nullish Coalescing and Optional Chaining. We need to switch to @babel/preset-env now that it has the bugfixes:true option.

FWIW --no-compress is just passing through the optional chaining syntax, which is definitely not something most folks would be expecting to find in an npm package. For now you could enable it by adding a .babelrc containing the following:

{
  "plugins": [
    [
      "@babel/plugin-proposal-optional-chaining",
      {
        "loose": true
      }
    ]
  ]
}

@pkrawc
Copy link
Author

pkrawc commented Aug 10, 2020

Thanks @developit for the workaround for now. Will microbundle consume babel.config.js the same way as a .babelrc?

@pkrawc
Copy link
Author

pkrawc commented Aug 10, 2020

I assume yes but I've only ever seen .babelrc mentioned in the documentation.

@wardpeet
Copy link
Collaborator

@pkrawc we should also support babel.config.js if not mistaken.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants