Microbundle: Optional Chaining Breaks Without No Compress Flag

Created on 10 Aug 2020  路  4Comments  路  Source: developit/microbundle

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} />
  )
}
bug

Most helpful comment

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

All 4 comments

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
      }
    ]
  ]
}

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

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

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SleeplessByte picture SleeplessByte  路  3Comments

Kikobeats picture Kikobeats  路  4Comments

sptimer picture sptimer  路  4Comments

MarkLyck picture MarkLyck  路  4Comments

bitttttten picture bitttttten  路  4Comments