Skip to content

Commit f77fef3

Browse files
committedNov 8, 2018
Add ClojureScript hello-world which compiles to Node.js
1 parent aee1b4c commit f77fef3

File tree

7 files changed

+91
-0
lines changed

7 files changed

+91
-0
lines changed
 

‎cljs-hello-world/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build/
2+
out/
3+
.cpcache/

‎cljs-hello-world/README.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Sample app to generate compile ClojureScript to NodeJS module
2+
3+
The output can be used in AWS Lambda, and in general as a Node.js module.
4+
5+
## Compile with Clojure
6+
7+
```bash
8+
clj -m cljs.main --target node --optimizations advanced -c hello-world.core
9+
```
10+
11+
The above generates the `out/` directory which will contain the `main.js` file.
12+
13+
### Compile with Lumo
14+
15+
An alternative way to compiling ClojureScript is using [Lumo](https://github.com/anmonteiro/lumo).
16+
17+
```
18+
# Download Lumo Docker image or install it on your system
19+
docker pull anmonteiro/lumo:latest
20+
21+
# Compile command
22+
docker run -v `pwd`:`pwd` -w `pwd` --rm -it anmonteiro/lumo --classpath src ./build.cljs
23+
24+
# The Docker user by default is root so fix the ownership
25+
sudo chown -R `id -u`:`id -u` main.js
26+
```
27+
28+
This will compile the CLJS code and generate the `out/main.js`, as defined in `build.cljs`, which is the same as the one compiled by Clojure.
29+
30+
## Create the AWS Lambda bundle
31+
32+
```
33+
./build-tools/lambda_bundle.sh
34+
```
35+
36+
Creates the `build/` directory which will contain the lambda bundle zip.

‎cljs-hello-world/build-tools/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports.handler = require('./main').hello_world.core.handler;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
3+
# Use Clojure to build the ClojureScript function
4+
clj -m cljs.main --target node --optimizations advanced -c hello-world.core
5+
# Or use Lumo which is ontop of NodeJS so no need for Java/Clojure.
6+
# Assumes `docker pull anmonteiro/lumo:latest`
7+
#docker run -v `pwd`:`pwd` -w `pwd` --rm -it anmonteiro/lumo --classpath src ./build.cljs && chown -R `id -u`:`id -u` main.js
8+
9+
rm -rf build/ && mkdir -p build/
10+
zip -j build/awslambda.zip build-tools/index.js out/main.js

‎cljs-hello-world/build.cljs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
(require '[lumo.build.api :as b])
2+
3+
(b/build "src"
4+
{:main 'hello-world.core
5+
:output-to "out/main.js"
6+
:optimizations :advanced
7+
:target :nodejs})

‎cljs-hello-world/deps.edn

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{:deps {org.clojure/clojurescript {:mvn/version "1.10.339"}}}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
(ns hello-world.core
2+
(:require [cljs.nodejs :as nodejs]
3+
[clojure.string :as str]))
4+
5+
(nodejs/enable-util-print!)
6+
(enable-console-print!)
7+
8+
(println "Hello world!")
9+
10+
(defn average [& a]
11+
(do
12+
(println (str "Calculating average for: " a))
13+
(/ (reduce + a) (count a))))
14+
15+
(defn extract-nums [event]
16+
(into
17+
[]
18+
(map
19+
#(js/parseInt %)
20+
(filter #((comp not str/blank?) %) (str/split (get (get event "queryStringParameters") "n" "") #",")))))
21+
22+
(defn ^:export handler [event context callback]
23+
(do
24+
(println event)
25+
(callback
26+
nil
27+
(clj->js {:statusCode 200
28+
:body (.stringify js/JSON (apply average (extract-nums (js->clj event))))
29+
:headers {}}))))
30+
31+
; The following only works with `simple` optimizations
32+
; (set! (.-exports js/module) #js
33+
; {:handler handler})

0 commit comments

Comments
 (0)