How to write frontend (reagent) and backend (express.js) applications in a same clojurescript setup?

245 views Asked by At

Can I write frontend (reagent) and backend (express.js) applications in a same clojurescript project?

Following these two pages.

  1. Writing Node.js applications in Clojure and using NPM packages Backend (express.js)

  2. shadow-cljs Frontend (reagent, react)

Both of them are similar, create a new cljs project with npx create-cljs-project {project-name}. However, these are two individual projects, can I write frontend and backend application in a same project, if it is possible, how to manage repl in this project?

1

There are 1 answers

1
Thomas Heller On BEST ANSWER

Sure, the entire system is designed to combine as many things as you like. Just configure multiple builds in the shadow-cljs.edn :builds map.

If you already have both separate projects just take the .cljs files and move them to the other. Then take the build config and also move it into the other shadow-cljs.edn.

You may either control multiple builds from the shadow-cljs UI or the command line. All build relevant commands allow you to specify multiple builds (eg. npx shadow-cljs watch backend frontend, with the :frontend and :backend builds in shadow-cljs.edn).

You can use namespaces to properly "categorize" your project. I generally have something like my.app.ui or my.app.frontend for frontend related things and something like my.app.server or my.app.backend.

With the watch for both builds running you can just switch between the REPLs in your editor. From the command line you can just npx shadow-cljs cljs-repl frontend or npx shadow-cljs cljs-repl backend. Or switch between them from a Clojure REPL via npx shadow-cljs clj-repl and then (shadow/repl :frontend). That'll bring you into the CLJS REPL for the :frontend build. You may exit that via :cljs/quit, which brings you back to the CLJ REPL.