Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling external JS files with Cljsbuild in ClojureScript

I'm trying to compile some JS libraries that we have with lein-cljsbuild to integrate them in our ClojureScript code base. First I added some goog.provide in top of each file, and the files are hierarchically organised in a directory tree according to their namespace (like in Java). That is namespace a.b.c is in src-js/libs/a/b/c.js

I have put the JS files in the root directory of the projects in src-js/libs, and I have the following :compiler options for lein-cljsbuild:

{:id "prod",
 :source-paths ["src-cljs" "src-js"]
 :compiler
 {:pretty-print false,
  :libs ["libs/"]
  :output-to "resources/public/js/compiled-app.js",
  :optimizations :simple}}

None of the JS files get compiled into the compiled-app file. What's wrong?

I also tried to put them in resources/closure-js/libs without success.

I'm using lein-cljsbuild 0.3.0.

like image 414
z1naOK9nu8iY5A Avatar asked Oct 05 '22 17:10

z1naOK9nu8iY5A


1 Answers

First, unlike what is suggested in some texts, you do not need to include your private closure library locations in any classpath configuration statement in your project.clj. So unless the "src/js" directory included in your "source-paths:" statement is for some other purpose, you can remove it.

Second, the only thing to add to your project.clj, for the sake of bringing in your private closure code, is the "libs:" reference you have made; BUT unlike what you have entered, that reference must be to a specific *.js file (or files) and not merely a directory. So if the library you want to used is in a file named test.js and that resides in the /src/js directory, your libs: entry would be: "src/js/test.js". See the cljs-build release notes if you want to use that plugin's default :libs directory option.

Third, (and it looks like you know this already, but this is what tripped me up) if you are using a browser-backed REPL (repl-listen option of cljsbuild), you still will not be able to load/reference/use your private library assets from that REPL until you include a :require statement somewhere in the source for your compiled-app.js (e.g. "(ns testing (:require [myprivatelib]))" ), THEN you must re-compile (lein cljsbuild once) and reload your browser page with a link to compiled-app.js. This brings in that code base. Otherwise, your browser REPL will just keep insisting that the namespace provided in your closure library is not defined.

I hope this helps.

like image 192
popek Avatar answered Oct 13 '22 01:10

popek