I'm a huge fan of Clojure and ClojureScript, and I would generally prefer to use ClojureScript over other alternatives for my projects, but one thing that sometimes holds me back from using it, particularly on smaller projects, is the ~80kb added by the inclusion of the Google Closure library in the generated javascript, even when I make no use of the apis in my code.
Is there any way to compile ClojureScript that avoids this extra weight?
The extra size isn't coming from the Google Closure library---if you have advanced optimizations turned on the Closure compiler will remove any Closure library code that you're not using from the final JavaScript. Rather, the JavaScript seems big because there's a whole Clojure runtime in there implementing things like lazy seqs, promises, and everything else.
Correction: as Zubair points out, the step below disables the Google Closure optimization but does NOT eliminate Google Closure code from the final JavaScript. You should choose advanced optimization to eliminate unused JavaScript as the other answer suggests.
In ClojureScript: Up and Running, the authors explain how to disable the Google Closure step:
With an
:optimizations
value of:none
[inproject.clj
], the Google Closure Compiler will not be invoked at all, and the build will write out the JavaScript produced by the ClojureScript compiler directly. This mode is useful for development and debugging. However, the JavaScript output will be split across many individual files, requiring slightly different handling in a browser [...]
Note that this may or may not reduce the size of the produced JavaScript, since Google Closure does a fair bit of work to strip out anything your code doesn't specifically invoke. It's probably worth playing around with the various values of :optimizations
(:none
, :whitespace
, :simple
and :advanced
) and seeing how big the resulting JavaScript is in each case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With