Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I compile Clojurescript forms from within Clojure?

I am using clj-webdriver to do some Selenium based testing on a Clojurescript web app. Sometimes, there is something in the app itself that I want to be able to fiddle with while the test are running. I see that clj-webdriver has something called (execute-script js args) that takes a string of Javascript code and runs it on the current testing browser. I have tested this and it seems to work. I would like to pass clojurescript code to execute-script though. I need something that will compile my Clojure form into Clojurescript code.

I see the following question that sort of relates. It says to use the js/emit function from clutch. I searched clutch and found it mentioned only in (view) in cljs-views.clj I have attempted the following in a repl:

user> (use 'com.ashafa.clutch.cljs-views)  
  nil  
user> view 
  <core$comp$fn__4034 clojure.core$comp$fn__4034@ebd3f80> 
user> js/emit 
  CompilerException java.lang.RuntimeException: No such namespace: js, #compiling (NO_SOURCE_PATH:1)   
user>

This isn't terribly surprising, how could js be in a regular clojure namesapce? But how do I use this (or any other) system to generate Clojurescript (javascript) code that I can pass to execute-script?

like image 821
Stephen Cagle Avatar asked Nov 25 '12 21:11

Stephen Cagle


1 Answers

Use the cljs.closure/build function:

(use '[cljs.closure :only [build]])
(build '(print "hello") {:optimizations :simple :pretty-print true})

There are more examples in a comment at the bottom of closure.clj. There are options for outputting to a file as well.

like image 155
Dan Lipsitt Avatar answered Oct 22 '22 05:10

Dan Lipsitt