Replies: 4 comments 13 replies
-
If you want to inject polyfills based on your targets, I recomment compiling Depending on how you are running Babel, usually this just means not excluding |
Beta Was this translation helpful? Give feedback.
-
@nicolo-ribaudo For your reply: I tried this:
Then I complied with source code: var a = async () => {
}
const b = {
c: {
d: 2
}
};
console.log(b?.c?.e); The output is: import _regeneratorRuntime from "@babel/runtime/regenerator";
import "regenerator-runtime/runtime";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
var _b$c;
var a = /*#__PURE__*/function () {
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
case "end":
return _context.stop();
}
}
}, _callee);
}));
return function a() {
return _ref.apply(this, arguments);
};
}();
var b = {
c: {
d: 2
}
};
console.log(b === null || b === void 0 ? void 0 : (_b$c = b.c) === null || _b$c === void 0 ? void 0 : _b$c.e); As we know |
Beta Was this translation helpful? Give feedback.
-
I too am wondering sometimes what is the best way to set up core-js 3 with the combination of preset-env and babel-runtime, babel-transform-runtime. I feel like this should just be it's own preset or something. |
Beta Was this translation helpful? Give feedback.
-
What is the point of using something like core-js with @babel/core. Don't they both do the same thing conceptually? |
Beta Was this translation helpful? Give feedback.
-
When I use core-js with babel, I want to try my best to reduce the output file size. So I will configure babel to import only some polyfills. First, I use
@babel/preset-env
withuseBuiltIns: usage
like this:Thanks to core-js and @babel/preset-env, this works well. But I want to use
@babel/runtime
to import some common helper functions to save more output file size:However, there is a problem: when I use
corejs: false
in@babel/plugin-transform-runtime
, some helper functions may need polyfill which I don't import withuseBuiltIns: usage
. For example, when I import a helper function :import '@babel/runtime/helpers/asyncToGenerator';
. With the source code of asyncToGenerator, we can see that this helper needs a Promise polyfill:However, If my source code doesn't have a Promise function, then the promise polyfill won't be imported. So the runtime function will be broken is some old browsers.
Another way, if I use core-js with @babel/runtime instead of preset-env:
As we know, @babel/runtime-corejs3 will not only polyfill runtime helpers, but also polyfill my source code. But there is also a big problem: @babel/runtime-corejs3 will ignore the target configure. So it will import all possible polyfills.
So, what's the best practice of babel and core-js? Any help will be appreciated!
Beta Was this translation helpful? Give feedback.
All reactions