Frage

I'm in the process of migrating a project from jQuery to Closure. I have some code that is only half-migrated that I would like to compile. The uncompiled source works fine. I want to know what compile command to compile it using SIMPLE_OPTIMIZATIONS.

The compile command for the original jQuery-based code was this:

java -jar ~/closure/closure-compiler/build/compiler.jar \
  --js ~/Sites/mysite/js/bc_school6_2.js \
  --js ~/Sites/js_common/bc.job_school.js \
  --js ~/Sites/js_common/bc_help.js \
  --js ~/Sites/js_common/validation.js \
  --js ~/Sites/js_common/md5.js \
  --js ~/Sites/js_common/chosen.jquery.js \
  --js ~/Sites/js_common/jquery.reveal.js \
  --js ~/Sites/js_common/printArea.js 
  > ~/Sites/mysite/js-minified/bc_school6_2s.js 

The SIMPLE_OPTIMIZATIONS compile command for the source when it is fully migrated will be this (although the fully-migrated code will use ADVANCED_OPTIMIZATIONS):

closure-library/closure/bin/build/closurebuilder.py \
  --root=closure-library/ \
  --root=closure-templates/javascript/ \
  --root=bc/ \
  --namespace="bc.bc_school6_2" \
  --output_mode=compiled \
  --compiler_jar=closure-compiler/build/compiler.jar \
  --compiler_flags="--compilation_level=SIMPLE_OPTIMIZATIONS" \
  > ~/Sites/mysite/js-minified/bc_school6_2s.js

At present, the namespace is not properly set up in the source, so the latter compile process won't work properly.

Is it possible to compile the source using the Google Closure library, but then add in all my jQuery files from js_common folder? Can I do it in one compile command, or if not, can I compile my goog code, and then incorporate the jQuery material?

Thanks.

War es hilfreich?

Lösung

You can compile your goog code and then include the jQuery code as external code. This method will also let you compile your code in ADVANCED MODE and still be able to use jQuery in it's original form.

To do this, you will have to use a .js file that contains all of your extern declarations. Then you use the --externs flag to tell the closure compiler where to look for externs. See the sample usage below:

java -jar compiler.jar --compilation_level ADVANCED_OPTIMIZATIONS \
     --js makeallnotes.js --externs extern1.js --externs extern2.js

To find out more about how to declare externs, see this tutorial. Basically, they are used to tell the closure compiler about an external API or library you are using.

There is actually a jQuery extern file included with the Google Closure source code. Find the version of jQuery you are using on this page.

On a side note, I'd look into using Plovr to build your Closure Project. It lets you use a configuration a file to set all of your build parameters and will save a lot of time if you are building your code often.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top