Convert Figma logo to code with AI

clojure logoclojurescript

Clojure to JS compiler

9,272
790
9,272
7

Top Related Projects

ClojureScript compilation made easy

4,811

A minimalistic ClojureScript interface to React.js

A ClojureScript framework for building user interfaces, leveraging React

Quick Overview

ClojureScript is a dialect of the Clojure programming language that compiles to JavaScript. It allows developers to write client-side web applications in a functional, immutable style, leveraging the power and expressiveness of the Clojure language while targeting the ubiquitous JavaScript runtime.

Pros

  • Functional Programming: ClojureScript embraces the functional programming paradigm, promoting immutability, higher-order functions, and a focus on data transformation, which can lead to more concise, expressive, and maintainable code.
  • Interoperability with JavaScript: ClojureScript seamlessly interoperates with existing JavaScript libraries and frameworks, allowing developers to leverage the vast ecosystem of JavaScript tools and libraries.
  • Powerful Tooling: The ClojureScript ecosystem provides a rich set of tools, including a powerful compiler, a REPL (Read-Eval-Print Loop) for interactive development, and a growing collection of libraries and frameworks.
  • Performance: ClojureScript's compilation to highly optimized JavaScript can result in efficient client-side applications, often outperforming traditional JavaScript-based approaches.

Cons

  • Learning Curve: Clojure and ClojureScript have a steeper learning curve compared to more mainstream JavaScript frameworks, which may be a barrier for developers new to functional programming.
  • Ecosystem Size: While the ClojureScript ecosystem is growing, it is still smaller than the vast JavaScript ecosystem, which may limit the availability of pre-built libraries and tools for certain use cases.
  • Tooling Maturity: Some of the tooling and development workflows around ClojureScript may not be as mature as those found in the broader JavaScript ecosystem, which can make the initial setup and configuration more challenging.
  • Adoption: ClojureScript, while gaining popularity, is still a relatively niche language compared to the widespread use of JavaScript, which may make it harder to find experienced developers or integrate with existing JavaScript-based projects.

Code Examples

Here are a few examples of ClojureScript code:

Creating a Simple Counter

(ns example.core
  (:require [reagent.core :as r]))

