You may have noticed the out directory which contains all of the compiled
JavaScript. There’s about 6 1/2 megabytes worth of JavaScript in there. This may
seem unwieldy but fortunately the ClojureScript compiler generates output
optimized for the Google Closure Compiler. The Google Closure Compiler performs
many optimizations and the most significant for browser-based clients are
minification and dead code elimination.
Let’s remove the REPL modifications we made earlier from src/hello_world/core.cljs:
(ns hello-world.core)
(println "Hello world!")
We can create a release build by setting the appropriate value for the
--optimizations flag. The default optimization level is none, but this time
we want to use all the optimizations provided by both ClojureScript and Google
Closure Compiler - this can be done by specifying advanced. Other valid
options for --optimizations are whitespace and simple but these are less
commonly used:
clj -M -m cljs.main --optimizations advanced -c hello-world.core
java -cp "cljs.jar;src" cljs.main --optimizations advanced -c hello-world.core
This process will take significantly longer which is why we don’t use
this compilation mode for development.
Examine out/main.js, the file size should be around 90K. If you zip
this file you’ll see that it’s around 20K. This is significantly smaller
than a jQuery dependency yet when using ClojureScript you have implicit
dependencies on the entire ClojureScript standard library (10KLOC) and
the Google Closure Library (300KLOC). You can thank dead code
elimination.
You can test that this file still works by running the built in simple web
server via the --serve flag:
clj -M -m cljs.main --serve
java -cp "cljs.jar;src" cljs.main --serve