ClojureScript

repository·master·Indexed 27 days ago

https://github.com/clojure/clojurescript

A 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).

Tokens
3.2K
Snippets
14
Records
25
Agent score
90%

What's inside ClojureScript

  1. Use Externs with Advanced Optimizations

    master

    When 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 :externs option.

    cljsc src '{:optimizations :advanced :output-to "hello-extern.js" :externs ["externs.js"]}'
  2. One-time Setup for ClojureScript

    master

    Before using the ClojureScript CLI, perform the following setup steps:

    1. Create a CLOJURESCRIPT_HOME environment variable pointing to your ClojureScript root directory.
    2. Run the bootstrap script located at $CLOJURESCRIPT_HOME/script/bootstrap.
    3. Add $CLOJURESCRIPT_HOME/bin to your system PATH.
  3. Add ClojureScript to your project dependencies

    master

    Depending 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>
  4. Compile in Advanced Mode using cljsc

    master

    To use Closure Compiler's "advanced" optimization setting via cljsc, pass a Clojure map of compiler options.

    Advanced mode produces a single .js file (twitterbuzz.js), requiring only one <script> tag in your HTML. To view the application compiled with these optimizations, open index-advanced.html.

    cljsc src '{:optimizations :advanced}' > twitterbuzz.js
  5. Compile in Development Mode using the REPL

    master

    For faster development cycles, you can compile using a Clojure REPL.

    1. Start the REPL using script/repl.
    2. Once the REPL is active, evaluate the following code to build the project. Note that :output-dir is set to samples/twitterbuzz/out because the index.html script tag expects files in that directory.

    For more compilation examples, consult the cljs.closure source code.

    (use 'cljs.closure)
    (def opts {:output-to "samples/twitterbuzz/twitterbuzz.js" :output-dir "samples/twitterbuzz/out"})
    
    (build "samples/twitterbuzz/src" opts)
  6. Compile ClojureScript for Development Mode

    master

    To compile your ClojureScript source files for development, use the cljsc command. This mode loads each file in a separate script tag, making it easier to track errors to specific files.

    After compiling, open hello-dev.html in your browser to view the result.

    cljsc src > hello.js
  7. Build a ClojureScript project for browser evaluation

    master

    To 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.

    1. Navigate to the project directory.
    2. Launch a Clojure REPL using the provided script.
    3. Use cljs.closure to 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)
  8. Start a browser-connected ClojureScript REPL

    master

    A 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:

    1. Require cljs.repl.
    2. Require cljs.repl.browser.
    3. Create a new evaluation environment using browser/repl-env.
    4. Start the REPL with that environment using repl/repl.

    After starting, open http://localhost:9000/ in your browser (or serve your local index.html via a webserver) to connect.

    (require '[cljs.repl :as repl])
    (require '[cljs.repl.browser :as browser])
    (def env (browser/repl-env))
    (repl/repl env)