如何使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请求。

为了解决您的问题而不进行修补,您可以添加仅包含以下函数定义的extern文件,该文件应该起作用:

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

关于javascript - Google Closure编译器为新的Web Audio API方法提供了JSC_INEXISTENT_PROPERTY,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37787606/

10-11 14:51