How to use use React libraries in ClojureScript - clojurescript

I'm trying to use cljsjs/vis in my re-frame / reagent / leiningen project, but getting the error when import this library into namespace.
cljsjs/vis in namespace required but not available
Have tried to use another libraries for charts/data visualizations, but still have same result. It seems something wrong with import JS libraries to clojurescript project.
leiningen project.cljs file
(defproject test1 "0.1.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.10.1"]
[org.clojure/clojurescript "1.10.764"
:exclusions [com.google.javascript/closure-compiler-unshaded
org.clojure/google-closure-library
org.clojure/google-closure-library-third-party]]
[thheller/shadow-cljs "2.9.3"]
[reagent "0.10.0"]
[re-frame "0.12.0"]
[cljs-ajax "0.7.5"]
[cljsjs/vis "4.21.0-1"]]
:plugins [[lein-shadow "0.2.0"]
[lein-shell "0.5.0"]]
:min-lein-version "2.9.0"
:source-paths ["src/clj" "src/cljs"]
:clean-targets ^{:protect false} ["resources/public/js/compiled" "target"]
:shell {:commands {"open" {:windows ["cmd" "/c" "start"]
:macosx "open"
:linux "xdg-open"}}}
:shadow-cljs {:nrepl {:port 8777}
:builds {:app {:target :browser
:output-dir "resources/public/js/compiled"
:asset-path "/js/compiled"
:modules {:app {:init-fn test1.core/init
:preloads [devtools.preload]}}
:devtools {:http-root "resources/public"
:http-port 8280
}}}}
:aliases {"dev" ["with-profile" "dev" "do"
["shadow" "watch" "app"]]
"prod" ["with-profile" "prod" "do"
["shadow" "release" "app"]]
"build-report" ["with-profile" "prod" "do"
["shadow" "run" "shadow.cljs.build-report" "app" "target/build-report.html"]
["shell" "open" "target/build-report.html"]]
"karma" ["with-profile" "prod" "do"
["shadow" "compile" "karma-test"]
["shell" "karma" "start" "--single-run" "--reporters" "junit,dots"]]}
:profiles
{:dev
{:dependencies [[binaryage/devtools "1.0.0"]]
:source-paths ["dev"]}
:prod {}
}
:prep-tasks [])
cljs file
(ns my-project
(:require [cljsjs.vis]))

shadow-cljs does not support CLJSJS packages. Instead you install the original npm packages those CLJSJS packages represent and use that directly.
So instead of
(ns my-project
(:require [cljsjs.vis]))
;; accessing vis via js/WhateverGlobalItUses
you do
(ns my-project
(:require ["vis-network" :as vis]))
;; then use "vis" directly
The npm version is split into multiple separate packages, so just include whatever you want to use.
The shadow-cljs docs explain using npm packages is much more detail. There is also an older example repo using vis-network.

Related

I use the emacs to start or connect clojurescript but show error: ClojureScript is not available

