1
votes

Using Lumo and the files mentioned below I am able to run $ node main.js and presto! "Hello world!". This is great but there are drawbacks to using Lumo, and I would like to know if this is possible with the cljs.jar.

How can I create a single javascript file from clojurescript using the ClojureScript Compiler (cljs.jar) that can be ran by NodeJs.

core.cljs

(ns hello-world.core
  (:require [cljs.nodejs :as nodejs]))
(nodejs/enable-util-print!)
(defn -main [& args]
  (println "Hello world!"))
(set! *main-cli-fn* -main)

node.cljs

(require '[lumo.build.api :as b])
(b/build "src"
  {:main 'hello-world.core
   :output-to "main.js"
   :optimizations :advanced
   :target :nodejs})

The goal is to have a single file that is all inclusive with no import/require of external files.

Why do I want this? There are many websites that have developer challenges for creating and/or enhancing algorithms. The problem is that most of these sites do not all you to use clojure, nor clojurescript. But they do let you use javascript.

These sites usually allow you to use Java as well...so if there is a another way perhaps get an export of a "java" file, not a Class file, that would also work. I doubt thats possible but figured I'd ask.

1
What is not working in your current setup? Doesn't build with your node.cljs generate a single javascript file that can be run with node? - Piotrek Bzdyl
Oh, you are using lumo.build.api. I think you can do the same with cljs.build.api. - Piotrek Bzdyl
All my attempts to generate a single file that can be executed by NodeJs failed. They included import statements. The key is not to import anything, I would like to have an all inclusive file. I updated the post to clarify. - tkolleh

1 Answers

1
votes

build.clj

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

build command

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