5

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"]]}}
jack the lesser
  • 701
  • 2
  • 7
  • 15
  • I don't do any CLJS, but I thought a [source map](http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/) was for mapping "pre compiled" code to "compiled" code, and didn't have anything to do with the URLs inside the code. – Shepmaster Jan 04 '14 at 15:36
  • Yes, sorry. Source maps work fine for me, it's only when I set optimizations to :none, that I have problems. – jack the lesser Jan 05 '14 at 06:28

2 Answers2

2

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.

lorinpa
  • 556
  • 1
  • 5
  • 6
  • While :whitespace works, the compile time is around 7 seconds, whereas with :none it took less than 1 second to compile, so I'm looking to speed up development. – jack the lesser Jan 06 '14 at 04:44
  • 1
    Run lein cljsbuild auto while you are editing your cljs source files. That will speed up the development process. Please consider editing your original post. Your issue doesn't sound like it has anything to do "files reference their dependencies using the local file-system path"? If it does, please post an example? Your issue is speeding up the development process, right. Not generation of file references? – lorinpa Jan 06 '14 at 05:19
  • I'm using `lein cljsbuild auto` for compiling the cljs files(it still takes around 6 seconds for compiling when optimzations are set to :whitespace or :simple etc). The two issues are interconnected, when the optimizations are set to :whitespace the output js file correctly sets the server path of its dependencies(closure files, my project's cljs files etc). However when I set it :none, it seems to use local file paths such as (../../my-cljs-file.cljs) when generating the cljs files. – jack the lesser Jan 06 '14 at 06:20
  • An optimization on :none means cljsbuild is not generating js from your cljs source. Compare the command line and js output between to 2 opt levels.Do a lein cljsbuild clean between builds (that guarantees fresh output). If you want to prototype your cljs code try running a repl with: "lein trampoline cljsbuild repl-rhino". Hope that helps. – lorinpa Jan 06 '14 at 14:59
  • I ran `lein cljsbuild clean` and then with optimizations set to :none, then I ran `lein cljsbuild auto`, The cljs files are being compiled to corresponding js files. The single output file has a few require calls to the other dependencies and also to my generated js file. Unfortunately it uses local file paths like for example `goog.addDependency("../clojure/string.js", ['clojure.string'], ['cljs.core', 'goog.string', 'goog.string.StringBuffer']); goog.addDependency("../cljs/reader.js", ['cljs.reader'], ['cljs.core', 'goog.string']);` – jack the lesser Jan 07 '14 at 08:08
0

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.

0rca
  • 166
  • 4