Following up on my own confusion, I'll contribute some comments about
potential improvements to either the ClojureScript compiler itself, or
documentation about it's usage in development mode, where everything
is not stuffed into one single js file.

First of all, the options to the compiler. I'll base my comments on
the example found on:

  https://github.com/clojure/clojurescript/wiki/Quick-Start

My apologies in advance if I mess it up, as I'm trying to "rewire"
experiences from my own code to apply to that example.

If you want to invoke the compiler from within a repl environment, or
your own traditional (JVM based) clojure program, the following syntax
is used:

(cljsc/build "samples/hello/src" {:output-dir "samples/hello/
out" :output-to "samples/hello/hello.js"})

Doing this will put the google closure library in the
"samples/hello/out" directory, together with a "hello.js" which is the
output of the clojurescript compilation phase, i.e. your clojurescript
program in javascript.

In addition, it will output another "hello.js" file in the
"samples/hello" directory. This file is just an "include" file, that
effectively pulls in the other stuff, including the identically named
hello.js file found below the out directory.

All these javascript files need to be accessible to your web browser,
and assuming you are fine with having "samples/hello" in your web
server documentroot or similar, I guess that should work
fine. But that also means that your clojure source code can be found
inside the documentroot (the "src" directory).

I worked around this in my own source by telling the clojurescript
compiler to write it's output beneath the out directory and giving it
a totally uniqe name, i.e. :output-to "samples/hello/out/add-deps.js"
.

I want it in the out directory to make it easy to make it simple to
put everything into the documentroot, which in this case could be done
simply by symlinking/copying the whole out directory to the
documentroot.

There is some "magic" in the clojurescript compiler that look up and
add the path references in the closure (see add-dep-string inside
closure.clj), which probably works fine if you run everything as
written in the docs. But if you try to get fancy with symlinking the
"out" directory and the "one level up" hello.js, this "magic" can be
pretty confusing and make it really hard to get right.

My "workaround" by putting the output-to destination into the "out"
directory with a unique name makes this stuff a lot easier I believe.

There's also another gotcha that I noticed, related to how javascript
works. Assuming you've followed my example, your "out/add-deps.js"
file looks something like this:

goog.addDependency("../cljs/core.js", ['cljs.core'], ['goog.string',
'goog.string.StringBuffer', 'goog.object', 'goog.array']);
goog.addDependency("../hello.js", ['hello'], ['cljs.core']);

Taking the example from the docs, extending it naively like I did,
your index.html might look something like this:

<script type="text/javascript" src="out/goog/base.js"></script>
<script type="text/javascript" src="out/add-deps.js"></script>
<script>
  goog.require('hello');
  alert(hello.greet("Hello world!"));
</script>

Unfortunately, that does not work. This does however:

<script type="text/javascript" src="out/goog/base.js"></script>
<script type="text/javascript" src="out/add-deps.js"></script>
<script>
  goog.require('hello');
</script>
<script>
  alert(hello.greet("Hello world!"));
</script>

This is related to some inner workings of the closure library, which I
believe this is explained on the following page:

http://code.google.com/closure/library/docs/gettingstarted.html

With this in place, things are running smoothly at my end, and my
clojure ring+jetty based server, with pasted code from
https://github.com/maxweber/cljs-devmode, happily recompiles any
clojurescript client side modules to javascript whenever the cljs
source file changes.

Thanks,

Marius K.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to