Clojurescript JavaScript Compatibility

I created an application using mostly Angular. Now I would like to translate the project into Clojurescript. Clojurescript interacts very well with JavaScript, as we all know, but is it possible to do this the other way around?

How is regular JavaScript / Angular code used in JavaScript created using Clojurescript?

The dream was to write new functions in clojurescript and make them work side by side with outdated code.

Any advice or recommendations in this regard are welcome.

+7
angularjs clojurescript
source share
2 answers

Clojurescript vars, functions and deftypes / records are normal JS-vars, functions and constructor functions / objects, so you can go cljs.core.abc() into your javascript and call clojurescript without any problems.

Cautions:

  • The name is munging. Clojurescript names allow a wider range of characters than javascript, so many function names will be garbled. For example. cljs.core/< becomes cljs.core._LT_ .
  • Macros. Macros exist only at the time of clojurescript compilation, so you cannot use the CLJS macro from javascript.
  • Advanced compilation. The core of Clojurescript is very large, and it uses the Google Closure compiler to eliminate dead code, which is only available in extended compilation. Using advanced compilation safely is very simple in pure clojurescript code but harder in javascript code . (The greatest danger is line movement and access to symbolic property.)

You have several options:

  • CLJS deploys legacy code as a library. Your program entry point is CLJS, but uses legacy code as an external library. There is extern in your legacy code where CLJS calls your legacy code. The outdated code only calls CLJS functions that are explicitly exported using ^:export (if you specify a name) or something like goog.exportSymbol . CLJS and legacy code are deployed in separate JS files, which are created separately.
  • CLJS directly deploys legacy code. The program entry point is CLJS, but your old code is another js compiled into the same project. CLJS and legacy code are deployed in a single JS file built by the clojurescript compiler. To use advanced compilation, your old code must be safe to use with advanced compilation and structured for use with the Google Closure compiler (goog.provides / requires, type annotations, etc.). Angular will probably remain separate, but there is angular extern available .
  • Outdated code calls the CLJS code as a library. This is almost the same as "CLJS embeds legacy code as a library", except that your entry point is legacy code and calls the exported cljs functions.
  • The legacy code uses some CLJS functions through a library, such as mori . Here you are not using CLJS directly, but a JS library that provides some clojurescript functions like regular JS. It remains a separate js library and you never write clojurescript.
+12
source share

You can use the gyr extension to integrate Angular into ClojureScript. You can use clj-> js to convert data from Clojure types to Java Script types.

+1
source share

All Articles