How can I create a basic ClojureScript Hello World app in Lighttable?
Asked Answered
L

3

15

The documentation seems quite sparse in LightTable. I want to create a very bare bones ClojureScript web application in LightTable as a starting point to build on. I have the Instarepl in Clojure working fine, and then I create a new file called dummy.cljs containing the following:

(ns dummy)

(js/alert "Hello lighttable")

How can I run this?

Update

I have figured this out now, and I will post a video on how todo it as it is quite visual.

Update 2

Here is the video:

http://www.youtube.com/watch?v=GZ6e0tKqYas

Lole answered 28/12, 2012 at 12:14 Comment(0)
K
14
  1. you should first create a project via lein( https://github.com/technomancy/leiningen) as described here
  2. then add cljsbuild to your project, as described here
  3. run from terminal(or cmd on win) simultaneously lein ring server and lein cljsbuild auto, in order to run local web-server with clojurescript autocompile
  4. connect LightTable(via console at the bottom) to the project you've created in step 1
  5. now you can edit clojure and cljs in LightTable and changes should be delivered to your browser. But don't forget to check terminal for errors, as they probably won't be shown in LT.

Simpler way would be using http://clojurescriptone.com/ and lein repl as main development tool and use LT only as additional tool for solving some small problems within one-two files.

Kittrell answered 7/1, 2013 at 22:5 Comment(2)
You might also want to take a look at github.com/konrad-garus/cljs-kickoff to get a minimal leiningen project for getting clojurescript running on a web app.Batha
For even more minimal project: github.com/danielribeiro/hello-clojurescriptGurge
I
3

Well, LightTable is good as REPL in development phase, but when you're done, you need to compile the ClojureScript to be executed (i.e., with node.js).

Setup for LightTable

  1. Open a clojurescript file with cljs extension.
  2. Click Control-space
  3. Select connect: Add Connection, and select LightTable

enter image description here

enter image description here

Then, you can give the ClojureScript expression, and evaluate it with command-enter or shift-command-enter key (with Mac OS X).

enter image description here

Setup for node.js

The simplest way is to use lein, but if you don't want to use lein, this is one of the possible ways.

Step1: Download cljs.jar compiler

Or download a newer version if available.

Step2: Create source directory and create files

└── src
    ├── build.clj
    └── smcho
        └── core.cljs

build.clj is as follows, you can change the namespace and accordingly the directory name as necessary.

(require 'cljs.build.api)
(cljs.build.api/build "src"
  {:main 'smcho.core
    :output-to "main.js"
    :target :nodejs})

This is ClojureScript code; the main method is added.

(ns smcho.core
    (:require [cljs.nodejs :as nodejs]))

(nodejs/enable-util-print!)

(defn factorial [x]
    (reduce * (range 1 (inc x))))

(defn fib [n]
    (if (<= n 1)
        1
        (+ (fib (- n 1)) (fib (- n 2)))))

(defn sort-seq []
    (sort (repeat 100 (rand-int 2000))))

(defn time-fun [fun]
    (let [start  (.getTime (js/Date.))
                _      (fun)
                end    (.getTime (js/Date.))
                result (- end start)]
        result))

(defn time-it [fun]
    (let [values  (for [i (range 200)] (time-fun fun))]
        (/ (apply + values)
             (count values))))

(defn -main []
    (println "(factorial 5000) \t Avg: " (time-it #(factorial 5000)))
    (println "(fib 20) \t Avg: "         (time-it #(fib 20)))
    (println "(sort-seq) \t Avg: "       (time-it #(sort-seq))))

(set! *main-cli-fn* -main)

Step3: build to get the main.js script.

java -cp cljs.jar:src clojure.main src/build.clj

Step4: Run node.js

node main.js

This will show the execution results.

(factorial 5000)     Avg:  0.65
(fib 20)     Avg:  0.135
(sort-seq)   Avg:  0.135

The example code is copied from http://blog.gonzih.me/blog/2013/01/23/clojurescript-on-beaglebone-simple-benchmark-with-node-dot-js/ .

Isocyanide answered 28/10, 2015 at 1:36 Comment(0)
D
1

To use light table with clojurescript, one difference from clojure project is:

If I connect some page with browser (internal or external), I need to run:
lein cljsbuild auto
in terminal.

otherwise, the goog related js can not be found.

You can check from here:
https://groups.google.com/forum/#!topic/light-table-discussion/fJBLMzmZSWw

Disharmony answered 20/2, 2014 at 10:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.