使用带有google-closure-compiler

时间:2015-08-03 09:40:22

标签: javascript google-closure-compiler google-closure-library

我正在尝试使用谷歌闭包编译器构建我的项目。可悲的是,我的项目使用了Box2D:一个没有谷歌关闭的物理库,所以我只有一个缩小的文件,包含他的库的功能。

如何构建我的项目而不出错?

这是我的构建命令行:

java -jar ../libs/closure-compiler.jar \
    --compilation_level SIMPLE_OPTIMIZATIONS \
    --language_in=ECMASCRIPT5_STRICT \
    --warning_level VERBOSE \
    --only_closure_dependencies\
    --summary_detail_level 3 \
    --process_closure_primitives true \
    --closure_entry_point="MyProject.Main"\
    --js='../src/**.js' \
    --js='../libs/closure-library/**.js' \
    --js='!../libs/closure-library/**_test.js' \
    --js='!../libs/closure-library/**_test.js' \
    --js_output_file Project.js

以下是我遇到的错误:

ERROR - variable Box2D is undeclared
        var col = Box2D.wrapPointer(color, Box2D.b2Color);

ERROR - variable b2_kinematicBody is undeclared
        this.instance.SetType(b2_kinematicBody);        

ERROR - variable b2Vec2 is undeclared
    this.instance.SetLinearVelocity(new b2Vec2(x, y));

ERROR - variable b2BodyDef is undeclared
    var definition = new b2BodyDef();

ERROR - variable b2FixtureDef is undeclared
    var fixture = new b2FixtureDef();

ERROR - variable b2CircleShape is undeclared
    var shape = new b2CircleShape();

我尝试将--js='../libs/Box2D-min.js'添加到我的构建脚本中,错误总是在这里。

1 个答案:

答案 0 :(得分:1)

对于任何源与ADVANCED模式下的Closure-Compiler不兼容的库,您将需要extern定义。这包含在official documentation

使用--externs标志(而不是-js标志)向编译器提供外部程序。虽然将外部库源作为外部提供可能很诱人,但这几乎总会产生不良结果。

有关创作extern的具体详情,请参阅https://stackoverflow.com/a/20101173/1211524

相关问题