Skip to content

How to make a clojure library self host compatible

Yehonathan Sharvit edited this page Jun 6, 2018 · 11 revisions

In order to make a clojure library self host compatible, you need first to port it to cljc and then make it self-host compatible.

From Clojure to ClojureScript

  • rename non macro files from .clj to .cljc
  • use reader conditionals when a different code is needed for clojure and clojurescript
  • move the macros code into a .clj or .cljc file
  • Use macrovich: It simplifies a lot the write of self-host compatible macros

From ClojureScript to Self-host compatible

Planck/Lumo

  • install planck and lumo
  • move to the main folder of your library
  • Launch planck/lumo with the correct classpath:
    planck -c`lein classpath`

or

    lumo -c`lein classpath`
  • require the namespaces of your library
  • fix the bugs
  • test again

Once you are done push the code to github and test online with KLIPSE.

KLIPSE

  • Open the KLIPSE REPL with external-data-libs=[<your raw github root>] e.g. http://app.klipse.tech/?external-libs=[https://raw.githubusercontent.com/viebel/math.combinatorics/master/src/main/clojure/]
  • require the namespaces of your libray
  • fix the bugs and re-push
  • test again
  • add the verbose=1 url parameter to get (helpful) debug messages

Tips and Tricks

First of all, take a deep breath: it's not easy to make a lib self-host compatible. But at the end of the day, you will be very happy!

Reader conditionals

In self-host, reader conditionals always branch to :cljs - even for macro files.

Data Types and protocols

Clojure and Clojurescript data types and protocols are very different

Debugging

It's hard!