I'm working on a web project using Babel 7 with Webpack 4. I've never used Babel before and can't really understand some parts of it. Based on the documentation I'm using @babel/preset-env because it seems the recommended way (especially for beginners). Also using Browserslist integration via my .browserslistrc file.
Webpack does the compilation well (babel-loader version 8.0.2), I have no errors but I'm confused about this useBuiltIns: "entry" option mentioned here and how polyfill system is working in Babel.
.babelrc.js
module.exports = {
presets: [
['@babel/preset-env', {
"useBuiltIns": "entry" // do I need this?
}]
],
plugins: [
'@babel/plugin-syntax-dynamic-import'
]
};
.browserslistrc
Copied from here (thought reasonable because my project is using Bootstrap).
>= 1%
last 1 major version
not dead
Chrome >= 45
Firefox >= 38
Edge >= 12
Explorer >= 10
iOS >= 9
Safari >= 9
Android >= 4.4
Opera >= 30
So my questions are:
1) Do I need to use that useBuiltIns: "entry" option?
2) Do I need to install @babel/polyfill package and start my vendors.js with require("@babel/polyfill"); ?
3) What if I omit both?
If I do 1 and 2, my vendors.js grows up to 411 KB
If I ommit both it's just 341 KB
after a production build.
I thought @babel/preset-env handles all the rewrites and polyfills by default without any extra import/require needed on my side...
Thanks!
-- EDIT --
Babel's team has just updated the docs of @babel/polyfill based on some GitHub issues (including mine) complaining about unclear/misleading documentation. Now it's obvious how to use it. (...and after that my original question seems stupid :)
Yes, if you want to include polyfills based on your target environment.
TL;DR
There're basically 3 options for
useBuiltIns:"entry": when using this option,
@babel/preset-envreplaces direct imports ofcore-jsto imports of only the specific modules required for a target environment.That means you need to add
to your entry point and these lines will be replaced by only required polyfills. When targeting chrome 72, it will be transformed by
@babel/preset-envto"usage": in this case polyfills will be added automatically when the usage of some feature is unsupported in target environment. So:
in browsers like
ie11will be replaced withIn case target browser is latest chrome, no transformations will apply.
That's personally my chosen weapon as there's no need to include anything (core-js or regenerator) in source code as only required polyfills will be added automatically based on target environment set in browserlist.
false: that's the default value when no polyfills are added automatically.
Yes for environment prior to
babel v7.4andcore-js v3.TL;DR
No. Starting from
babel v7.4andcore-js v3(which is used for polyfilling under the hood)@babel/preset-envwill add the polyfills only when it know which of them required and in the recommended order.Moreover
@babel/polyfillis considered as deprecated in favor of separatecore-jsandregenerator-runtimeinclusions.So using of
useBuiltInswith options other than false should solve the issue.Don't forget to add
core-jsas a dependency to your project and set its version in@babel/preset-envundercorejsproperty.As @PlayMa256 already answered, there will be no polyfills.
More detailed and whole info con be found at
core-jscreator's pageAlso please feel free to play with babel sandbox