<< Back to previous view

[CLJS-77] Add support for Closure defines Created: 18/Sep/11  Updated: 03/Dec/13  Resolved: 03/Dec/13

Status: Closed
Project: ClojureScript
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Enhancement Priority: Major
Reporter: John Li Assignee: Unassigned
Resolution: Completed Votes: 3
Labels: None

Attachments: File define.diff    
Patch: Code


The Closure compiler has "--define" which allows you to set some library values at compile time. The settable values are annotated with "@define": https://code.google.com/closure/compiler/docs/js-for-compiler.html

The particular values I was interested in were goog.dom.ASSUME_{QUIRKS,STANDARDS}_MODE because it would save some code and runtime computation if the rendering mode is known at compile time. (It turns out to be very minor for my tiny test program, but probably more substantial for things that use more of the Closure library.) goog.DEBUG also seems useful to tweak.

The attached patch supports a new compile option :define, along with a map from keys/strings to values. The Closure compiler takes a -define option like: "-define='goog.DEBUG=false'". In ClojureScript, this would be {:define {"goog.DEBUG" false}} or {:define {:goog.DEBUG false}}.

Comment by Rafal Babinicz [ 26/Jan/12 11:26 AM ]

Better support for mobile (Android, iOS, iPad) development in ClojureScript.

Please add support for passing "--define" option to compiler.jar.
With this we can optimize code performance where only mobile browsers are our target.

goog.userAgent global props: http://closure-library.googlecode.com/svn/docs/closure_goog_useragent_useragent.js.html
goog.userAgent.product global props: http://closure-library.googlecode.com/svn/docs/closure_goog_useragent_product.js.html
"java -jar compiler.jar --help" for all available options
Google Closure Definitive Guide at Google Books: http://books.google.pl/books?id=p7uyWPcVGZsC

clojurescript/src/clj/cljs/closure.clj: functions "make-options" and "set-options"

"TODO: Add any other options that we would like to support."
(defn set-options
"TODO: Add any other options that we would like to support."
[opts ^CompilerOptions compiler-options]
(when (contains? opts :pretty-print)
(set! (.prettyPrint compiler-options) (:pretty-print opts)))
(when (contains? opts :print-input-delimiter)
(set! (.printInputDelimiter compiler-options)
(:print-input-delimiter opts))))
(defn make-options
"Create a CompilerOptions object and set options from opts map."
(let [level (case (:optimizations opts)
:advanced CompilationLevel/ADVANCED_OPTIMIZATIONS
:whitespace CompilationLevel/WHITESPACE_ONLY
:simple CompilationLevel/SIMPLE_OPTIMIZATIONS)
compiler-options (doto (CompilerOptions.)
(.setCodingConvention (ClosureCodingConvention.)))]
(do (.setOptionsForCompilationLevel level compiler-options)
(set-options opts compiler-options)

Comment by Sergei Lebedev [ 31/May/12 3:22 PM ]

One more usecase, I've stumbled upon, while wrapping goog.i18n, here's a line from goog/base.js:59:

 * @define {string} LOCALE defines the locale being used for compilation. It is
 * used to select locale specific data to be compiled in js binary. BUILD rule
 * can specify this value by "--define goog.LOCALE=<locale_name>" as JSCompiler
 * option.

So, without --define it's simply impossible to create i18n apps with ClojureScript and Closure Library.

Comment by Sergei Lebedev [ 31/May/12 4:32 PM ]

Attached patch doesn't work with the latest master.

Comment by David Nolen [ 03/Dec/13 9:04 PM ]

fixed, https://github.com/clojure/clojurescript/commit/cb7e97f13ae6a03086f5d96ba58e5f3d5cba7dc3

Generated at Mon Feb 27 19:14:02 CST 2017 using JIRA 4.4#649-r158309.