What are all these babel dependencies? What are each of them for and how do they differ? Which one should I use for my nodejs web app?
I have looked on the API website but is there any guide that puts these into simple human English?
I've been trying to set up some mocha tests and I stumbled upon that babel-core/register is from a different package (babel-core) than babel-register.... so what's up with that?
I've been using core-js with Babel for years in fairly large-scale projects where the bloat it produces has been somewhat disguised by the fact bundles are pretty big regardless. The pros of everything just working for a target environment outweigh any cons, and I think this is where core-js really shines.
However, I've recently started building some minimal NPM packages and it's been an eye-opener for me. So much so that I'm questioning whether core-js is fundamentally broken or if I'm just not fully understanding its purpose or how to leverage it properly.
Assume I'm targeting IE11 specifically and take the following example:
function trimmer(value) {
return value.trim();
}
There is nothing fancy here; String.prototype.trim() has been supported since IE10.
Running this through Babel with core-js enabled appends require("core-js/modules/es.string.trim.js"); which then creates a chain reaction of dependency bloat. In fact if you use @rollup/plugin-commonjs to include these files in the bundle you end up with nearly 600 lines of code. Let that sink in. That's a 60000% increase for something that didn't even need polyfilling.
Even if a polyfill was required, why the bloat? There's a one-liner on MDN which probably covers 99.9% of cases.
Perhaps this is a bug, so let's move on to another example:
const test = {
...{
foo: 'bar'
}
};Spread in object literals is not supported in IE11. That's fine.
If you forego core-js and simply use preset-env then a small block of helper code is injected to polyfill Object.assign(). The same is true when using TSC and compiling to <= ES5. You can take either of these outputs and run them in IE11 with no issues.
If you enable core-js the compiled output grows to over 1300 lines of code.
I'm clearly missing something as there is no way this is even remotely acceptable.
I'm wondering if anyone out there is/was in a similar position to me or can offer some advice.
The opinion I'm leaning toward at the moment is that core-js is great for large-scale projects where the overhead is mostly negated, and you have the peace of mind knowing that everything will just work. For smaller projects, if you're targeting a legacy environment then be conscious of the code you're writing (e.g. use a traditional for loop rather than for...of if it means avoiding 10 extra lines of polyfill code) and include case-specific polyfills manually if there's no other choice.
I'm 100% with you! I've been maintaining a list of core-js modules not to polyfill for the projects I manage. These can be disabled using the `exclude` option in babel.config.js. Feel free to use and/or provide corrections if you discover any additional ones!
We've gone down this rabbit hole with Fusion.js. The TL;DR: is core.js aims to be standard-compliant, which means it'll often pull in a lot of code to deal with obscure corner cases like dealing w/ Symbols.
You can get around this a few different ways:
-
configure webpack/babel to not polyfill certain features (meaning, you lose support for old browsers)
-
provide custom polyfills (meaning you still have support for old browsers, but your implementation may be susceptible to brokenness of obscure edge cases)
-
not use core.js and instead expose a
modulefield in package.json (orbrowser, etc, depending on how the package is consumed) and let the consumer's build pipeline figure out what to do wrt module transpilation (meaning you mostly provide source code, and the consumer has to decide how much standards compliance they want vs how much they can get away with simpler polyfills)
The last option has the most potential for performance gains because a consumer may choose to conditionally transpile depending on whether the user agent is a modern browser, meaning e.g. Chrome gets the terse and sweet ES6 code whereas a playstation browser would get the ES5 bundle (this is what we do w/ Fusion.js). But mind that this approach requires some more infrastructure on the consumer side. We've built it into our framework, and I'm aware of a few others that do similar stuff (Meteor, for example), but not all frameworks do.
Note also that the last option is not mutually exclusive with transpiling options. You can still provide a transpiled main field in package.json to support bundler setups that don't support the module field.
I'm currently evaluating different TypeScript transpilers for a new project I'm working on and am trying to decide between Babel, swc, and Sucrase. I’ve read up on each, but I would love to hear from others who have firsthand experience with these tools.
-
Performance: Which of these transpilers offers the best build performance? I'm particularly interested in faster compile times during development.
-
Ease of Use: Is there a significant difference in the ease of setup and configuration between these transpilers?
-
Feature Support: Considering Babel's comprehensive support for experimental features versus the more streamlined approaches of swc and Sucrase, how does this affect your day-to-day development? Do swc or Sucrase miss out on any crucial features?
-
Community and Support: Which of these tools has a more active community and better support? Have you encountered any issues, and how responsive were the maintainers?
Any insights, benchmarks, or personal experiences you could share would be greatly appreciated. Looking forward to your advice to help me make an informed decision.
Thank you!
I've looked into this topic for some time and it's confusing.
I only care about "modern browsers". And it seems like vite.js premise is - use esbuild and you dont need the core-js/babel - is that correct?
But couldn't there be a possibility that feature is supported in all browsers except like safari or firefox - and so even if browser is "modern" - it still would require a polyfill? so to me it seems like babel/core-js is still kind of good to have with vite.js?
What's your take on it? do you use babel with vite.js?
» npm install @babel/core
babel-core is the API. For v5 the babel package is the CLI and depends on babel-core. For v6, the babel-cli package is the CLI (the CLI bin command is still babel though) and the babel package doesn't do anything. babel-runtime I guess is just the runtime (polyfill and helpers) to support code that's already been transformed.
TL;DR The things to compare here are:
- babel (use for 5.x.x) vs babel-cli+babel-core (pick one for 6.x.x)
- babel-polyfill (use for non-libraries) vs babel-runtime+babel-plugin-transform-runtime (use for libraries)
From https://babeljs.io/blog/2015/10/31/setting-up-babel-6:
The babel package is no more. Previously, it was the entire compiler and all the transforms plus a bunch of CLI tools, but this lead to unnecessarily large downloads and was a bit confusing. Now we’ve split it up into two separate packages: babel-cli and babel-core.
npm install --global babel-cli
or
npm install --save-dev babel-core
If you want to use Babel from the CLI you can install babel-cli or if you want to use the Node API you can install babel-core.
babel-runtime just allows polyfills that don't pollute the global space, unlike babel-polyfill which pollutes your global space. From http://babeljs.io/docs/plugins/transform-runtime/:
[babel-runtime] automatically polyfills your code without polluting globals. (This plugin is recommended in a library/tool)
If you use babel-runtime, you should also
npm install --save-dev babel-plugin-transform-runtime
In most cases, you should install babel-plugin-transform-runtime as a development dependency (with --save-dev) and babel-runtime as a production dependency (with --save).
The transformation plugin is typically used only in development, but the runtime itself will be depended on by your deployed/published code.
Also, babel-runtime+babel-plugin-transform-runtime and babel-polyfill are generally mutually exclusive--meaning you should only use one or the other. From a comment here http://jamesknelson.com/the-six-things-you-need-to-know-about-babel-6/:
You should be using either babel-polyfill or babel-runtime. They are mutually exclusive—unless of course you know what you are doing. But they are essentially the same thing. These are just helpers. babel-polyfill achieves the same goal by mutating globals whereas babel-runtime does so modularly. Unless you are developing a library, I’d recommend you use the polyfill.
Just considering buying Babbel after I finish the French and the Danish courses on Duolingo, but it's very expensive so I want to hear from people that use it first.
I’m using babel (with preset-env) for “modern” es2015+ support with node 8 LTS.
My app has two entry points - babel-register-based, and another which calls the transpiled script in a dist folder. I require(“core-js/stable”) first in both. I’ve also set the following preset-env flags in my babel config useBuiltIns: “usage, corejs: 3”
Is this needed in a node environment? If so, why? Been running in circles on this one
» npm install babel-core