ClojureScript

Add support for Closure defines

Details

  • Type: Enhancement Enhancement
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Completed
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None
  • Patch:
    Code

Description

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}}.

Activity

Hide
Rafal Babinicz added a comment -

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

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

Resources:
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."
[opts]
(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)
compiler-options)))

Show
Rafal Babinicz added a comment - Benefits: Better support for mobile (Android, iOS, iPad) development in ClojureScript. Problem: Please add support for passing "--define" option to compiler.jar. With this we can optimize code performance where only mobile browsers are our target. Resources: 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." [opts] (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) compiler-options)))
Hide
Sergei Lebedev added a comment - - edited

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.

Show
Sergei Lebedev added a comment - - edited 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.
Hide
Sergei Lebedev added a comment -

Attached patch doesn't work with the latest master.

Show
Sergei Lebedev added a comment - Attached patch doesn't work with the latest master.

People

  • Assignee:
    Unassigned
    Reporter:
    John Li
Vote (3)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: