Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating & using libraries in both Clojure and ClojureScript

I've just written some ClojureScript code, only to find out that I couldn't access one of the libraries listed in my project.clj's dependencies. Is this correct, i.e. that you can't use Clojure libraries from CLJS unless they're specifically designed to allow it?

If so, how much extra work is required to take a Clojure library that doesn't use any Java interop, and would itself be valid ClojureScript code, and make it useable from ClojureScript? From looking around GitHub, many libs appear to have separate source directories for clj and cljs code. Can such a library be added to my project.clj and be used immediately from either platform?

like image 578
Matthew H Avatar asked Mar 27 '13 13:03

Matthew H


1 Answers

There are some ClojureScript differences from Clojure.

Dependecies from "project.clj" can be applicable / visible / usable by ClojureScript, for example, take a look at "jayq". You would include it in "project.clj":

(defproject xyz/xyz "0.1.0-SNAPSHOT"
  :dependencies [[clj-time "0.4.3"]
                 [jayq "2.2.0"]
                  ....

And then use it in the ClojureScript file:

(ns xyz.some.cljs
  (:require ...
            ...
            [clojure.browser.repl :as repl]
            [jayq.core :as jq])

While "jayq" is not a "Clojure" library in the "backend" sense since it just wraps JavaScript, it is an example of using a "project.clj" dependency on the ClojureScript side.

In addition most of the core and several non core libraries are already ported to the ClojureScript side:

  • clojure.set
  • clojure.string
  • clojure.walk
  • clojure.zip
  • clojure.core.reducers
  • fold is currently an alias for reduce
  • core.match
  • core.logic (in works)

Other Clojure libraries will have to conform to the ClojureScript subset in order to work in ClojureScript.

It is worthwhile to clone ClojureScript repo and get a sense of what it supported (plus add your own features if you feel adventurous :)

ClojureScript dependencies are usually "front end" based (included the ones ported from backend). In other words, the end goal is to be compiled by V8 and run as JavaScript, hence anything that can be compiled by the ClojureScript compiler (repo above) can be used.

like image 151
tolitius Avatar answered Oct 22 '22 01:10

tolitius