How to avoid duplication in cljsbuild project?

There is a lot of duplication in :cljsbuildmy section project.clj:

: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 am sure it should be possible to use profiles to remove this duplication, but my Leiningen fu is missing.

+4
source share
1 answer

A not-so-pretty, but viable solution is to add defor defnover yours defproject, which contains the standard parameters that you reuse, for example: (Not tested by orfor or 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 up merge, which are mapped to custom values:

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

Source: https://habr.com/ru/post/1539164/


All Articles