ClojureScript
repository·master·Indexed 27 days ago
https://github.com/clojure/clojurescriptA compiler that translates Clojure code into JavaScript, optimized for the Google Closure compiler. It supports multiple optimization levels (none, whitespace, simple, advanced) and provides a CLI tool, cljsc, for compilation. The project includes support for browser-connected REPLs, JavaScript interop, and integration with build tools like Clojure deps.edn, Leiningen, and Maven (version 1.12.145).
What's inside ClojureScript
- ClojureScript is a compiler for Clojure that targets JavaScript. It is specifically designed to produce JavaScript code that is compatible with the advanced compilation mode of the Google Closure optimizing compiler.
Get started with ClojureScript
masterTo begin using ClojureScript, follow these resources:
- Quick Start Guide: https://clojurescript.org/guides/quick-start
- Official Documentation: https://clojurescript.org
- Tutorials: https://clojurescript.org/guides
Community support and issue reporting
masterIf you have questions, feedback, or need to report issues, use the following channels:
- Mailing Lists: Clojure mailing list or the ClojureScript user mailing list.
- Chat: IRC channel
#clojurescripton freenode.net or the Clojurians Slack channel. - Bug/Feature Tracking: Jira.
Note: Before submitting issues, please review the Reporting Issues page.
Use Externs with Advanced Optimizations
masterWhen using
:optimizations :advanced, external JavaScript calls may be optimized away, causing errors. To prevent this, you must compile your source with a referred externs file using the:externsoption.cljsc src '{:optimizations :advanced :output-to "hello-extern.js" :externs ["externs.js"]}'One-time Setup for ClojureScript
masterBefore using the ClojureScript CLI, perform the following setup steps:
- Create a
CLOJURESCRIPT_HOMEenvironment variable pointing to your ClojureScript root directory. - Run the bootstrap script located at
$CLOJURESCRIPT_HOME/script/bootstrap. - Add
$CLOJURESCRIPT_HOME/binto your systemPATH.
- Create a
Add ClojureScript to your project dependencies
masterDepending on your build tool, use the following dependency configurations to include ClojureScript (version 1.12.145) in your project.
### Clojure deps.edn ```clojure org.clojure/clojurescript {:mvn/version "1.12.145"}Leiningen
[org.clojure/clojurescript "1.12.145"]Maven
<dependency> <groupId>org.clojure</groupId> <artifactId>clojurescript</artifactId> <version>1.12.145</version> </dependency>Compile in Advanced Mode using cljsc
masterTo use Closure Compiler's "advanced" optimization setting via
cljsc, pass a Clojure map of compiler options.Advanced mode produces a single
.jsfile (twitterbuzz.js), requiring only one<script>tag in your HTML. To view the application compiled with these optimizations, openindex-advanced.html.cljsc src '{:optimizations :advanced}' > twitterbuzz.jsCompile in Development Mode using the REPL
masterFor faster development cycles, you can compile using a Clojure REPL.
- Start the REPL using
script/repl. - Once the REPL is active, evaluate the following code to build the project. Note that
:output-diris set tosamples/twitterbuzz/outbecause theindex.htmlscript tag expects files in that directory.
For more compilation examples, consult the
cljs.closuresource code.(use 'cljs.closure) (def opts {:output-to "samples/twitterbuzz/twitterbuzz.js" :output-dir "samples/twitterbuzz/out"}) (build "samples/twitterbuzz/src" opts)- Start the REPL using
Compile ClojureScript for Development Mode
masterTo compile your ClojureScript source files for development, use the
cljsccommand. This mode loads each file in a separate script tag, making it easier to track errors to specific files.After compiling, open
hello-dev.htmlin your browser to view the result.cljsc src > hello.jsBuild a ClojureScript project for browser evaluation
masterTo prepare a project for a browser-connected REPL, you must first build the ClojureScript source files into JavaScript. This requires an HTML file and a ClojureScript file to establish the connection.
- Navigate to the project directory.
- Launch a Clojure REPL using the provided script.
- Use
cljs.closureto build the source directory into a target output directory.
cd samples/repl ../../script/repl ;; Inside the Clojure REPL: (require '[cljs.closure :as cljsc]) (def opts {:output-to "main.js" :output-dir "out"}) (cljsc/build "src" opts)Start a browser-connected ClojureScript REPL
masterA browser-connected REPL allows you to evaluate code where side-effects occur directly in the browser (e.g., manipulating the DOM or showing alerts).
To start the REPL using the browser as the evaluator, follow these steps in your Clojure REPL:
- Require
cljs.repl. - Require
cljs.repl.browser. - Create a new evaluation environment using
browser/repl-env. - Start the REPL with that environment using
repl/repl.
After starting, open
http://localhost:9000/in your browser (or serve your localindex.htmlvia a webserver) to connect.(require '[cljs.repl :as repl]) (require '[cljs.repl.browser :as browser]) (def env (browser/repl-env)) (repl/repl env)- Require
Run the string-requires-npm-deps demo
masterTo run this demonstration, you must first bootstrap the ClojureScript repository and then useclojure.mainto execute the build script. This demo showcases string-based requires in ClojureScript.