Search code examples
javascriptgoogle-closure-compilergoogle-closuregoogle-closure-library

Build a library using Closure Library


I'm trying to "build" my project using Closure Library; Sadly, after many tests I can't build something without problems. My project is a library, so I haven't got an entry point or anything like that, most of the code is made of objects and functions for the user.

I've got a project like that:

 - build
      - build.sh
      - compiler.jar
 - libs
      - closure-library
 - src

My build.sh file:

java -jar compiler.jar --compilation_level SIMPLE_OPTIMIZATIONS --js_output_file out.js `find ../src/ -name '*.js'`

With this command line I've got the error:goog.require('goog.vec.Vec2');; So I think I need to include google closure library in this line, right?

So, I've tried to change my build.sh to something like that (added closure-library folder):

java -jar compiler.jar --compilation_level SIMPLE_OPTIMIZATIONS --js_output_file out.js `find ../src/ ../libs/closure-library/closure/ -name '*.js'`

With this script, I've got many error from the closure library like that:

../libs/closure-library/closure/goog/i18n/datetimeformat_test.js:572: ERROR - This style of octal literal is not supported in strict mode.
  var date = new Date(Date.UTC(2015, 11 - 1, 01, 11, 0, 1));

And my resulting file (out.js) haven't got all my library's functions. I'm not sure to understand where is the problem.

  • Did I really have to include google closure in the build part?
  • I'm working on a Javascript library so I haven't got an entry point and all the code must be included in the resulting file. So, how can I do that?

Thanks for your time!

Edit: I've tried something different: removing all lines like "goog.require('goog.vec.Mat4');" from my library. The build is done successfully but my simulation didn't work anymore: Cannot read property 'Mat4' of undefined


Solution

  • The functionality you are looking for is documented on the GitHub wiki under Manage Closure Dependencies

    Determining Dependencies

    1. Your Library Source Contains goog.provide Statements

    In this case you would use --only_closure_dependencies combined with --closure_entry_point flags. Your "entry" points are any class in your library from which you want to calculate dependencies. You can have multiple entry points.

    2. Your Library Source Does Not Contain goog.provide Statements

    Use the --manage_closure_dependencies flag. This instructs the compiler to include any JS file which does not contain a goog.provide statement in the output and to calculate all needed dependencies based on the goog.require statements in those files.

    Providing Files To The Compiler

    Closure-compiler's --js input flags can specify minimatch glob style patterns and this is the preferred method for providing Closure-library files as input. If you are using the --manage_closure_dependencies option, you must exclude the Closure-library test files.

    Example:

    java -jar compiler.jar --compilation_level=SIMPLE_OPTIMIZATIONS
        --js_output_file=out.js
        --manage_closure_dependencies
        --js='../src/**.js'
        --js='../libs/closure-library/**.js'
        --js='!../libs/closure-library/**_test.js'