问题
Why are there clj and cljs folders in my lein re-frame template as below? And why do they both include files called .core
that appear to use the same namespaces? I've been told this is the place to start when learning re-frame, but I cannot find any explanations of why the templates are setup the way they are or created including the content they include.
There is no explanation for any of the boilerplate or code that comes with any lein template which make them very hard to use for beginners.
Thanks in advance.
回答1:
This setup is used to separate Clojure Backend code from the ClojureScript frontend. It isn't actually necessary and I don't particularly recommend it but I can explain its history and why you'd want to do it.
For the ClojureScript side it really doesn't matter at all.
When builing a Clojure Backend you will often deploy in some "uberjar" or "uberwar" setup. This means that all source files and dependencies are packed into one single .jar
file (basically just a zip file). This is typically done by including all files from a specified set of directories, so it would include src/clj
but not src/cljs
. If everything is in one directory it would add the .cljs
files as well although they are never used by the Clojure backend. So in essence it just makes your "uberjar" bigger. It is not an important optimizations but some people prefer to keep things lean and clean.
In addition some developers just prefer to separate the code this way. In this case the template authors did.
回答2:
One answer:
As the comments point out, some projects develop the backend code (Clojure) and the frontend code (ClojureScript) in the same project repo. I think this is a mistake as it can easily lead to confusion and entanglement (esp. if using lein to start both projects simultaneously). IMHO it is better to keep both front- and back-end parts in separate repositories. I would also strongly recommend using figwheel-main and the Clojure deps build tool for the CLJS code.
Another answer:
For CLJS code, any macros have to be defined in an "earlier" compilation stage. Thus, for namespaces defining macros, you often see files like either util.clj
or util.cljc
to define the macro, and then a file like util.cljs
where the macro is used.
You can find more information below, but it is subtle & confusing:
- https://clojurescript.org/guides/ns-forms
- https://clojurescript.org/about/differences
- https://blog.fikesfarm.com/posts/2018-08-12-two-file-clojurescript-namespace-pattern.html
来源:https://stackoverflow.com/questions/57791249/why-are-there-clj-and-cljs-folders-in-my-lein-re-frame-template