Add targets option to @babel/plugin-transform-runtime#11572
Add targets option to @babel/plugin-transform-runtime#11572TomerAberbach wants to merge 12 commits intobabel:masterfrom TomerAberbach:master
Conversation
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit d71fef1:
|
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/23389/ |
developit
left a comment
There was a problem hiding this comment.
LGTM.
One additional useful thing would be the ability to prevent the inclusion of a given polyfill (like JSON.stringify/json/stringify).
...orm-runtime/test/fixtures/runtime-corejs3/built-in-static-methods-esmodules-target/output.js
Show resolved
Hide resolved
I think that's a good idea. We could mirror |
Co-authored-by: Huáng Jùnliàng <jlhwung@gmail.com>
|
@JLHwung is there any interest in this PR anymore? If not, then I can close it |
|
@TomerAberbach As a side effect of #12845, now this plugin supports const input = String.raw`
Array.from(3);
foo.flatMap(fn);
`;
const out = babel.transform(input, {
configFile: false,
sourceType: "module",
targets: "chrome 60",
plugins: [[transformRuntime, { corejs: 3 }]],
});
console.log(out.code);logs import _flatMapInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/flat-map";
Array.from(3);
_flatMapInstanceProperty(foo).call(foo, fn);while const input = String.raw`
Array.from(3);
foo.flatMap(fn);
`;
const out = babel.transform(input, {
configFile: false,
sourceType: "module",
targets: "chrome 40",
plugins: [[transformRuntime, { corejs: 3 }]],
});
console.log(out.code);logs import _Array$from from "@babel/runtime-corejs3/core-js-stable/array/from";
import _flatMapInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/flat-map";
_Array$from(3);
_flatMapInstanceProperty(foo).call(foo, fn);Thanks for the PR anyway! |
@babel/plugin-transform-runtimeis great because it can be used to polyfill features without polluting the global namespace (i.e. ponyfills). This is the ideal choice for libraries because they shouldn't pollute the global namespace. However,@babel/plugin-transform-runtimedoes not support only polyfilling the features required by the target browser/node versions like@babel/preset-envdoes. This results in unnecessarily large bundles containing unused polyfills.This pull request adds a
targetsoption to@babel/plugin-transform-runtimethat behaves similarly to@babel/preset-env'stargetsfield. It also addsconfigPathandignoreBrowserslistoptions like@babel/preset-envhas.With regards to dependencies,
@babel/compat-data,@babel/helper-compilation-targets, andcore-js-compatdependencies were added to@babel/plugin-transform-runtime.Lastly, I'm aware that Babel is rethinking its polyfilling story so this change may become obsolete in the future. However, I think people have been waiting a long time for this feature and it could be a while before Babel's new polyfilling approach is ready; so I think this feature is useful as a short-term solution.
Looking forward to your feedback!