Skip to main content
 首页 » 编程设计

babeljs之在*不*开发库时,使用 babel-runtime 和 babel-polyfill 之间有什么实际区别吗 (例如网络应用程序)

2025年01月19日19mfrbuaa

这一切都在标题中,真的。

在 Babel 文档中,page describing babel-runtime 上有以下行

Another purpose of this transformer is to create a sandboxed environment for your code. Built-ins such as Promise, Set and Map are aliased to core-js so you can use them seamlessly without having to require a globally polluting polyfill.



polyfill就是这样,一个单独的 JavaScript 文件,其中包含了一些缺失的东西。

我已经用我的构建工具(webpack)测试了 polyfill 与使用 babel-runtime 的对比,当我使用 babel-runtime 时,我的文件稍微小了一点。

我不是在开发库或插件,只是一个 Web 应用程序,也不关心全局范围被污染。知道了这一点,除了最终文件大小略小之外,在 polyfill 上使用运行时还有其他实际好处或要点吗?

请您参考如下方法:

如果你不关心污染全局作用域,polyfill 是更好的选择:运行时不适用于实例方法,例如 [0, 1, 2].includes(1) ,而 polyfill 会。

两者的主要区别在于polyfill污染全局作用域,使用实例方法,而runtime不污染全局作用域,不使用实例方法。

polyfill 也不允许你在你的代码中包含两个单独的版本。如果在您的代码中的某处需要/导入两个单独的 polyfill,这只是一个问题。