developit / microbundle

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

add Polyfill #907

Open lvzegeng opened 2 years ago

lvzegeng commented 2 years ago

I want to add Polyfill to new syntax such as Object.assign when packaging, for example, add useBuiltIns:'entry' to the corresponding babel configuration. What should I do, create a new babel configuration file, if so, what is the default babel configuration of the microbundle? I can modify it based on this

developit commented 2 years ago

Microbundle's babel configuration is generated here: https://github.com/developit/microbundle/blob/afa74ca46b455c7cb76ac7e6c656b1f32012d2cd/src/lib/babel-custom.js#L171

useBuiltIns:'entry' is not supported because it modifies the global runtime environment, which is something libraries generally should avoid doing. For Object.assign, you could have Babel transpile to an inline helper that would avoid modifying the Object global: https://www.npmjs.com/package/@babel/plugin-transform-object-assign

lvzegeng commented 2 years ago

Not only Object.assign, but also other new APIs, such as Iterator, Generator, Set, Maps, Proxy, Reflect, Symbol, Promise and other objects. I think I should use "useBuiltIns": "usage", how should I modify the configuration now to achieve it. Just create a new babel.config.json file as follows, this will completely overwrite or merge the default configuration:

{
   "presets": [
     [
       "@babel/preset-env",
       {
         "useBuiltIns": "usage",
         "corejs": 3,
       }
     ]
   ],
   "plugins": [
     ["@babel/plugin-transform-runtime", {
       "corejs": 3
     }]
   ]
}
developit commented 2 years ago

@lvzegeng it will overwrite the default configuration.