1

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.

3
  • 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? Commented Nov 7, 2017 at 7:31
  • Oh, you are using lumo.build.api. I think you can do the same with cljs.build.api. Commented Nov 7, 2017 at 7:32
  • 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. Commented Nov 10, 2017 at 2:38

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

From my testing this includes the google closure functions for imports. I've updated the comment with clarifications. I'd like to generate a single javascript file that contains all required libraries.
If you add :optimizations :advanced or :simple, it will bundle all deps into one file.
Thank you! IDK where I went astray but your solution worked. (require 'cljs.build.api) (cljs.build.api/build "src" {:main 'hello-world.core :output-to "main.js" :optimizations :advanced :target :nodejs})
I was focused on using lein run -m clojure.main node.clj I get Caused by: java.io.FileNotFoundException: Could not locate cljs/build/api__init.class or cljs/build/api.clj on classpath.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.