shadow-cljs

ClojureScript compilation made easy

README


undefined npm Clojars Project

shadow-cljs provides everything you need to compile your ClojureScript code with a focus on simplicity and ease of use.


Features


- Good configuration defaults so you don't have to sweat the details
- Seamless npm integration
- Fast builds, reliable caching, ...
- Supporting various targets :browser, :node-script, :npm-module, :react-native, :chrome-extension, ...
- Live Reload (CLJS + CSS)
- CLJS REPL
- Code splitting (via :modules)

overview-img

Requirements


- node.js (v6.0.0+, most recent version preferred)
- npm (comes bundled withnode.js) or yarn
- Java SDK (Version 11+, Latest LTS Version recommended)

Quick Start


Creating your project can be done quickly using the npx create-cljs-project utility. npx is part of npm and lets us run utility scripts quickly without worrying about installing them first. The installer will create a basic project scaffold and install the latest version of shadow-cljs in the project.

  1. ``` sh
  2. $ npx create-cljs-project acme-app
  3. npx: installed 1 in 5.887s
  4. shadow-cljs - creating project: .../acme-app
  5. Creating: .../acme-app/package.json
  6. Creating: .../acme-app/shadow-cljs.edn
  7. Creating: .../acme-app/.gitignore
  8. Creating: .../acme-app/src/main
  9. Creating: .../acme-app/src/test
  10. ----
  11. Installing shadow-cljs in project.

  12. npm notice created a lockfile as package-lock.json. You should commit this file.
  13. + shadow-cljs@<version>
  14. added 88 packages from 103 contributors and audited 636 packages in 6.287s
  15. found 0 vulnerabilities

  16. ----
  17. Done.
  18. ----
  19. ```

The resulting project has the following structure

  1. ``` sh
  2. .
  3. node_modules (omitted ...)
  4. package.json
  5. package-lock.json
  6. shadow-cljs.edn
  7. src
  8.      main
  9.      test
  10. ```

shadow-cljs.edn will be used to configure your CLJS builds and CLJS dependencies. package.json is used by npm to manage JS dependencies.

Everything is ready to go if you just want to start playing with a REPL

  1. ``` sh
  2. $ npx shadow-cljs node-repl
  3. # or
  4. $ npx shadow-cljs browser-repl
  5. ```

When building actual projects we need to configure the build first and create at least one source file.

The default source paths are configured to use src/main as the primary source directory. It is recommended to follow the Java Naming Conventions to organize your CLJS namespaces. It is recommended to start all namespaces with a unique enough prefix (eg. company name, project name) to avoid conflicts with generic names such asapp.core. Suppose you were building a Web Frontend for Acme Inc. usingacme.frontend.app might be a good starting point as it can easily grow to include acme.backend.* later on.

Using the above example the expected filename for acme.frontend.app is src/main/acme/frontend/app.cljs.

Lets start with a simple example for a browser-based build.

  1. ```clojure
  2. (ns acme.frontend.app)

  3. (defn init []
  4.   (println "Hello World"))
  5. ```

Inside the shadow-cljs.edn :builds section add

  1. ```clojure
  2. {...
  3. :builds
  4. {:frontend
  5.   {:target :browser
  6.    :modules {:main {:init-fn acme.frontend.app/init}}
  7.    }}}
  8. ```

This config tells the compiler to call (acme.frontend.app/init) when the generated JS code is loaded. Since no :output-dir is configured the default public/js is used. You can start the development process by running:

  1. ``` sh
  2. $ npx shadow-cljs watch frontend
  3. ...
  4. a few moments later ...
  5. ...
  6. [:frontend] Build completed. (134 files, 35 compiled, 0 warnings, 5.80s)
  7. ```

The compilation will create the public/js/main.js we configured above (:main becomes main.js in the :output-dir). Since we want to load this in the browser we need to create a HTML file in public/index.html.

  1. ``` html
  2. <!doctype html>
  3. <html>
  4.   <head>
  5.     <meta charset="utf-8" />
  6.     <title>acme frontend</title>
  7.   </head>
  8.   <body>
  9.     <div id="root"></div>
  10.     <script src="/js/main.js"></script>
  11.   </body>
  12. </html>
  13. ```

We also need a simple HTTP server to serve our HTML since modern Browsers all place a few restrictions on files loaded directly from disk which will lead to issues later. shadow-cljs provides such a server but you can use anything you like at this point. It only matters that the files from the public directory are served properly. To start the built-in web server just adjust the build config from above.

  1. ```clojure
  2. {...
  3. :dev-http {8080 "public"}
  4. :builds
  5. {:frontend
  6.   {:target :browser
  7.    :modules {:main {:init-fn acme.frontend.app/init}}
  8.    }}}
  9. ```

Once the config is saved the server will automatically start and serve the content at http://localhost:8080. There is no need to restart shadow-cljs. When opening the above URL the Browser Console should show "Hello World".


To be continued ...

Documentation


Please refer to the User Manual. (Work in Progress)

Video Courses


- [EN] Learn Reagent Free - reagent+firebase demo application built using shadow-cljs
- [EN] Learn Reagent Pro [Affiliate Link, 30$ discount] - reagent+firebase demo application built using shadow-cljs
- [EN] Learn re-frame [Affiliate Link] - re-frame SPA tutorial

Guides


- ... please let me know if you created something to include here

Examples


- re-frame-template -  Leiningen template that creates a re-frame project using the shadow-cljs build tool with many optional extras.
- jacekschae/shadow-reagent - shadow-cljs + proto-repl + reagent
- jacekschae/shadow-firebase - shadow-cljs + firebase
- ahonn/shadow-electron-starter - ClojureScript + Shadow-cljs + Electron + Reagent
- [jacekschae/conduit](https://github.com/jacekschae/conduit) - Real world application built with shadow-cljs + re-frame + re-frame-10x
[Demo](https://jacekschae.github.io/conduit-re-frame-demo/) | [Demo with re-frame-10x](https://jacekschae.github.io/conduit-re-frame-10x-demo/)
- quangv/shadow-re-frame-simple-example - a simple re-frame + shadow-cljs example.
- CryptoTwittos - reagent, re-frame, web3
- loganpowell/shadow-proto-starter - shadow-cljs, Atom, Proto-REPL, node.js library
- manuel-uberti/boodle - re-frame based Accounting SPA withdeps.edn based backend
- shadow-cljs-kitchen-async-puppeteer - Automated browser test with puppeteer and cljs.test, built with shadow-cljs
- baskeboler/cljs-karaoke-client - web karaoke player using shadow-cljs + reagent + re-frame + re-frame-10x + re-frame-async-flow-fx + build hooks for minifying css and generating seo pages (Demo)
- flexsurfer/ClojureRNProject - simple React Native application with ClojureScript, re-frame and react navigation v5
- jacekschae/shadow-cljs-devcards - how to configure devcards with shadow-cljs
- jacekschae/shadow-cljs-tailwindcss - shadow-cljs + tailwindcss-jit setup
- ertugrulcetin/racing-game-cljs - A 3D racing game built with ClojureScript, React and ThreeJS
- prestancedesign/pingcrm-clojure - A full stack SPA application built with Clojure + ClojureScript to illustrate how Inertia.js works.
- ... please let me know if you created something to include here

Libraries

- flexsurfer/rn-shadow-steroid - React Native with shadow-cljs on steroids
- re-frame-flow - A graph based visualization tool for re-frame event chains using shadow-cljs
- ... please let me know if you created something to include here

License


Copyright © 2022 Thomas Heller

Distributed under the Eclipse Public License either version 1.0 or (at
your option) any later version.