Google Closure Compiler 为新的 Web Audio API 方法提供 JSC_INEXISTENT_PROPERTY

Google Closure Compiler gives JSC_INEXISTENT_PROPERTY for new Web Audio API methods

问题

如何让 Google Closure Compiler 识别已支持的 API 的新方法(在本例中为 Web Audio API)或至少阻止它重命名他们(最好在我的代码中使用 annotations)?

问题

我在 AudioContext 中使用的方法无法被 Google Closure 编译器识别(但)。例如 AudioContext.suspendAudioContext.resume:

var ctx = new AudioContext;
ctx.suspend();
...
ctx.resume();

闭包编译器给了我“JSC_INEXISTENT_PROPERTY: Property X never defined on AudioContext...”,在某些地方它继续将方法名称重命名为较短的名称。比如上面的代码变成:

var v=new AudioContext;v.a(); ... ;v.resume();

这里,v.a()应该是v.suspend(),所以生成的代码显然不行。

根本原因是 Web Audio API definition in the closure compiler 不是最新的,与其修补闭包编译器,我更想找到一个更通用的解决方法。

你至少应该打开一个 Issue 或者更好的 Pull request。

要在不打补丁的情况下解决您的问题,您可以添加一个仅包含这些函数定义的外部文件,它应该可以工作:

AudioContext_fix_externs.js

/**
 * @return {Promise}
 * @see https://developer.mozilla.org/fr/docs/Web/API/AudioContext/suspend
 */
AudioContext.prototype.suspend = function(){};

要使用该外部文件:

java -jar compiler.jar --externs AudioContext_fix_externs.js ...

https://developers.google.com/closure/compiler/docs/api-tutorial3#howto-app

打开一个问题:https://github.com/google/closure-compiler/issues/new