clojurescriptcljsbuild

How do I avoid duplication in a cljsbuild project?


The :cljsbuild section of my project.clj has a lot of duplication:

:cljsbuild {
  :builds {:dev {:source-paths ["src-cljs"]
                 :compiler {:output-dir "resources/public/js"
                            :output-to "resources/public/js/main.js"
                            :optimizations :whitespace
                            :pretty-print true
                            :source-map "resources/public/js/main.map"
                            :language-in :ecmascript5
                            :foreign-libs [{:file "third-party/mutation-summary/mutation-summary.js"
                                            :provides ["MutationSummary"]}]}}
           :production {:source-paths ["src-cljs"]
                        :compiler {:output-to "resources/public/js/main-min.js"
                                   :optimizations :advanced
                                   :pretty-print false
                                   :language-in :ecmascript5
                                   :foreign-libs [{:file "third-party/mutation-summary/mutation-summary.js"
                                                   :provides ["MutationSummary"]}]}}
           :test {:source-paths ["src-cljs" "test-cljs"]
                  :compiler {:output-to "resources/private/js/unit-test.js"
                             :optimizations :whitespace
                             :pretty-print true
                             :language-in :ecmascript5
                             :preamble ["react/react.min.js"]
                             :externs ["react/externs/react.js"]
                             :foreign-libs [{:file "third-party/mutation-summary/mutation-summary.js"
                                             :provides ["MutationSummary"]}]}}}
  :test-commands {"unit-tests" ["slimerjs" :runner
                                "resources/private/js/unit-test.js"]}}

I'm sure that it must be possible to use profiles to remove this duplication, but my Leiningen fu is lacking.


Solution

  • A not so pretty but viable solution is to add a def or defn above your defproject that holds the standard parameters that you are reusing, eg: (Not spellchecked nor functioning example)

    (def defaults
       "Returns default compiler options"
       {:language-in :ecmascript5
        :foreign-libs [{:file "third-party/mutation-summary/mutation-summary.js"
                        :provides ["MutationSummary"]}]})
    

    And then to mergethat map with the custom values:

     :builds {:dev {:source-paths ["src-cljs"]
                    :compiler     (merge defaults {:pretty-print true})