### Start ClojureScript REPL Source: https://github.com/clojure/clojurescript/blob/master/devnotes/README.org Use these commands to require the compiler, set up a REPL environment, and start the REPL. ```clojure (require '[cljs.compiler :as comp]) (def jse (comp/repl-env)) (comp/repl jse) ``` -------------------------------- ### Start Browser REPL Environment in ClojureScript Source: https://github.com/clojure/clojurescript/wiki/The-REPL-and-Evaluation-Environments Initialize and start a ClojureScript REPL that connects to a browser environment. This requires the browser implementation of IJavaScriptEnv and then starts the REPL with the specified environment. ```clojure (require '[cljs.repl :as repl]) (require '[cljs.repl.browser :as browser]) ;; require the browser implementation of IJavaScriptEnv (def env (browser/repl-env)) ;; create a new environment (repl/repl env) ;; start the REPL ``` -------------------------------- ### Start Browser-Connected REPL Source: https://github.com/clojure/clojurescript/blob/master/samples/repl/README.md Starts a ClojureScript REPL using the browser as the evaluation environment. This requires the 'cljs.repl' and 'cljs.repl.browser' namespaces. ```clojure (require '[cljs.repl :as repl]) (require '[cljs.repl.browser :as browser]) (def env (browser/repl-env)) (repl/repl env) ``` -------------------------------- ### Install V8 via Homebrew Source: https://github.com/clojure/clojurescript/wiki/Running-the-tests Install V8 using Homebrew and set the V8_HOME environment variable to the Homebrew binary path. ```shell brew install v8 ``` ```shell export V8_HOME="/usr/local/bin" ``` -------------------------------- ### Node.js REPL Example Source: https://github.com/clojure/clojurescript/wiki/Custom-REPLs This example demonstrates the new pattern for ClojureScript REPLs, relying on the Node.js runtime to interpret `goog.require`. It's concise due to this reliance. ```clojure (require '[cljs.repl :as repl]) (require '[cljs.repl.node :as node]) (defmacro -setup [opts] `(node/repl-env ~opts)) (defmacro - teardown [env] `(node/repl-env- teardown ~env)) (defmacro -eval [env form] `(node/repl-env-eval ~env ~form)) (defmacro -complete [env token prefix] `(node/repl-env-complete ~env ~token ~prefix)) ``` -------------------------------- ### Start ClojureScript REPL with Rhino Environment Source: https://github.com/clojure/clojurescript/wiki/The-REPL-and-Evaluation-Environments Use this snippet to initiate the ClojureScript REPL within the Rhino JavaScript environment. Ensure you have the necessary ClojureScript libraries installed. ```clojure (require '[cljs.repl :as repl]) (require '[cljs.repl.rhino :as rhino]) ;; require the rhino implementation of IJavaScriptEnv (def env (rhino/repl-env)) ;; create a new environment (repl/repl env) ;; start the REPL ``` -------------------------------- ### Example CommonJS Module Source: https://github.com/clojure/clojurescript/wiki/JavaScript-Module-Support-(Alpha) A simple JavaScript module demonstrating basic arithmetic functions. ```javascript var calculator = { add: function (a, b) { return a + b; }, subtract: function (a, b) { return a - b; } }; module.exports = calculator; ``` -------------------------------- ### Build ClojureScript for Maven Source: https://github.com/clojure/clojurescript/wiki/Building-the-compiler Run this script from the repository root to build ClojureScript and install it into your local Maven repository. The output will indicate the version installed. ```sh script/build ``` -------------------------------- ### Source Map Configuration with Optimizations Source: https://github.com/clojure/clojurescript/wiki/Source-maps When using optimizations other than :none, ensure that `:output-to`, `:output-dir`, and `:source-map` share the same parent directory. This example shows a valid configuration. ```clojure {:output-to "resources/public/js/compiled/main.js" :output-dir "resources/public/js/compiled" :optimizations :simple :source-map "resources/public/js/compiled/main.js.map"} ``` -------------------------------- ### Example Project Structure for cljc Source: https://github.com/clojure/clojurescript/wiki/Using-cljc Illustrates a common project structure for applications using clj, cljs, and cljc files. This separation helps manage platform-specific code and shared logic. ```clojure src |-- clj | |-- feed | |-- api.clj | |-- db.clj |-- cljs | |-- feed | |-- components.cljs | |-- store.cljs |-- cljc | |-- feed | |-- util.cljc | |-- schema.cljc ``` -------------------------------- ### Install ClojureScript from Master Source: https://github.com/clojure/clojurescript/wiki/Reporting-Issues This script installs ClojureScript into your local Maven repository. It's useful for verifying if an issue has been resolved in master, but not for reporting new issues. ```bash ./script/build ``` -------------------------------- ### Install ClojureScript Prerequisites with Puppet Source: https://github.com/clojure/clojurescript/wiki/ClojureScript-Puppet-Manifest This Puppet manifest ensures essential packages like git-core, curl, libssl-dev, libreadline5-dev, and unzip are installed. The default-jdk package is commented out due to a known bug with ClojureScript. ```puppet class lucid32 { package { "git-core": ensure => present, } package { "curl": ensure => present, } package {"libssl-dev": ensure => present, } package {"libreadline5-dev": ensure => present, } # package{"default-jdk": # ensure => present, # } package {"unzip": ensure => present, } } ``` -------------------------------- ### Start ClojureScript REPL Remotely Source: https://github.com/clojure/clojurescript/wiki/Remote-REPL This Clojure code snippet initializes a ClojureScript REPL environment targeting a remote NodeJS host. It requires Piggieback and specifies the output directory for compiled files. ```clojure (ns mirabox-sandbox-clojure.node-repl (:require [cemerick.piggieback :as piggie] [cljs.repl.node :as node] [clojure.java.io :as io])) (piggie/cljs-repl (node/repl-env :host "repl-host") :node-command "repl-host-node.sh" :output-dir "/tmp/cljs-repl-share") ``` -------------------------------- ### Create goog.jar in ClojureScript Directory Source: https://github.com/clojure/clojurescript/wiki/Windows-Setup This command sequence creates a goog.jar file from the closure library and copies it to the lib directory, a necessary step for Windows Command Prompt setup. ```shell cd closure\library\closure jar cf goog.jar goog copy goog.jar ..\..\..\lib ``` -------------------------------- ### ClojureScript doto macro example Source: https://github.com/clojure/clojurescript/wiki/FAQ-(for-JavaScript-developers) Demonstrates the use of the `doto` macro for chaining method calls on an object, providing a more concise syntax than equivalent JavaScript. ```clojure (def my-date (doto (new Date) (.setDate 7) (.setMonth 7) (.setFullYear 1991))) ``` ```clojure (def my-date (let [d (new Date)] (.setDate d 7) (.setMonth d 7) (.setFullYear d 1991) d)) ``` -------------------------------- ### ClojureScript REPL Stack Trace Example Source: https://github.com/clojure/clojurescript/wiki/REPL-stack-traces This is an example of a typical stack trace encountered in the ClojureScript REPL. It shows the evaluation of a ClojureScript form, the generated JavaScript, and the underlying Java exception. Focus on lines indicating the original form, generated JS, and the .cljs file/line number for debugging. ```text 1. "Error evaluating:" (cljs.core.prn (pr-str 5)) 2. :as "cljs.core.fn_of_(cljs.core.prn)(cljs.core.fn_of_(cljs.core.pr_str)(5));\n" 3. sun.org.mozilla.javascript.internal.EcmaError: TypeError: 4. sun.org.mozilla.javascript.internal.Undefined@397577f9 is not a function, 5. it is sun.org.mozilla.javascript.internal.Undefined. (cljs/core.cljs#1688) 6. at sun.org.mozilla.javascript.internal.ScriptRuntime.constructError(ScriptRuntime.java:3224) 7. at sun.org.mozilla.javascript.internal.ScriptRuntime.constructError(ScriptRuntime.java:3214) 8. at sun.org.mozilla.javascript.internal.ScriptRuntime.typeError(ScriptRuntime.java:3230) 9. at sun.org.mozilla.javascript.internal.ScriptRuntime.typeError2(ScriptRuntime.java:3249) 10. at sun.org.mozilla.javascript.internal.ScriptRuntime.notFunctionError(ScriptRuntime.java:3304) 11. at sun.org.mozilla.javascript.internal.ScriptRuntime.notFunctionError(ScriptRuntime.java:3292) 12. at sun.org.mozilla.javascript.internal.Interpreter.interpretLoop(Interpreter.java:3104) 13. at script.pr_str_with_opts(cljs/core.cljs:1688) 14. at script() 15. [...snip...] ``` -------------------------------- ### Search Closure Library on GitHub for keywords Source: https://github.com/clojure/clojurescript/wiki/Google-Closure-Library Search the Closure Library repository on GitHub using keywords to find relevant functions or examples. ```html Search Closure Library on Github: "[hours minutes](https://github.com/google/closure-library/search?utf8=%E2%9C%93&q=hours+minutes)" ``` -------------------------------- ### JavaScript equivalent of doto macro Source: https://github.com/clojure/clojurescript/wiki/FAQ-(for-JavaScript-developers) Shows the equivalent imperative JavaScript code for the ClojureScript `doto` macro example, highlighting the verbosity difference. ```javascript var myDate = (function(){ var d = new Date(); d.setDate(7); d.setMonth(7); d.setFullYear(1991); return d; }()); ``` -------------------------------- ### deps.cljs for jQuery with Dependencies Source: https://github.com/clojure/clojurescript/wiki/Packaging-Foreign-Dependencies Illustrates packaging multiple foreign libraries (jQuery, UI Core, Autocomplete) with explicit dependency declarations using the `:requires` vector. This setup is for libraries that depend on each other. ```clojure {:foreign-libs [{:file "jquery/jquery.js" :file-min "jquery/jquery.min.js" :provides ["org.jquery.jQuery"]} ; Provides jQuery {:file "jquery/ui/core.js" :file-min "jquery/ui/core.min.js" :provides ["org.jquery.ui.Core"] :requires ["org.jquery.jQuery"]} ; Requires jQuery {:file "jquery/ui/autocomplete.js" :file-min "jquery/ui/autocomplete.min.js" :provides ["org.jquery.ui.Autocomplete"] :requires ["org.jquery.ui.Core"]}] ; Requires UI Core :externs ["jquery/jquery.js" "jquery/jquery.ui.js"]} ``` -------------------------------- ### CommonJS Module Transformation Example Source: https://github.com/clojure/clojurescript/wiki/Google-Summer-of-Code-2015 Illustrates how a simple CommonJS module exporting a 'hello' object with a 'greet' function is transformed by the Google Closure Compiler into a Closure-compatible format. ```javascript var my = {}; my.hello = { greet: function(name) { return "Hello " + name; } }; exports.hello = my.hello; ``` -------------------------------- ### Define IJavaScriptEnv Protocol Source: https://github.com/clojure/clojurescript/wiki/The-REPL-and-Evaluation-Environments Implement this protocol to create a new JavaScript evaluation environment. Functions like setup and tear-down handle environment creation and destruction, while evaluate executes JavaScript code and load handles namespace loading. ```clojure (defprotocol IJavaScriptEnv (-setup [this opts]) (-evaluate [this filename line js]) (-load [this ns url]) (-tear-down [this])) ``` -------------------------------- ### Bootstrap Dependencies Source: https://github.com/clojure/clojurescript/wiki/Running-the-tests Run the bootstrap script to set up project dependencies. ```shell ./script/bootstrap ``` -------------------------------- ### Configure System JavaScriptCore Path Source: https://github.com/clojure/clojurescript/wiki/Running-the-tests Add the system's JavaScriptCore framework to your PATH for testing. ```shell PATH=$PATH:/System/Library/Frameworks/JavaScriptCore.framework/Resources ``` -------------------------------- ### Run All Tests Source: https://github.com/clojure/clojurescript/wiki/Running-the-tests Execute the main test script for the project. ```shell ./script/test ``` -------------------------------- ### ClojureScript HTML templating example Source: https://github.com/clojure/clojurescript/wiki/FAQ-(for-JavaScript-developers) An example of using ClojureScript's data notation for embedding HTML templates directly within the language, showcasing its lightweight syntax. ```clojure [:div.text-right [span "Click here: "] [:button {:class "btn" :on-click #(do something)} "Click me!"]] ``` -------------------------------- ### Build ClojureScript Project Source: https://github.com/clojure/clojurescript/blob/master/samples/string-requires-npm-deps/README.md Use this command to build the ClojureScript project. Ensure you are in the correct directory and have the necessary Java classpath set up. ```shell java -cp `ls ../../lib/*.jar | paste -sd ":" -`:../../src/main/cljs:../../src/main/clojure:src clojure.main build.clj ``` -------------------------------- ### Basic ClojureScript REPL Operations Source: https://github.com/clojure/clojurescript/wiki/The-REPL-and-Evaluation-Environments Examples of fundamental ClojureScript expressions that can be evaluated in a REPL, including arithmetic, map literals, and function definitions. ```clojure ;; the basics (+ 1 1) (:a {:a :b}) (reduce + [1 2 3 4 5]) (defn sum [coll] (reduce + coll)) (sum [2 2 2 2]) ``` -------------------------------- ### Compile TwitterBuzz Demo with REPL in Development Mode Source: https://github.com/clojure/clojurescript/blob/master/samples/twitterbuzz/README.md Compile the demo using the REPL for faster development cycles. Ensure the :output-dir matches the script tag in index.html. ```clojure (use 'cljs.closure) (def opts {:output-to "samples/twitterbuzz/twitterbuzz.js" :output-dir "samples/twitterbuzz/out"}) (build "samples/twitterbuzz/src" opts) ``` -------------------------------- ### Greet and Sum in ClojureScript Source: https://github.com/clojure/clojurescript/blob/master/samples/hello/hello-dev.html Displays a greeting and the sum of a list of numbers using ClojureScript. Ensure the 'hello.core' namespace is required. ```clojurescript goog.require('hello.core'); alert(greet("ClojureScript")); alert("The sum of \[1,2,3,4,5,6,7,8,9\] is: " + hello.core.sum(\[1,2,3,4,5,6,7,8,9\])); ``` -------------------------------- ### Browser-Specific Side-Effect in ClojureScript REPL Source: https://github.com/clojure/clojurescript/wiki/The-REPL-and-Evaluation-Environments Execute JavaScript functions directly from the ClojureScript REPL to interact with the browser. This example shows how to trigger a JavaScript alert. ```clojure ;; browser specific (js/alert "I am an evil side-effect") ``` -------------------------------- ### Run Compiler Infrastructure Tests Source: https://github.com/clojure/clojurescript/wiki/Running-the-tests Execute tests for the analyzer, compiler, and closure components using Leiningen. ```shell lein test ``` -------------------------------- ### Externs for Foreign Library Object Methods Source: https://github.com/clojure/clojurescript/wiki/Advanced-Compilation Define externs for methods on an object when there isn't a top-level API. This example defines externs for `Object.foo` and `Object.bar`. ```javascript Object.foo = function() {}; Object.bar = function() {}; ``` -------------------------------- ### Build ClojureScript Project Source: https://github.com/clojure/clojurescript/blob/master/samples/repl/README.md Builds a ClojureScript project using cljs.closure. Ensure you are in the 'samples/repl' directory before running. ```bash cd samples/repl ../../script/repl ``` ```clojure (require '[cljs.closure :as cljsc]) (def opts {:output-to "main.js" :output-dir "out"}) (cljsc/build "src" opts) ``` -------------------------------- ### Declare NPM Dependencies in deps.cljs Source: https://github.com/clojure/clojurescript/wiki/Enhanced-Node.js-Modules-Support Specify Node.js NPM dependencies within your `deps.cljs` file. This allows the build system to manage and potentially install these packages. ```clojure { ;;.. :npm-deps {"react" "15.4.2" "object-assign" "4.1.1"} ;; ... } ``` -------------------------------- ### Display Greeting and Sum in ClojureScript Source: https://github.com/clojure/clojurescript/blob/master/samples/hello/hello.html This snippet shows how to display a greeting and the sum of a list of numbers using ClojureScript. Ensure the `greet` function and `hello.core.sum` are defined. ```javascript alert(greet("ClojureScript")); alert("The sum of [1,2,3,4,5,6,7,8,9] is: " + hello.core.sum([1,2,3,4,5,6,7,8,9])); ``` -------------------------------- ### Compile TwitterBuzz Demo in Development Mode Source: https://github.com/clojure/clojurescript/blob/master/samples/twitterbuzz/README.md Use this command to compile the demo in development mode. After compilation, open index.html to view the application. ```bash cljsc src > twitterbuzz.js ``` -------------------------------- ### IJavaScriptEnv Protocol Source: https://github.com/clojure/clojurescript/wiki/The-REPL-and-Evaluation-Environments The IJavaScriptEnv protocol defines the interface for creating and managing JavaScript evaluation environments. Implementations handle setup, evaluation, loading, and teardown of these environments. ```APIDOC ## Protocol: IJavaScriptEnv ### Description Defines the interface for JavaScript evaluation environments. ### Methods - **-setup [this opts]**: Initializes the JavaScript evaluation environment. - **-evaluate [this filename line js]**: Evaluates a JavaScript string in the environment. - **-load [this ns url]**: Loads JavaScript from a URL into the environment for specified namespaces. - **-tear-down [this]**: Cleans up and destroys the JavaScript evaluation environment. ### -evaluate Details - **Parameters**: - `filename` (string): The name of the file where the JavaScript originated. - `line` (number): The line number in the file where the JavaScript originated. - `js` (string): The JavaScript code to evaluate. - **Returns**: A map with `:status` and `:value` keys. - `:status` can be `:success`, `:error`, or `:exception`. - `:value` contains the result or error message. - May include a `:stacktrace` key if an exception occurred. ``` -------------------------------- ### Evaluate Basic ClojureScript Forms in Browser REPL Source: https://github.com/clojure/clojurescript/blob/master/samples/repl/README.md Demonstrates evaluating basic arithmetic, data structures, strings, and JavaScript interop (like alerts) within a browser-connected REPL. ```clojure ;; Evaluate some basic forms. (+ 1 1) {:a :b} "hello" (reduce + [1 2 3 4 5]) (js/alert "Hello World!") ``` -------------------------------- ### Define and Call Functions in Browser REPL Source: https://github.com/clojure/clojurescript/blob/master/samples/repl/README.md Illustrates defining a custom function within the REPL and then calling it with arguments. ```clojure ;; Define functions and call them. (defn sum [coll] (reduce + coll)) (sum [2 2 2 2]) ``` -------------------------------- ### Get Module Name using ProcessCommonJsModules Source: https://github.com/clojure/clojurescript/wiki/Google-Summer-of-Code-2015 Demonstrates how to use the `toModuleName` method from `ProcessCommonJsModules` to retrieve the generated module name for a JavaScript module, which is needed for namespace mapping. ```clojure (ProcessCommonJsModules/toModuleName "hello.js") ;; returns module$hello ``` -------------------------------- ### DOM Manipulation in ClojureScript REPL Source: https://github.com/clojure/clojurescript/wiki/The-REPL-and-Evaluation-Environments Interact with the HTML Document Object Model (DOM) using ClojureScript. This example appends a new text node to an element with the ID 'content'. ```clojure (ns test.dom (:require [clojure.browser.dom :as dom])) (dom/append (dom/get-element "content") (dom/element "ClojureScript is all up in your DOM.")) ``` -------------------------------- ### Encapsulate State with Closure using reify Source: https://github.com/clojure/clojurescript/wiki/Working-with-Javascript-classes This example demonstrates how to achieve private state encapsulation by using a closure. The `store` variable is local to the `bag` function and is not directly accessible from outside. ```clojure (defn bag [] (let [store (create-store)] (reify Object (add [this x] (.push store x)) (print [this x] (.log js/console store))))) ``` -------------------------------- ### JavaScript Object Instantiation in ClojureScript Source: https://github.com/clojure/clojurescript/blob/master/devnotes/cljs.org Demonstrates how to instantiate JavaScript objects from ClojureScript, mirroring Java-like constructor calls. ```clojure (new Foo 1 2 3) ``` ```javascript (new Foo(1,2,3)) ```