使用带有google-closure-compiler的外部库

Use external library with google-closure-compiler

本文关键字:外部 google-closure-compiler      更新时间:2023-09-26

我正在尝试使用谷歌闭包编译器构建我的项目。遗憾的是,我的项目使用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'到我的构建脚本,错误总是在这里。

对于那些在ADVANCED模式下与Closure-Compiler不兼容的库,你需要外部定义。这在官方文档中有介绍。

extern提供给编译器时带有--externs标志(而不是——js标志)。虽然将外部库源代码作为外部文件提供可能很诱人,但这几乎总是产生糟糕的结果。

有关创建外部的详细信息,请参见https://stackoverflow.com/a/20101173/1211524