-
Notifications
You must be signed in to change notification settings - Fork 114
ClojureScript
Clara offers ClojureScript support with a couple constraints. Because compiling the Rete network requires generating and compiling code, this action must be done on a JVM until ClojureScript is self-hosted. Therefore the ClojureScript version needs to use pre-defined rule sessions rather than generating them on the fly.
Fortunately, this is provided with the defsession
macro, which ClojureScript users can simply call as follows:
(defsession my-session 'my.rule.namespace 'my.other.namespace)
where 'my-rule-namespace
and others are simply symbols identifying the namespace where rules and queries are defined with defrule
and defquery
, respectively.
See the shopping example in clara-examples to see this in action.
Sometimes when working in development envrionment you will want to use :optimizations :none
flag for clojurescript compiler. This is especially true if you're aiming for interactive development with figwheel. If you set compiler flag :optimizations
to :none
make sure to also set flag :cache-analysis
to :false
.
Example of proper :cljsbuild
configuration:
:cljsbuild {:builds [{:source-paths ["src/main/clojurescript"]
:compiler {:main "clara.examples"
:output-to "resources/public/js/examples.js"
:output-dir "resources/public/js/out"
:asset-path "js/out"
:optimizations :none
:cache-analysis false}}]}
TL,DR If you don't do it your code will break sometimes, and you won't get repeatable builds.
Some more explanation. Clojurescript compiler will perform incremental builds when :optimizations
is set to :none
. This means that only namespaces that changed and dependent namespaces will get recompiled. This is usually not bad on it's own, because to perform the analysis all namespaces will be read by the clojurescript analyser, and effectively all macros will get expanded. However, when in used from REPL session or when :optimization
is set to :none
clojurescript compiler will also dump analysis results on disk (to gain some speed on further builds).
Unfortunately, if these cache entries are used, some files won't even be read, effectively resulting in no macroexpansion of some namespaces. Since clara uses code generation at macroexpansion time, some of your rules and queries might not get compiled. You might see some rules are not firing and in case of queries you will probably see errors about not defined queries.
This is not a bug in clara. This is how clojurescript compiler works.