(defn counter []
  (let [count (r/atom 0)]
    (fn []
      [:div
       [:h1 @count]
       [:button {:on-click #(swap! count inc)} "Increment"]])))

(r/render [counter] (js/document.getElementById "app"))

This code creates a simple counter component using the Reagent library, which provides a ClojureScript wrapper around React.

Fetching Data from an API

(ns example.core
  (:require [cljs-http.client :as http]
            [cljs.core.async :refer [<!]]))

(defn fetch-data []
  (go
    (let [response (<! (http/get "https://api.example.com/data"))]
      (println (:body response)))))

(fetch-data)

This example demonstrates how to use the cljs-http library to make an HTTP GET request and handle the asynchronous response using core.async.

Defining a Reusable Component

(ns example.components
  (:require [reagent.core :as r]))

(defn button [label on-click]
  [:button {:on-click on-click} label])

(defn app []
  [:div
   [button "Click me!" #(println "Button clicked!")]])

(r/render [app] (js/document.getElementById "app"))

This code defines a reusable button component that can be used within a larger application. The app function then uses the button component to create a simple application.

Getting Started

To get started with ClojureScript, you'll need to have Clojure and Node.js installed on your system. Here's a quick guide to set up a new ClojureScript project:

  1. Install Clojure and Leiningen (a popular Clojure build tool):

  2. Create a new ClojureScript project using Leiningen:

    lein new figwheel-main my-
    

Competitor Comparisons

ClojureScript compilation made easy

Pros of shadow-cljs

  • Faster Compilation: shadow-cljs is known for its fast compilation times, which can be a significant advantage for larger projects.
  • Comprehensive Tooling: shadow-cljs provides a rich set of tools and features, including a development server, hot reloading, and advanced build configurations.
  • Seamless Node.js Integration: shadow-cljs makes it easy to integrate ClojureScript with Node.js, allowing for server-side rendering and other server-side use cases.

Cons of shadow-cljs

  • Steeper Learning Curve: shadow-cljs has a more complex configuration compared to the standard ClojureScript setup, which may be a barrier for some users.
  • Limited Community Support: While shadow-cljs has a dedicated following, the ClojureScript community is generally more focused on the official ClojureScript compiler.
  • Potential Vendor Lock-in: By using shadow-cljs, you may become more dependent on the tool and its ecosystem, which could make it harder to switch to other ClojureScript tooling in the future.

Code Comparison

Here's a brief comparison of the configuration for a simple ClojureScript project using clojure/clojurescript and thheller/shadow-cljs:

clojure/clojurescript

(defproject my-project "0.1.0-SNAPSHOT"
  :dependencies [[org.clojure/clojure "1.10.1"]
                 [org.clojure/clojurescript "1.10.773"]]
  :plugins [[lein-cljsbuild "1.1.8"]]
  :cljsbuild {:builds [{:id "dev"
                       :source-paths ["src"]
                       :compiler {:main my-project.core
                                  :output-to "resources/public/js/main.js"
                                  :output-dir "resources/public/js"
                                  :asset-path "/js"
                                  :optimizations :none
                                  :source-map true}}]})

shadow-cljs

{:source-paths ["src"]
 :dependencies [[org.clojure/clojure "1.10.1"]
                [org.clojure/clojurescript "1.10.773"]]
 :builds {:app {:target :browser
                :output-dir "resources/public/js"
                :asset-path "/js"
                :modules {:main {:entries [my-project.core]}}
                :devtools {:http-root "resources/public"
                           :http-port 3000}}}}
4,811

A minimalistic ClojureScript interface to React.js

Pros of Reagent

  • Reagent provides a simple and lightweight wrapper around React, making it easier to use React in a Clojure(Script) context.
  • Reagent's component-based approach aligns well with the functional programming principles of Clojure(Script), promoting modular and reusable UI components.
  • Reagent's integration with Clojure(Script)'s data structures and immutable state management makes it a natural choice for building reactive and efficient user interfaces.

Cons of Reagent

  • Reagent has a smaller community and ecosystem compared to ClojureScript, which may limit the availability of third-party libraries and resources.
  • Reagent's simplicity and minimalism can be a double-edged sword, as it may require more boilerplate code or custom implementation for certain advanced features.
  • Debugging and troubleshooting Reagent-based applications may be more challenging, as the integration with React can add an additional layer of complexity.

Code Comparison

ClojureScript (from clojure/clojurescript):

(defn hello-world []
  [:div
   [:h1 "Hello, World!"]
   [:p "This is a ClojureScript example."]])

(defn ^:export main []
  (js/ReactDOM.render
   (hello-world)
   (js/document.getElementById "app")))

Reagent (from reagent-project/reagent):

(defn hello-world []
  [:div
   [:h1 "Hello, World!"]
   [:p "This is a Reagent example."]])

(defn ^:export main []
  (reagent.dom/render
   [hello-world]
   (js/document.getElementById "app")))

A ClojureScript framework for building user interfaces, leveraging React

Pros of re-frame

  • re-frame is a well-established and widely-used framework for building complex web applications with ClojureScript, providing a clear and opinionated structure for managing application state and UI.
  • re-frame comes with a rich ecosystem of libraries and tooling, making it easier to build and maintain large-scale ClojureScript applications.
  • re-frame's focus on functional programming and reactive programming principles can lead to more maintainable and testable code.

Cons of re-frame

  • re-frame has a steeper learning curve compared to ClojureScript alone, as it introduces its own set of concepts and patterns that developers need to understand.
  • re-frame's opinionated nature may not align with the preferences of all developers, and it may be more difficult to deviate from the recommended practices.
  • The re-frame ecosystem is smaller compared to the broader ClojureScript ecosystem, which may limit the availability of third-party libraries and tools.

Code Comparison

ClojureScript (from clojure/clojurescript):

(defn hello-world []
  [:div "Hello, World!"])

(defn ^:export main []
  (js/ReactDOM.render (hello-world) (js/document.getElementById "app")))

re-frame (from day8/re-frame):

(ns my-app.core
  (:require [re-frame.core :as re-frame]))

(re-frame/reg-event-db
 ::initialize-db
 (fn [_ _]
   {:message "Hello, World!"}))

(re-frame/reg-sub
 ::message
 (fn [db _]
   (:message db)))

(defn hello-world []
  [:div @(re-frame/subscribe [::message])])

(defn ^:export main []
  (re-frame/dispatch [::initialize-db])
  (js/ReactDOM.render [hello-world] (js/document.getElementById "app")))

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

What is ClojureScript?

ClojureScript is a compiler for Clojure that targets JavaScript. It is designed to emit JavaScript code which is compatible with the advanced compilation mode of the Google Closure optimizing compiler.

Official web site: https://clojurescript.org

Releases and dependency information

Latest stable release: 1.11.132

Clojure deps.edn dependency information:

org.clojure/clojurescript {:mvn/version "1.11.132"}

Leiningen dependency information:

[org.clojure/clojurescript "1.11.132"]

Maven dependency information:

<dependency>
  <groupId>org.clojure</groupId>
  <artifactId>clojurescript</artifactId>
  <version>1.11.132</version>
</dependency>

Getting Started

Questions, Feedback?

Please point all of your questions and feedback to the Clojure mailing list. There is a community run ClojureScript user mailing list and the IRC channel, #clojurescript on freenode.net, is quite active. There is also a community run Slack channel. The Jira bug/feature tracking application is located at https://clojure.atlassian.net/browse/CLJS. Before submitting issues please read the Reporting Issues page first.

Developers Welcome

ClojureScript operates under the same license as Clojure. All contributors must have a signed CA (Contributor's Agreement) and submit their patch via the appropriate channels. If you're interested in contributing to the project, please see the contributing page on clojure.org. For more information about working on the compiler and testing check the Developer section of the wiki.

YourKit

YourKit has given an open source license for their profiler, greatly simplifying the profiling of ClojureScript performance.

YourKit supports open source projects with its full-featured Java Profiler. YourKit, LLC is the creator of YourKit Java Profiler and YourKit .NET Profiler, innovative and intelligent tools for profiling Java and .NET applications.

License

Copyright (c) Rich Hickey. All rights reserved. The use and
distribution terms for this software are covered by the Eclipse
Public License 1.0 (https://opensource.org/license/epl-1-0/)
which can be found in the file epl-v10.html at the root of this
distribution. By using this software in any fashion, you are
agreeing to be bound by the terms of this license. You must
not remove this notice, or any other, from this software.