5

I have a web app that uses Clojure on backend and ClojureScript on frontend, and it consists of several pages, each requiring appropriate builded js file.

Now I know that cljsbuild can build separate builds (if specified in :builds section of config file), provided that each build has it's own folder.

Problem is: now I need to duplicate some shared code in each folder in order to build properly. Besides, it's really annoying that I have to create whole new folder for even trivial cljs file to be used, not to mention editing every time the :builds section of my project.clj.

So, it's 2016, and all the tutorials on ClojureScript I've seen so far, are for single-page applications. Are there any resources or best practices for what I'm looking for, am I missing something out?

How do I use ClojureScript in standard multi-page Clojure-stack web app?

Thanks.

Twice_Twice
  • 527
  • 4
  • 16

3 Answers3

2

The :modules compiler option can be used to split your clojurescript build into optimized modules that can be required on demand.

tblomseth
  • 38
  • 5
Terje Norderhaug
  • 3,649
  • 22
  • 25
0

Do you really need multiple cljs builds?

Can't you just create a single one, exporting the components you need and hooking them up into dom whenever they are needed?

kongeor
  • 712
  • 1
  • 6
  • 14
  • I assume that in your case I need to include single builded js file on every page? – Twice_Twice Jun 01 '16 at 13:13
  • Yes. Why shouldn't you? You can organize your code using namespaces. – kongeor Jun 01 '16 at 14:53
  • Can you elaborate a little more, may be show some code, links? Thing is - this is the first time I'm dealing with Google Closure library itself, may be that's the problem. Any examples are appreciated. – Twice_Twice Jun 02 '16 at 05:28
0

It may be as easy as using require from one namespace into another.

For instance, say that you have the following structure :

project ├── src │   └── cljs │   ├── page1 │   ├── page2 │   │  

You could require things from the namespace page1.subnamespace from inside page2.

nha
  • 17,623
  • 13
  • 87
  • 133
  • Yes, I've got that, but how can I include functions from the build that are only needed in particular html page? Should I include the whole compiled js thing (which can be >x MB) on every single page even if I need small fraction of cljs codebase in it? – Twice_Twice Jun 04 '16 at 06:35
  • As far as I know there is no way to fraction the build, since the google closure compiler assumes to be handling the whole content every time (and eliminates dead code therefore). – nha Jun 05 '16 at 17:38
  • So the best practice is building single js file, include it in every page and then call only things from it that are needed on this page? – Twice_Twice Jun 06 '16 at 13:58
  • I don't know about "best practice". That could be a tradeoff (slower first load, faster next times due to caching) which brings you closer to a SPA. Or you can also consider making a build for every page, knowing that there will be some redundancy but the initial page load will be faster and that there will be no unnecessary code (due to google closure dead code elimination). – nha Jun 06 '16 at 14:08
  • Regarding your previous comment: "whole compiled js thing (which can be >x MB) on every single page even if I need small fraction of cljs codebase in it" that should not happen, since the closure compiler eliminates dead code. – nha Jun 07 '16 at 15:46