How do I execute a single clojurescript file as a script?

In clojure, I can use a plugin lein-exec

to lein

"execute" a single clojure file as a script like this:

lein exec foo.clj

      

Naturally, I'm also wondering if there is a similar way to "run" one clojurescript file on node.js

directly without the process of compiling and running the .js file (possibly in a terminal)?

Thank!

+3


source to share


2 answers


Clojurescript needs to be compiled to JS if you want to execute it. What I am aware of is that the lein cljsbuild method is missing since CLJS runs in multiple environments (node, rhino, v8, browser, etc.) and each one is very specific about running JS, so it cannot make a choice for you.

Here are some quick ways to run a cljs script in node (keep in mind that compilation will take time as it is based on java and startup times are a bit bad)

The easy way

The easiest way will now be something like this (to execute the script in node):

lein new mies-node hello-world
cd hello-world
lein cljsbuild once
node run.js

      

There are more templates for node cljs, mies-node is just one of them.




Difficult (but later path)

If you need the hard way, install the clojurescript compiler and then you can do something like this:

$ node -e "$(cljsc hello.cljs '{:target :nodejs :optimizations :whitespace}')"

      

Note: cljsc must be in the path

Note: this can lead to issues with external components and what not. play with the parameters you pass to the compiler

+1


source


Because your question is "run one clojurescript file", then try to do self-capture of the Clojurescript compiler:

https://github.com/lazerwalker/clojurescript-in-clojurescript

Readme says:

./run.js nodecljs.js hello.cljs

      



If you want to run a REPL with Nodejs support:

https://github.com/bodil/cljs-noderepl

The "problem" (for your concern) with Clojurescript is that it uses the Google Closure library (tags goog.require

) to manipulate modules (like a namespace in Clojurescript terms) that are not Nodejs style for loading modules. Consequently, all involved modules must be packed into a single file javascript (using any optimization modes :whitespace

, :simple

and :advanced

) to be able to run in Nodejs.

0


source







All Articles