Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it better to have polyfills as import statements with babel-preset-env or add them in entries at webpack.config.js?

babel-preset-env offers the option useBuiltIns to apply it to babel-polyfill and transform import 'babel-polyfill' to specific, environment-based, import 'core-js/whatever.

Is this transformation applied if I add babel-polyfill in my Webpack entry section (see example below), or in that case useBuiltIns is ignored and replaced by all possible imports?

entry: {
  app: ['babel-polyfill', './src/app.js']
}
like image 231
Alf Avatar asked Apr 07 '17 15:04

Alf


People also ask

Do you need Babel polyfill?

So long story short, just using babel is not enough for your application to work because all the latest Javascript features are not supported in all browsers. So to fix this problem, we need to use a polyfill.

Does Babel add Polyfills?

Babel includes a polyfill that includes a custom regenerator runtime and core-js. This will emulate a full ES2015+ environment (no < Stage 4 proposals) and is intended to be used in an application rather than a library/tool. (this polyfill is automatically loaded when using babel-node ).

What is Babel preset ENV used for?

@babel/preset-env is a smart preset that allows you to use the latest JavaScript without needing to micromanage which syntax transforms (and optionally, browser polyfills) are needed by your target environment(s). This both makes your life easier and JavaScript bundles smaller!

Which help in using Babel with Webpack?

We have to install the Babel dependencies and load them. To get the loaders working, update the webpack. config file just like in the style loaders, with babel-loaders. And now you should have a JavaScript project together with Webpack and Babel support.


1 Answers

It works when you are specifing:

"presets": [ ..., "env" ]

It doesn't related to the entry point as your ./src/app.js already includes some code with requirements, I guess. I just don't understand what do you want to achieve by adding babel-polyfill to the entry point, but seems it no effect in this case.

I'll try to elaborate how it works. There is a babel-preset-env plugin which prepares the list of transformation plugins and polyfills. This list is used by transform-polyfill-require-plugin which look for import and require statements and replaces it by list of environment-specific modules.

It doesn't related to the entry point at all. By adding the babel-polyfill you just add it's code into your bundle. So the transform-polyfill-require-plugin doesn't work there anyhow. It's possible to check it via a simple debugging.

And you don't need it really. You can just add require("babel-polyfill"); once in your app as it's noticed in the docs. You even can't import babel-polyfill twice as it might cause an error as it writes down itself into the global and has a check for the collision.

like image 158
Roman Maksimov Avatar answered Oct 21 '22 17:10

Roman Maksimov