ClojureScript npm Package
Making the ClojureScript language usable from Node.js projects
var cljs = cljs// '(function (a){\nreturn [cljs.core.str("Hello "),cljs.core.str(a)].join(\'\');\n})'var f = cljs// 'Hello World'
Status
Very early. Do not use for anything critical. Contributions welcome!
Installation
npm install -g clojurescript
Usage
Evaluating & Compiling
eval
will return values that are directly usable from JavaScript. The whole standard library is available for use.
var f = cljs// [ 2, 4, 6, 8 ]
compile
will return a string that can be evaluated using JavaScript's eval
.
cljs'((((1 < 2)) && (((2 < 3)) && (((3 < 4)) && ((4 < 5)))))?console.log("Less"):null)'
Any code that makes use of namespaces of def
s will need to run after lib/bootsrap.js
has set up the ClojureScript environment. Contexts take care of this.
cljs'goog.provide(\'your.namespace\');\ngoog.require(\'cljs.core\');\n(function (){\nyour.namespace.f = (function your$namespace$f(x){\nreturn [cljs.core.str("your "),cljs.core.str(x)].join(\'\');\n}); return (\nnew cljs.core.Var(function(){return your.namespace.f;},new cljs.core.Symbol("your.namespace","f","your.namespace/f",1343508863,null),cljs.core.PersistentHashMap.fromArrays([new cljs.core.Keyword(null,"ns","ns",441598760),new cljs.core.Keyword(null,"name","name",1843675177),new cljs.core.Keyword(null,"file","file",-1269645878),new cljs.core.Keyword(null,"end-column","end-column",1425389514),new cljs.core.Keyword(null,"column","column",2078222095),new cljs.core.Keyword(null,"line","line",212345235),new cljs.core.Keyword(null,"end-line","end-line",1837326455),new cljs.core.Keyword(null,"arglists","arglists",1661989754),new cljs.core.Keyword(null,"doc","doc",1913296891),new cljs.core.Keyword(null,"test","test",577538877)],[new cljs.core.Symbol(null,"your.namespace","your.namespace",-1885076140,null),new cljs.core.Symbol(null,"f","f",43394975,null),null,28,21,1,1,cljs.core.list(new cljs.core.PersistentVector(null, 1, 5, cljs.core.PersistentVector.EMPTY_NODE, [new cljs.core.Symbol(null,"x","x",-555367584,null)], null)),null,(cljs.core.truth_(your.namespace.f)?your.namespace.f.cljs$lang$test:null)])));})()\n'
Contexts
ClojureScript has its own notion of namespaces implemented by the Google Closure JavaScript library. It sets up nested objects in the global environment to store your functions and data. This works well in the browser, but makes it difficult to work with Node which perfers to leave the global environment untouched.
To reconcile these two approaches, this package only ever evaluates ClojureScript code in an execution context. A context is an object that contains the global environment with the Google Closure-style namespaces that ClojureScript expects.
var ctx = cljsctxcljscorestr// { [Function]// ... }ctxcljscoremap// { [Function]// ... }
This way, ClojureScript gets the view of the world it wants and the global environment is not affected.
Code can be evaluated in any context. Namespaces and functions are all all accesible and usable from JavaScript.
cljscljsctxfooname// 'Ramsey'cljsctxfoo// '~Hello~'
Seperate contexts do not share anything. Even the standard library is reevaluated from scratch for each new context. Importantly, they maintain their own current namespace in *ns*
.
cljs// 'foo'var ctx2 = cljscljs// 'cljs.user'cljscljsctx2fooname// 'Not Ramsey'
When eval
and compile
are used without a second argument, a default context is used. It is exposed as context
on the module object.
cljscljscontextfooa// 1
ClojureScript Require
:require
and :use
work in ns
forms as expected. The search path aims to mirror the Node package resolution algorithm, i.e. .cljs
files will be looked up in the node_modules
folder if it exists, and then in the node_modules
folder of the parent directory and so on to the top of the file system.
;; node_modules/helpers.cljs(ns helpers) (defn wrap [a min max] (cond (< a min) min (> a max) max :else a))
var cljs = cljscljs// 10cljs// 0
Node Require
require
ing "clojurescript/register"
will register the .cljs
extension with Node's require
function and allow you to require
cljs files in node. The module will export the contents of the last namespace in the file.
var helpers = helpers// { wrap: [Function: helpers$wrap] }helpers// 90helpers// 0
Command Line
Installing with -g
will create put a cljs
executable on your path.
Calling without arguments starts a REPL.
$ cljs> (+ 1 2)3> (ns mappers)nil> (defn inc-all [col] (map inc col))#'mappers/inc-all> (ns cljs.user)nil> (mappers/inc-all (range 20))(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20)> (ns cljs.user (:require helpers))nil> (helpers/wrap 0 0 0)0> (ns cljs.user (:require [helpers :refer [wrap]]))nil> (wrap 90 0 10)10> (wrap -90 0 10)0
Passing a file as an argument will execute that file.
$ cat foo.cljs(->> (range 20) (map inc) (filter even?) (map #(println (str "Hello " % " World"))) dorun)$ cljs foo.cljsHello 2 WorldHello 4 WorldHello 6 WorldHello 8 WorldHello 10 WorldHello 12 WorldHello 14 WorldHello 16 WorldHello 18 WorldHello 20 World
API
cljs.newContext([globals])
Creates a new ClojureScript execution context.cljs.context
The default execution context. Used bycompile
andeval
by default.cljs.compile(str[, ctx])
— Takes a string of ClojureScript code and returns a string of JavaScript code. Optionally takes a context to compile the code in.cljs.eval(str[, ctx])
— Takes a string of ClojureScript code, evaluates it, and returns the resulting value. Optionally takes a context to evaluate the code in.
Building
lein cljsbuild once
to rebuild lib/bootstrap.js
, where the ClojureScript compiler and standard library live. Requires Leiningen, Clojure, and Java to be installed and properly configured.
Legal
npm package © 2015-2016 Ramsey Nasser, provided under the Eclipse Public License 1.0
Uses code from elbow © 2015–2016 Mike Fikes and Contributors. Used under the Eclipse Public License version 1.0.
ClojureScript © Rich Hickey, provided under the Eclipse Public License 1.0