Compile multiple cljs-files to independent js-files

1.1k Views Asked by At

Edit: In case someone tries to build independent cljs/js-files for one project: It works exactly the way described below. You just have to ensure the cljs-directories contain just the required files - it was my fault to leave unintentionally a copy of another (not used) cljs-file in one directory; although not referenced, this will be compiled into wanted file.

I use cljsbuild with multiple builds to generate two different cljs/js-files on same page. There are no dependencies between these files and they use different namespaces. Compiling in advanced-mode builds two js-files, where one of the js-file includes the whole other - so that one files grows unnecessary.

Is there a way to generate two completely independent js-files without setting up two different projects?

Part of my project.clj:

:cljsbuild {:builds {:app1 {:source-paths ["src/cljs-app1"]
                          :compiler {:output-to     "resources/public/js/app1.js"
                                     :output-dir    "resources/public/js/out-app1"
                                     :asset-path   "/js/out-app1"
                                     :optimizations :none
                                     :pretty-print  true}}
                   :app2 {:source-paths ["src/cljs-app2"]
                          :compiler {:output-to     "resources/public/js/app2.js"
                                     :output-dir    "resources/public/js/out-app2"
                                     :asset-path   "/js/out-app2"
                                     :optimizations :none
                                     :pretty-print  true}}}}

:profiles {:dev {:cljsbuild {:builds {:app1 {:compiler {:source-map true}}
                                  :app2 {:compiler {:source-map true}}}}}

         :uberjar {:hooks [leiningen.cljsbuild minify-assets.plugin/hooks]
                   :env {:production true}
                   :aot :all
                   :omit-source true
                   :cljsbuild {:jar true
                               :builds {:app1 {:compiler
                                              {:optimizations :advanced
                                               :pretty-print false}}
                                        :app2 {:compiler
                                               {:optimizations :advanced
                                                :pretty-print false}}}}}
       :production {:cljsbuild {}}
       }
2

There are 2 best solutions below

0
On

Currently the combined list of source paths is used when compiling each of the builds, which may lead to surprising inclusions/leaks in the resulting js files. A work-around is to run cljsbuild separately for each build:

lein cljsbuild once app1
lein cljsbuild once app2
0
On

Found something that might help https://github.com/thheller/shadow-build