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

折月煮酒 提交于 2019-12-04 08:06:47

问题


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


回答1:


  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.




回答2:


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

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

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

  • https://github.com/clojure/clojurescript/releases/download/r1.7.145/cljs.jar

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




回答3:


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



来源:https://stackoverflow.com/questions/14069428/how-can-i-create-a-basic-clojurescript-hello-world-app-in-lighttable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!