Javascript 谷歌关闭:依赖项错误

Javascript 谷歌关闭:依赖项错误,javascript,google-closure-compiler,Javascript,Google Closure Compiler,自从谷歌关闭3天以来,我遇到了一个问题,因为依赖项的顺序错误,我得到了以下结果: main.js (function () { goog.provide('MYENGINE.Core'); goog.require('MYENGINE.engines.GraphicEngine'); goog.require('MYENGINE.engines.PhysicEngine'); goog.require('MYENGINE.engines.AudioEngine'); goog.require('

自从谷歌关闭3天以来,我遇到了一个问题,因为依赖项的顺序错误,我得到了以下结果:

main.js

(function () {
goog.provide('MYENGINE.Core');
goog.require('MYENGINE.engines.GraphicEngine');
goog.require('MYENGINE.engines.PhysicEngine');
goog.require('MYENGINE.engines.AudioEngine');
goog.require('MYENGINE.engines.GameEngine');

/*********************************
* @constructor
*
**********************************/
ENGINE.Core = function() 
{

};

})();
以及此代码(使用正确的名称):

我不知道为什么,但当我编译这个时,“MYENGINE.engines.graphicsengine”首先出现在MYENGINE.Core之前。 因此,当我运行页面时,我得到了一个错误:*uncaughtreferenceerror:MYENGINE没有定义*

我使用以下代码编译项目:

../extlib/closure/closure/bin/build/closurebuilder.py \
--root=../extlib/closure/ \
--root=../src \
--namespace="MYENGINE.Core" \
--output_mode=compiled \
--compiler_jar=compiler.jar \
> MYENGINE_min.js
在我的“MYENGINE_min.js”中,我可以在核心或初始名称空间(MYENGINE)之前找到GraphicEngine的创建,我忘了做什么了吗


非常感谢你的帮助

闭包的设计不需要将每个模块封装在匿名函数中。 如果删除匿名函数包装器,错误应该会消失。例如,main.js变成:

goog.provide('MYENGINE.Core');

goog.require('MYENGINE.engines.GraphicEngine');
goog.require('MYENGINE.engines.PhysicEngine');
goog.require('MYENGINE.engines.AudioEngine');
goog.require('MYENGINE.engines.GameEngine');

/**
 * @constructor
 */
MYENGINE.Core = function() 
{

};
你还问:

我不知道为什么,但当我编译这个时,“MYENGINE.engines.graphicsengine”首先出现在MYENGINE.Core之前

MYENGINE.Core
行中:

goog.require('MYENGINE.engines.GraphicEngine');
表示
MYENGINE.Core
依赖于
MYENGINE.engines.graphicsengine
。因此,
MYENGINE.engines.graphicsengine
必须首先出现,以便在从
MYENGINE.Core
调用时对其进行定义。例如,闭包的
base.js
通常是闭包生成器生成的列表中的第一个源,因为所有其他闭包库源都依赖于
base.js
来引导库

如果您希望将已编译的JavaScript封装在匿名函数中,以防止名称冲突,则闭包编译器提供以下标志:

--output_wrapper Interpolate output into this string at the place denoted by the marker token %output%. 此外,将编译器警告级别设置为详细将有助于在编译时捕获其他错误:

--compiler_flags="--warning_level=VERBOSE"
新的构建命令将是:

../extlib/closure/closure/bin/build/closurebuilder.py \ --root=../extlib/closure/ \ --root=../src \ --namespace="MYENGINE.Core" \ --output_mode=compiled \ --compiler_jar=compiler.jar \ --compiler_flags="--output_wrapper=(function(){%output%})();" \ --compiler_flags="--warning_level=VERBOSE" \ > MYENGINE_min.js ../extlib/closure/closure/bin/build/closurebuilder.py\ --root=../extlib/closure/\ --根=../src\ --namespace=“MYENGINE.Core”\ --输出模式=已编译\ --compiler\u jar=compiler.jar\ --编译器_flags=“--output_wrapper=(函数(){%output%})(;”\ --编译器_flags=“--warning_level=VERBOSE”\ >MYENGINE_min.js 你可以使用谷歌示波器

goog.scope(函数(){
//代码在这里

});非常感谢没有匿名函数包装器会更好:D。
--compiler_flags="--warning_level=VERBOSE"
../extlib/closure/closure/bin/build/closurebuilder.py \ --root=../extlib/closure/ \ --root=../src \ --namespace="MYENGINE.Core" \ --output_mode=compiled \ --compiler_jar=compiler.jar \ --compiler_flags="--output_wrapper=(function(){%output%})();" \ --compiler_flags="--warning_level=VERBOSE" \ > MYENGINE_min.js