Skip to content

Instantly share code, notes, and snippets.

@oskarth
Last active October 6, 2015 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oskarth/de185c3e7e108af54619 to your computer and use it in GitHub Desktop.
Save oskarth/de185c3e7e108af54619 to your computer and use it in GitHub Desktop.

Using cljs.jar, version 1.7.48.

Desired behavior: ability to watch a single cljs file for changes, without any additional directories.

I'm aware that:

  1. java -cp cljs.jar:src clojure.main watch.clj is the normal command to run.
  2. Single-segment namespaces are not normally recommended.

With that said, it would be very neat to be able to have just a single cljs file and a temporary out directory. How do you best achieve this? What changes have to be made to the FS watcher?

A. Single-segment namespace and src dir

watch.clj:

(require 'cljs.build.api)

(cljs.build.api/watch "src"
  {:main 'hello
     :output-to "out/main.js"})

src/hello.cljs:

(ns hello)

(enable-console-print!)

(println "Hello world A!")

Output of running java -cp ~/bin/cljs.jar: clojure.main watch.clj:

Reading analysis cache for jar:file:/Users/oskarth/bin/cljs.jar!/cljs/core.cljs
Compiling src/hello.cljs
WARNING: hello is a single segment namespace at line 1 src/hello.cljs
... done. Elapsed 0.992244 seconds
Watching paths: /Users/oskarth/tmp/cljs-watch-behavior/src
Change detected, recompiling ...
Compiling src/hello.cljs
WARNING: hello is a single segment namespace at line 1 src/hello.cljs
... done. Elapsed 0.110331 seconds

Change detected, recompiling ...
Compiling src/hello.cljs
WARNING: hello is a single segment namespace at line 1 src/hello.cljs
... done. Elapsed 0.066818 seconds

I get WARNING: hello is a single segment namespace at line 1 src/core.cljs, but it works.

B. Single-segment namespace and watching a single file

Moving src/hello.cljs to hello.cljs and changing watch.clj to:

(cljs.build.api/watch "hello.cljs"
  {:main 'hello
     :output-to "out/main.js"})

Output:

~/tmp/cljs-watch-behavior$ java -cp ~/bin/cljs.jar: clojure.main watch.clj

Building ...
Analyzing jar:file:/Users/oskarth/bin/cljs.jar!/cljs/core.cljs
WARNING: hello is a single segment namespace at line 1 hello.cljs
Compiling out/cljs/core.cljs
Using cached cljs.core out/cljs/core.cljs
... done. Elapsed 5.7541 seconds
Watching paths: /Users/oskarth/tmp/cljs-watch-behavior/hello.cljs

Changing hello.cljs doesn't change anything. (It also says it's using cached cljs.core, but I ran rm -r out in between runs.)

C. Single-segment namespace and watch current directory

If I change it to absolute path of current directory, it predictably goes into an infinite loop (due to FS changing after out dir)

watch.clj:

(cljs.build.api/watch "/Users/oskarth/tmp/cljs-watch-behavior"
  {:main 'hello
     :output-to "out/main.js"})
~/tmp/cljs-watch-behavior$ java -cp ~/bin/cljs.jar: clojure.main watch.clj

Building ...
Reading analysis cache for jar:file:/Users/oskarth/bin/cljs.jar!/cljs/core.cljs
Compiling /Users/oskarth/tmp/cljs-watch-behavior/hello.cljs
WARNING: hello is a single segment namespace at line 1 /Users/oskarth/tmp/cljs-watch-behavior/hello.cljs
Compiling out/cljs/core.cljs
Using cached cljs.core out/cljs/core.cljs
... done. Elapsed 1.521039 seconds
Watching paths: /Users/oskarth/tmp/cljs-watch-behavior
Change detected, recompiling ...
Compiling /Users/oskarth/tmp/cljs-watch-behavior/out/hello.cljs
WARNING: hello is a single segment namespace at line 1 /Users/oskarth/tmp/cljs-watch-behavior/out/hello.cljs
... done. Elapsed 0.098181 seconds
Change detected, recompiling ...
... done. Elapsed 0.069759 seconds
Change detected, recompiling ...
... done. Elapsed 0.058259 seconds
Change detected, recompiling ...
... done. Elapsed 0.061258 seconds
Change detected, recompiling ...

D. Build works for one file

build.clj:

(require 'cljs.build.api)

(cljs.build.api/build "hello.cljs" 
  {:main 'hello
   :output-to "out/main.js"})

Running java -cp ~/bin/cljs.jar: clojure.main build.clj gives no output (other than warning for single segment ns), but it produces js.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment