Reusable ClojureScript Libraries

So, I took the big Clojure project and split it into two.

I put together the bulk and made Uberjar.

Then printed:

lein deploy clojars 

to make this jar public, and then added it as a dependency in the project.clj file for my application.

This code is written in cljx, so it can compile in javascript.

What equivalent steps do I need to make my two cljx code sections be in two separate javascript libraries without repeating the whole ClojureScript virtual machine twice?

Update: just to clarify a couple of things.

1) I know that I can go to .cljc and not to .cljx. My only problem is that this seems pretty new, and I don't know if existing users of my code can have it. But I will most likely take this step soon. Answers in terms of cljc are also welcome.

2) I am already successfully compiling my code into one monolithic main.js. I am looking for how to compile individual cljs libraries that can be included in other cljs projects. Since every time I am currently compiling something in cljs, I get the main.js file with the entire clojurescript virtual machine.

3). One of my motives for returning to this question is that I want to start using Figwheel. So I want to be able to create libraries in clojurescript so that I can move on to the new clojurescript project that I am developing through Figwheel. Therefore, I assume that I will link to them in this new project project.clj file and include them in the web page as already compiled .js files. Am I wrong about this?

+6
source share
2 answers

Have you tried the Checking dependency function from Leiningen?

https://github.com/technomancy/leiningen/blob/stable/doc/TUTORIAL.md#checkout-dependencies

+2
source

It's hard to say what is wrong or missing in your configuration without having more details about your project.clj , but I will try to highlight the most important parts that you should take a look at.

Do not use cljx. Use reading conditions!

Currently, cljx is deprecated in favor of read settings (cljc) . If you have the opportunity to upgrade to clojure 1.7.0, then my suggestion is to do this and leave cljx behind : you will avoid many problems from cljx, because instead of using the build tool (which should be synchronized with the rest of the assembly pipeline) cljc - this is what the clojure compiler knows.

If you are still going to use cljx ...

I will not repeat all the things included in the excellent README from cljx , but just highlight some relevant parts:

  • You must be sure that cljx once is called before other tasks in the pipeline ( javac , compile , uberjar and even test or cljsbuild test ). This way you can create aliases or change the default value :prep-tasks in project.clj . Additional information: see the "Installation" section.

  • Your cljx :output-path should be added to your cljsbuild :source-paths . See Usage Section

And last but not least:

  1. Do not specify your dependency as :classifier "aot" in your dependency vector when using cljsbuild. Use it only if you know what you are doing. Delete it during development if you have dependency issues. This applies if you use cljx, cljc or just cljs.

  2. Take a look at other cljx configuration projects. For example: Prismatic / schema and "Switch to reading conditions" lock in Sablono

+2
source

Source: https://habr.com/ru/post/979052/


All Articles