rpetrich / babel-plugin-transform-async-to-promises

Transform async/await to somewhat idiomatic JavaScript promise chains
MIT License
246 stars 17 forks source link

No tree shaking with externalHelpers: true #78

Open pubkey opened 2 years ago

pubkey commented 2 years ago

When using externalHelpers: true, all helpers are imported from the helpers.js file. This is not optimal because the helpers.js file is not a module and therefore will prevent tree shaking when bundling. Instead we should provide a package.json that handles the export of the helpers. There we can set sideEffects: false and also use the .mjs file if possible.

I tried that out by adding the following package.json in node_modules/babel-plugin-transform-async-to-promises/helpers/package.json

{
    "name": "babel-plugin-transform-async-to-promises-helpers",
    "main": "../a-helpers.js",
    "jsnext:main": "../a-helpers.mjs",
    "module": "../a-helpers.mjs",
    "sideEffects": false
}  

Notice that I prefixed the helpers files with a- to ensure that the bundler picks the package.json instead of the plain file when importing like import { _await } from "babel-plugin-transform-async-to-promises/helpers";.

This worked for me and decreased the overall bundle size.