goog-define

macrosince v1.7.48Edit
(goog-define sym default)

Use instead of def when you want to configure its value at compile-time with the :closure-defines compiler option. Must be a string, number, or boolean.


Details:

You can use compile-time constants to signal code to remove from your build, with :optimizations :advanced.

To make this work for booleans, the compiler needs some explicit hinting:

(goog-define ^boolean DEBUG true)
;;           ^^^^^^^^ mark as boolean to allow compiler to optimize

(when DEBUG
  (println "Hello World")) ;; <-- removed from build when DEBUG is false

To make this work for strings, the compiler needs an explicit identical? comparison:

(goog-define LOCALE "en")

(def messages
  (cond
    (identical? LOCALE "nl") i18n.nl/messages  ;; <-- removed from build when unused
    (identical? LOCALE "fr") i18n.fr/messages  ;; <-- removed from build when unused
    :else i18n.en/messages))

See Also:


Source docstring:
Defines a var using `goog.define`. Passed default value must be
string, number or boolean.

Default value can be overridden at compile time using the
compiler option `:closure-defines`.

Example:
  (ns your-app.core)
  (goog-define DEBUG! false)
  ;; can be overridden with
  :closure-defines {"your_app.core.DEBUG_BANG_" true}
  or
  :closure-defines {your-app.core/DEBUG! true}
Source code @ clojurescript:src/main/clojure/cljs/core.cljc
(core/defmacro goog-define
  [sym default]
  (assert-args goog-define
   (core/or (core/string? default)
            (core/number? default)
            (core/true? default)
            (core/false? default)) "a string, number or boolean as default value")
  (core/let [defname (comp/munge (core/str *ns* "/" sym))
             type    (core/cond
                       (core/string? default) "string"
                       (core/number? default) "number"
                       (core/or (core/true? default) (core/false? default)) "boolean")]
    `(~(if (:def-emits-var &env) `return-first `do)
       (declare ~(core/vary-meta sym
                   (core/fn [m]
                     (core/cond-> m
                       (core/not (core/contains? m :tag))
                       (core/assoc :tag (core/symbol type))
                       ))))
       (def ~(vary-meta sym assoc :goog-define type) (goog/define ~defname ~default)))))