developit / microbundle

📦 Zero-configuration bundler for tiny modules.
https://npm.im/microbundle
MIT License
8k stars 361 forks source link

Overwrite babel config for UMD output #1022

Closed daun closed 10 months ago

daun commented 1 year ago

Is there an official/recommended way of enabling certain babel transforms only for the UMD build? Specifically, the goal is to make sure that ES6 classes will always be transpiled to function constructors in the UMD version.

We're using microbundle for a library with a plugin ecosystem and currently, the core lib seems to not transpile classes and some plugins do transpile them; we need to ensure it's either one or the other. Currently, we're hacking it with custom browserlistrc settings, but that doesn't work reliably or long-term.

I assume I'll need to hook into @rollup/babel-plugin somehow but not sure how. I've tried BABEL_SHOW_CONFIG_FOR=./src/index.js npm run build to debug the applied babel config, but it's hard to grok. Adding a babelrc to the project wouldn't work since it should only apply to the UMD build, not the others.

The good thing here is we already separated the modern from the standalone builds in our scripts. But there's probably no command line argument for specifying a custom babel config?

{
  "scripts": {
    "build": "npm run build:module && npm run build:bundle",
    "build:module": "microbundle src/index.ts -f modern,esm,cjs",
    "build:bundle": "microbundle src/index.ts -f umd --external none"
  }
}
daun commented 10 months ago

Closing this as we've found a solution via browserslist.