Auto building Clojurescript files for Compojure app

1.2k Views Asked by At

I have a web application where i'm using Compojure on the server and Clojurescript on the client. I'm using the leing-cljsbuild plugin to automatically compile cljs files to js.

I'm able to generate the required client side files and load them in the browser when I set the optimizations to :whitespace or :simple, but when I set optimizations to none, the js files reference their dependencies using the local file-system path, which leads to the files not loading at all in the browser.

So, my question is how do I make the generated files use server urls instead of local file paths when they are generated by the clojurescript compiler.

Here's my project.clj file

(defproject my-proj-clj "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"

  :dependencies [[org.clojure/clojure "1.5.1"]
                 [compojure "1.1.6"]
                 [org.clojure/tools.nrepl "0.2.3"]
                 [hiccup "1.0.3"]
                 [com.novemberain/monger "1.5.0"]
                 [org.clojure/clojurescript "0.0-2127"]
                 [jayq "2.5.0"]
                 ]

  :plugins [[lein-ring "0.8.8"]
            [lein-cljsbuild "1.0.1"] 
            ]

  :ring {:handler my-proj-clj.handler/app
                   }

  :cljsbuild { :builds 
              [{
                :source-paths ["src/my-proj-clj"]
                :compiler {
                           :output-dir "./resources/public/js"
                           :output-to "./resources/public/js/cljs-file.js"  
                           :pretty-print true
                           :source-map "./resources/public/js/cljs-file.js.map"
                           :optimizations :none
                           }}]}  

  :profiles {:dev {:dependencies [[javax.servlet/servlet-api "2.5"]]}}
2

There are 2 best solutions below

5
On

I believe the only valid optimization values are :whitespace, :simple, or :advanced. See line 96 at https://github.com/emezeske/lein-cljsbuild/blob/1.0.1/sample.project.clj.

Thus I would use :whitespace as the optimization level (at least to get something working).

Per your post, an optimization level of :whitespace works? Thus, perhaps you can elaborate.

What results are you expecting from an optimization level of ":none". How does your expected result differ from what an optimization level of :whitespace produces?

An optimization level of :none means cljsbuild is not generating js from your cljs source (it will generate a few goog.include statements but nothing else). Try using an interactive repl to help you prototype. Try running the following : lein trampoline cljsbuild repl-rhino

Hope that helps.

0
On

I have roughly same setup, optimizations set to :none, generated files use local paths. However, browser does load the scripts.

What I have is this in index.html:

<script src="js/development/goog/base.js" type="text/javascript"></script>
<script src="js/development/main.js" type="text/javascript" ></script>
<script type="text/javascript">goog.require("ixtlan.core");</script>

this in project.clj:

:cljsbuild {
  :builds [{:id "dev"
    :source-paths ["src/cljs"]
    :compiler {
      :output-to "resources/public/js/development/main.js"
      :output-dir "resources/public/js/development"
      :optimizations :none
      :source-map true}}
          ...

and routes contain:

(defroutes routes
  (GET "/" [] (index))
    (route/files "/" {:root "resources/public"}))

Hope, this helps.