简体   繁体   中英

Use external library with google-closure-compiler

I'm trying to build my project using google closure compiler. Sadly, my project use Box2D: a physic library without google closure so I've only got a minified file with his library's functions.

How can I build my project without errors?

Here is my build command line:

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

Here are the errors I've got:

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();

I've tried to add --js='../libs/Box2D-min.js' to my build script, errors are always here.

You will need extern definitions for any libraries whose source is not compatible with Closure-Compiler in ADVANCED mode. This is covered in the official documentation .

Externs are provided to the compiler with the --externs flag (rather than the --js flag). While it may be tempting to provide the external library source as an extern, this almost always produces poor results.

For specific details on authoring an extern, see https://stackoverflow.com/a/20101173/1211524

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM