Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the --module option in Closure Compiler to create multiple output files

Tags:

I'm creating a fairly large library of JavaScript, using Closure Compiler for both its wonderful compression as well as the type-checking and warning systems.

I want to create multiple output files though, because the files are loaded asynchronously (and some files are only loaded on-demand).

Poking around the source code, I've found the --module flag, as well as some related flags. The source code says the following about the option:

A javascript module specification. The format is <name>:<num-js-files>[:[<dep>,...][:]]]. Module names must be unique. Each dep is the name of a module that this module depends on. Modules must be listed in dependency order, and js source files must be listed in the corresponding order. Where --module flags occur in relation to --js flags is unimportant

... and that's all I can find. I'd love to learn more about how to use this option, does anyone have any experience here? Alternatively, if there's another way to create multiple output files, I'm all ears.

like image 556
Fortes Avatar asked Jul 08 '10 14:07

Fortes


1 Answers

java -jar compiler.jar ^ --chunk jq:1: --js jquery-1.6.2.js ^ --chunk t:1:jq: --js test.js ^ --compilation_level ADVANCED_OPTIMIZATIONS 

This example will compile out 2 files for you:

  • jq.js
  • t.js

jq.js will be jquery 1.6.2 with advanced minification, and t.js will use that minified version of JQuery properly.

I wish there was a JavaFiddle I could post this to to demonstrate it.


Older version

This original answer was for an older version of Closure Compiler. I've left it intact below in case you're in an environment that needs to keep the older version in place.

How to handle multiple output files, aka modules: http://groups.google.com/group/closure-compiler-discuss/browse_thread/thread/ec7f6809b19b019e/25a94f3994173840

Copy/pasting:

java -jar Build\Tools\compiler.jar ^  --compilation_level=ADVANCED_OPTIMIZATIONS ^  --externs Build\jQuery.externs.js ^  --js Build\Output\Compiling.js ^  --js Script/Themes.lang.js ^  --js Script/Themes.js ^  --module Core:3 ^  --js UI/ThemeChooser/ThemeChooser_en.htm.js ^  --js UI/ThemeChooser/ThemeChooser.js ^  --module UI_ThemeChooser:2:Core ^  --js UI/VerticalTabs/VerticalTabs_en.htm.js ^  --js UI/VerticalTabs/VerticalTabs.js ^  --module UI_VerticalTabs:2:Core ^  --js Pager/Pager_en.htm.js ^  --js Pager/jquery.Pager.js ^  --js Pager/Pager.js ^  --module Pager:3:VerticalTabs ^  --module_output_path_prefix .\Compiled\  

And as he notes, --js_output_file is irrelevant when outputting modules.

Note: Apparently the Closure Compiler has changed the arg "--module" to "--chunk". An editor suggested the change; for the newer version I kept the change, for the older version I kept the older argument name, since there are always people out there using older versions of build tools, and that kind of small breaking change can really screw ya up.

like image 61
Chris Moschini Avatar answered Oct 05 '22 02:10

Chris Moschini