Using cljsbuild, I can compile all my .cljs files to one file. However, I wish to be able to pick a directory for output and have each .cljs file compile into its own .js file. How can this be specified?
2 Answers
You can use 'multiple build configurations': cljsbuild accepts a vector of configurations for :builds
key, each element of which defines rules for compiling separate .js file (more info could be found in lein-cljsbuild README). Simple example:
:cljsbuild
{:builds
[;; Config for first .js file
{:source-paths ["dir-with-cljs-for-first-js"]
:compiler {:output-to "dir-for-js/first.js"}
;; Config for second .js file
{:source-paths ["dir-with-cljs-for-second-js"]
:compiler {:output-to "dir-for-js/second.js"}}]}

- 308
- 1
- 8
-
how would I get a seperate .js file for every .clj file in "dir-with-cljs-for-first-js"? – zcaudate Apr 15 '13 at 04:00
-
In this example two .js files would be compiled: one from .cljs sources in "dir-with-cljs-for-first-js", second - from sources in "dir-with-cljs-for-second-js". There is no way to to create .js file for each file in directory ("dir-with-cljs-for-first-js"), but you could put every .cljs in a separate sub-directory (for instance, "dir-with-cljs-for-first-js/1", "dir-with-cljs-for-first-js/2", etc.) and manually specify a build configuration for each such sub-directory. – gsnewmark Apr 17 '13 at 13:57
You're looking for the :output-dir option. My cljsbuild looks like this:
:cljsbuild
{:builds
[{:id "dev"
:source-paths ["src/cljs"]
:compiler {:output-to "resources/public/js/site.js"
:output-dir "resources/public/js/out"
:source-map true :optimizations :none }}
{:id "main"
:source-paths ["src/cljs"]
:compiler {:pretty-print false
:output-to "resources/public/js/site.js"
:source-map "resources/public/js/site.js.map"
:optimizations :advanced}}]}
With this config, and without site.js already compiled/up to date, lein cljsbuild once dev
or lein cljsbuild auto dev
will compile site.js, then see it's dependencies aren't compiled and go compile them, along with their sourcemaps. If site.js is compiled and up to date, cljsbuild will think there's nothing to be done and terminate before generating source maps, etc.
I also highly recommend lein figwheel for cljs development, and remember to enable sourcemaps.

- 904
- 9
- 16