书中的 ClojureScript 设置

ClojureScript Setup from book

我正在关注 Packt 的学习 ClojurScript 书,但我有点迷茫。我有一个 project.clj 文件,配置如下

(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]}}})

我可以运行 lein repl 并获得回复

然后我运行下面的这个结果

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 ... 

根据书本上我的结果,这是正确的。但下一步是在根目录下打开我的 html 文件,如下所示

<html>
  <body>
    <script type="text/javascript" src="out/main.js"></script>
  </body>
</html>

在我的 src 目录中,我有一个 core.clj 文件,其中包含以下内容

(ns piggieback-project.core 
  (:require [weasel.repl :as repl])) 
(when-not (repl/alive?) 
  (repl/connect "ws://localhost:9001")) 

当我打开 html 文件时,它应该连接到 weasel websocket 服务器并给我一个用户响应,但它没有。

我是 Clojurscript 的新手,所以任何帮助都将非常感谢

您的 core.clj 文件应命名为 core.cljs! (项目根目录的完整路径:src/piggieback_project/core.cljs。)

P.S。在重试之前删除不完整的“out”目录,让编译器重新开始。