I use the emacs to start or connect clojurescript but show error:
error in process filter: ClojureScript is not available. See https://docs.cider.mx/cider/basics/clojurescript for details
In terminal I use `npx shadow-cljs watch main` to start clojurescript app. It's to started. so in emacs I use `cider-connect-cljs` connect
M-x cider-connect-cljs
Host: localhost
Port: 3477
Select ClojureScript REPL type: shandow
Select shadow-cljs build: main
[terminal starts app](https://i.stack.imgur.com/Ait43.png)
[emacs](https://i.stack.imgur.com/0WUfC.png)
On the other hand. I use `cider-jack-in-cljs` to start app in emacs alone. the error as same as up.
shadow-cljs.edn
;; shadow-cljs configuration
{:deps {:aliases [:dev]}
:http {:port 3448}
:nrepl {:port 3447 :host "0.0.0.0"}
:jvm-opts ["-Xmx700m" "-Xms100m" "-XX:+UseSerialGC" "-XX:-OmitStackTraceInFastThrow"]
:dev-http {8888 "classpath:public"}
:builds
{:main
{:target :browser
:output-dir "resources/public/js/"
:asset-path "/js"
:devtools {:browser-inject :main
:watch-dir "resources/public"}
:build-options {:manifest-name "manifest.json"}
:modules
{:main {:entries [app.main]
:init-fn app.main/init}}}
}}
deps.edn
{:paths ["src" "vendor" "resources" "test"]
:deps {org.clojure/clojure {:mvn/version "1.11.1"}
org.clojure/clojurescript {:mvn/version "1.11.60"}
lilactown/helix {:mvn/version "0.1.9"}}
:aliases {:dev
{:extra-paths ["test" "dev"]
:extra-deps
{thheller/shadow-cljs {:mvn/version "2.20.2"}
org.clojure/tools.namespace {:mvn/version "RELEASE"}
cider/cider-nrepl {:mvn/version "0.29.0"}}}}}

ClojureScript Setup from book

I am following the learning ClojurScript book from Packt, and I am a little lost on something. I have a project.clj file with the following configuration
(defproject piggieback_project "0.5.2"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.10.3"]
[org.clojure/clojurescript "1.10.879"]
[weasel "0.7.1" :exclusions
[org.clojure/clojurescript]]]
:profiles {:dev {:dependencies [[cider/piggieback "0.5.2"]
[org.clojure/tools.nrepl"0.2.10"]]
:repl-options {:nrepl-middleware [cider.piggieback/wrap-cljs-repl]}}})
I am able to run lein repl
and get a repl
I then run the following with this result
Clojure 1.10.3
OpenJDK 64-Bit Server VM 1.8.0_302-b08
Docs: (doc function-name-here)
(find-doc "part-of-name-here")
Source: (source function-name-here)
Javadoc: (javadoc java-object-or-class-here)
Exit: Control+D or (exit) or (quit)
Results: Stored in vars *1, *2, *3, an exception in *e
user=> (require 'cljs.build.api)
nil
user=> (cljs.build.api/build "src"
#_=> {:main 'piggieback-project.core
#_=> :output-to "out/main.js"
#_=> :verbose true})
Options passed to ClojureScript compiler: {:output-dir "out", :closure-warnings {:check-types :off, :check-variables :off}, :closure-defines {}, :ups-libs nil, :cache-analysis true, :closure-module-roots [], :optimizations :none, :ups-foreign-libs [], :verbose true, :aot-cache false, :preloads [process.env], :ignore-js-module-exts [".css"], :output-to "out/main.js", :preamble ["cljs/imul.js"], :ups-externs nil, :opts-cache "cljsc_opts.edn", :source-map true, :cache-analysis-format :transit, :main piggieback-project.core, :language-in :es6, :emit-constants nil}
Unexpected error compiling at (REPL:1).
Could not write JavaScript nil
user=> (require 'weasel.repl.websocket)
user=> (cider.piggieback/cljs-repl
#_=> (weasel.repl.websocket/repl-env :ip "0.0.0.0" :port
#_=> 9001))
<< started Weasel server on ws://0.0.0.0:9001 >>
<< waiting for client to connect ...
This is correct according to the book as to my result. But The next step is to open up my html file at the root which looks like this
<html>
<body>
<script type="text/javascript" src="out/main.js"></script>
</body>
</html>
and in my src directory I have a core.clj file with the following
(ns piggieback-project.core
(:require [weasel.repl :as repl]))
(when-not (repl/alive?)
(repl/connect "ws://localhost:9001"))
When I open the html file it's supposed to connect to the weasel websocket server and give me a user repl but it does not.
I am brand new to Clojurscript, So any help here at all would be great thanks
Your core.clj file should be named core.cljs! (Full path from the project root: src/piggieback_project/core.cljs.)
P.S. Remove the incomplete "out" directory before you try again, to give the compiler a fresh start.

Clojurescript Build with Modules - Constants Table Crashing

I'm building an app using:
lein cljsbuild once min
where my min config is
{:id "min"
:source-paths ["src"]
:compiler {:output-dir "resources/public/js"
:main app.core
:optimizations :advanced
:modules {:app
{:output-to "resources/public/js/app.js"
:entries #{app.core}}}
:pretty-print false}}
I end up with 3 js files: cljs_base.js, constants_table.js and app.js
When I include them in my index.html file in that order, I get a console exception:
Uncaught TypeError: cljs.core.Keyword is not a constructor
at constants_table.js:1
If I add :optimize-constants false then it builds the modules correctly.

lein-doo not running the tests, what's going on?

When I try running the tests with lein-doo I get this error:
ERROR: doo was not loaded from the compiled script.
Make sure you start your tests using doo-tests or doo-all-tests
and that you include that file in your build
My project.clj looks like this:
(defproject tech.dashman/clientcommon "0.1.0-SNAPSHOT"
:description "Dashman - Common library to all clients"
:url "https://dashman.tech"
:min-lein-version "2.3.4"
:source-paths ["src"]
:cljsbuild {:builds {:test {:source-paths ["src" "test"]
:compiler {:output-to "target/test/clientcommon.js"
:output-dir "target/test"
:target :nodejs
:main clientcommon.test-runner}}}}
:doo {:build "test"
:debug true} ; Run tests: lein doo phantom
:dependencies [[org.clojure/clojure "1.8.0" :scope "provided"]
[org.clojure/clojurescript "1.9.229" :scope "provided"]
[com.taoensso/sente "1.11.0"]
[mount "0.1.10"]
[prismatic/schema "1.1.3"]
[cljsjs/react-with-addons "15.2.1-0"]
[reagent "0.6.0" :exclusions [cljsjs/react]]
[re-frame "0.8.0"]
[tech.dashman/reagent-toolbox "0.1.0-SNAPSHOT"]
[doo "0.1.7"]]
:plugins [[s3-wagon-private "1.3.0"]
[lein-cljsbuild "1.1.4"]
[lein-doo "0.1.7"]])
and my test-runner file looks like this:
(ns clientcommon.test-runner
(:require [doo.runner :refer-macros [doo-tests doo-all-tests]]
[clientcommon.crypto-test]
[clientcommon.util-test]))
(doo-tests 'clientcommon.crypto-test
'clientcommon.util-test)
I'm doing this on Windows and I found a related bug but it claims to be fixed: https://github.com/bensu/doo/issues/60
Any ideas what's going on?

clojurescript advanced compilation - size

I am new to clojurescript.
Is it normal to have a javascript file of "77 KB" after advanced compilation?
I have one clojurescript file:
I am using leinigen: lein cljsbuild once
(ns my-staff.core)
(defn ^:export main []
(.write js/document "<p>Hello from my_staff.core.main()</p>"))
(defproject my-staff "0.1.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.4.0"]
[org.clojure/clojurescript "0.0-1586"]]
:source-paths ["src-clj"]; source path for clojure
:plugins [[lein-cljsbuild "0.3.0"]]
:cljsbuild {
:builds [{
:source-paths ["src-cljs"]
:compiler {
:output-to "public/javascripts/main.js"
:optimizations :advanced
:pretty-print false}}]})
Yes, that's normal. The Google Closure compiler has trouble optimizing some of the code that the clojurescript compiler currently emits. This is expected to improve over time. Check CLJS-257 for progress.