NodeJS 如何在nbb中使用ClojureScript的Hiccups库

4uqofj5v  于 2023-04-29  发布在  Node.js
关注(0)|答案(1)|浏览(98)

我使用的是nbb文档中的示例,从node调用nbb,它的工作原理和预期一样,通常使用以下(稍微修改)片段:

;; example.cljs
(ns example)

(defn greet [name] (println "Hello," name)

;; this JS object is the return value of loadFile:
#js {:greet greet}
// index.js
import { loadFile } from 'nbb'

// destructure JS object returned from .cljs file:
const { greet } = await loadFile('example.cljs')

// execute the foo function
greet("World");
$ node index.js
Hello, World

我想使用ClojureScript的hiccups库,但我似乎不太清楚如何将其连接起来。

我所尝试的

我试着把打嗝图书馆添加到nbb。edn文件(如nbb文档中其他地方所建议的),如下所示:

{:deps
 {hiccups/hiccups {:mvn/version "0.3.0"}}}

然后在示例中需要它。cljs文件如下(改编自hiccups文档)

(ns example
  (:require [hiccups :as hiccups]))

;; ... etc.

我还尝试了其他的东西,或多或少是在黑暗中刺,没有做任何有用的。

错误

但我只是得到了一些对我来说太神秘的错误:

$ node index.js 
Downloading dependencies...
Error: Could not find or load main class clojure.main
Caused by: java.lang.ClassNotFoundException: clojure.main
Exception in thread "main" java.io.FileNotFoundException: /home/sirrobert/.clojure/.cpcache/A4CEF68F951217FE6FE54759160B5276.cp (No such file or directory)
... 100 more lines ...

显然,当nbb开始解释脚本时,它试图下载deps,但有一些babashka失败。..

请求

有人能提供一个示例脚本,显示如何连接起来?最好是评论。只要一个简单的(println (html [:span "Hello, World!"]))类型的脚本就足以让我开始。
谢谢!

8yoxcaq7

8yoxcaq71#

这并不能解决问题的根本原因,但是:
nbb自带试剂内置,你也可以用来渲染打嗝。

$ npm install nbb react-dom
$ npx nbb
user=> (require '[reagent.dom.server :refer [render-to-string]])
nil
user=> (render-to-string [:div [:p "Hello"]])
"<div><p>Hello</p></div>"

要调试deps问题,可以尝试以下操作:

bb --config nbb.edn -e nil

看看能打印出什么
欢迎随时在Clojurians Slack nbb / babashka频道跟进。

相关问题