How to create a library that works with Clojure and ClojureScript
Clojure and ClojureScript are forms of the same language targeting different hosts - JVM and JavaScript respectively. If you are creating a library, there is a big chance that a significant part of the code will work for both hosts but a part of it will be host-dependent.
Here we will discuss how to isolate the host-dependent parts of code to be used only when appropriate in order to write a single library that works for both Clojure and ClojureScript.
Steps that we are going to do:
- Create a simple logger Clojure library. Its
(log)
function will print the passed object with added timestamp and information from what language it was invoked. - Create a Clojure app that uses the
logger
library - Modify
logger
library to work with ClojureScript as well - Create a ClojureScript app that uses the
logger
library to check that our modifications worked correctly
Creating Clojure version of logger library
Here is a folder structure for our logger
library:
|
|
It’s deps.edn
can be an empty map:
|
|
And here is a code of the logger.clj
:
|
|
(timestamp)
is a helper function that uses Java host features to get a formatted timestamp.
(log)
is the function visible to the library users. It takes the user argument and using (cl-format)
prints it prepended with timestamp and language name (”Clojure” in this case).
The first argument of (cl-format)
- true
, means that printing should be done to the default output. You can read more about this function in the official documentation.
Creating Clojure app to use logger library
Now let’s create a Clojure app to use the library. It will be called cljapp
and put on the same lever with the logger:
|
|
Here is a folder structure for cljapp
:
|
|
In deps.edn
we’ll reference logger
library by location on the filesystem:
|
|
And here is the code inside core.clj
:
|
|
We required the namespace of the logger library and used (logger/log)
inside the (main)
to print different arguments.
Now let’s run the main function using Clojure CLI (from cljapp
folder) to make sure it works correctly:
|
|
Nice, as we see, it does)
Introducing reader conditionals
There is a part of Clojure tooling called Reader. It takes a textual code representation and turns it into the Clojure data structures. When you compile Clojure code, Reader will be responsible for processing your sources.
Reader supports two reader conditionals which allow you to specify different pieces of code and choose between them depending on the platform where the reader is invoked.
The standard reader starts with #?
and looks like:
|
|
When Reader encounters such a conditional, it will leave only one expression in the result data structure - the one corresponding to the current host or the default one if the current host is not listed.
So after reading this code:
|
|
On ClojureScript host Reader will return this datastructure:
|
|
The splicing reader starts with #?@
and looks like this:
|
|
When it encountered, Reader will choose the vector depending on the host and will put the content of vector in the surrounding context. Not the vector itself! It’s content.
And after reading this code:
|
|
on Clojure platform Reader will return the datastructure:
|
|
Note: in the source code reader conditionals work only in files with *.cljc
file extension!
To grasp reader conditionals better you can experiment in REPL by feeding different code pieces to the read-string
function (with {:read-cond :allow}
as a first argument) and inspecting the output.
|
|
Making the logger library work with ClojureScript
Now with all that knowledge about reader conditionals, it is time to revamp logger
to make it work for ClojureScript.
First, we need to rename logger.clj
→ logger.cljc
to enable reader conditionals.
Folder structure now:
|
|
Next we need to add ClojureScript-related code in (comment)
function in logger.cljc
. It will be wrapped with standard reader conditional:
|
|
And as the last step, we modify (log)
function to display the correct language name depending on the host. We use splicing reader conditional on doing this:
|
|
Full content of logger.cljc
now:
|
|
Now we need to check that changes didn’t affect the work of existing cljapp
Calling core namespace again from cljapp
folder:
|
|
Creating ClojureScript app to use logger library
And finally, we need to check that the library also works for the ClojureScript project.
Let’s create one, called cljsapp
on the same level as logger
and cljapp
:
|
|
Project structure:
|
|
deps.edn
content:
|
|
core.cljs
content:
|
|
And the actual check using Clojure CLI:
|
|
Perfect, now we have a library that works for both Clojure and ClojureScript :)