Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I compile ClojureScript without the Google Closure lib?

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?

like image 498
Dane Avatar asked Apr 15 '13 03:04

Dane


2 Answers

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.

like image 195
Kevin L. Avatar answered Oct 30 '22 01:10

Kevin L.


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 [in project.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.

like image 30
JohnJ Avatar answered Oct 30 '22 02:10

JohnJ