在 sapui5 项目中包含 babel polyfill 的最佳方法是什么?

What would be the best way to include babel polyfill in a sapui5 project?

我们使用 grunt 作为任务运行器,并且我们使用与 openui5 开发人员推荐和使用的相同的 grunt 设置。

https://github.com/SAP/grunt-openui5

https://github.com/SAP/openui5/blob/master/docs/tools.md

我们希望在构建应用程序时将 babel-polyfill 添加到项目中(以便我们可以使用最新的 javascript 功能) 就像你如何使用例如:webpack 一样。 提前感谢您的回答。

official documentation of babel polyfill 中描述了您拥有的选项。当我认为它们每个都应该使用时,我会描述一下。

这取决于您之后实际使用该应用程序的方式:它是一个独立的 UI5 应用程序(即您 运行 它直接来自 HTML 文件)还是一个 Fiori 应用程序(即您运行 来自 Fiori Launchpad)?

如果它是一个独立的应用程序,那么包含 babel-polyfill 的最简单和最非侵入性的方法是通过 HTML 文件简单地将 dist/polyfill.js 包含在 HTML 文件中=13=]标签。

如果它是一个 Fiori 应用程序,那么您唯一的机会就是将 polyfill 添加到您的脚本文件之一。它必须先 运行 ,所以我会将它添加到您在构建阶段获得的 Component-preload.js 文件中。要使用 g运行t 执行此 "prepend" 操作,您可以使用 grunt-contrib-concat 插件。

另一种可能性是简单地 require 组件模块中的 polyfill。您可以在组件文件中的 sap.ui.define 调用中执行此操作:

// the oPolyfill parameter most likely will have nothing in it
// as the polyfill most surely does not use the SAP AMD system
sap.ui.define(["./path/to/polyfill"], function(oPolyfill) {
    //...
}); 

使用此策略时,生成的 Component-preload.js 文件还将包含 polyfill,并且 polyfill 将在您的组件代码开始之前加载 运行ning。

请记住,如果您在 Fiori Launchpad 环境中使用这些方法中的任何一种,polyfill 也会影响其他 Fiori 应用程序。这是不可避免的,因为 polyfill 修改了 babel 文档中指定的全局变量:

The polyfill adds to the global scope as well as native prototypes like String in order to do this.