microbundle icon indicating copy to clipboard operation
microbundle copied to clipboard

add Polyfill

Open lvzegeng opened this issue 4 years ago • 3 comments

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

lvzegeng avatar Oct 29 '21 13:10 lvzegeng

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

developit avatar Oct 29 '21 15:10 developit

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

lvzegeng avatar Oct 29 '21 16:10 lvzegeng

@lvzegeng it will overwrite the default configuration.

developit avatar Mar 07 '22 20:03 developit