figwheel /无法声明A:环手柄

发布于 2025-02-07 13:17:14 字数 4093 浏览 2 评论 0原文

我的Clojure/Reagent项目是使用静态resources/public/index.html启动的,其中src/test/client.cljs提供了渲染功能。


现在,我想将服务器端切换到动态渲染器(为了向客户端提供REST API)。
这是项目的结构:

test
 |___src
 |    |___test
 |         |___core.clj
 |         |___client.cljs
 |___resources
 |    |___public
 |         |___index.html
 |         |___style.css
 |___project.clj

和主要文件:

;; src/test/core.clj
(ns test.core
  (:use compojure.core)
  (:require
   [ring.adapter.jetty :as jetty]
   [clostache.parser :as clostache]
   [compojure.route :as route]
   [compojure.handler]
   [clojure.data.json :as json]
   ))

(def home
  "<!DOCTYPE html>
<html>
  <head>
    <meta charset=\"UTF-8\">
    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">
    <link href=\"style.css\" rel=\"stylesheet\" type=\"text/css\">
    <link rel=\"icon\" href=\"https://clojurescript.org/images/cljs-logo-icon-32.png\">
  </head>
  <body>
    <div id=\"container\"></div>
    <script src=\"/client.js\" type=\"text/javascript\"></script>
  </body>
</html>")

(defn handler [request]
  (if (and (= :get (:request-method request))
           (= "/"  (:uri request)))
    {:status 200
     :headers {"Content-Type" "text/html"}
     :body home}
    {:status 404
     :headers {"Content-Type" "text/plain"}
     :body "Not Found"}))

;; src/test/client.cljs
(ns test.client
  (:require
   [reagent.dom :as rdom]))

(defn test-container []
  [:div
   [:h1 "Test ring-handler"]
  ])

(defn ^:export run []
  (rdom/render [test-container] (js/document.getElementById "container")))

;; project.clj
(defproject test-ring-handler "0.1.2-SNAPSHOT"
  :min-lein-version "2.0.0"
  :description "test-ring-handler"
  :license {:name "GNU GPL v3+"
            :url "http://www.gnu.org/licenses/gpl-3.0.en.html"}
  :dependencies [[org.clojure/clojure "1.11.1"]
                 [org.clojure/clojurescript "1.11.54"]
                 [reagent "1.1.1"]
                 [cljsjs/react "18.0.0-rc.0-0"]
                 [cljsjs/react-dom "18.0.0-rc.0-0"]
                 [ring "1.9.5"]
                 [ring/ring-core "1.9.5"]
                 [figwheel "0.5.20"]
                 ]
  :plugins [[lein-cljsbuild "1.1.7"]
            [lein-figwheel "0.5.19"]
            ]
  
  :resource-paths ["resources" "target"]
  :clean-targets ^{:protect false} [:target-path]

  :profiles {:dev {:cljsbuild
                   {:builds {:client
                             {:figwheel {:on-jsload "test.client/run"}
                              :compiler {:main "test.client"
                                         :optimizations :none}}}}}}

  :figwheel {:repl false
;;             :http-server-root "public"
             :ring-handler test.core/handler
             }
  
  :cljsbuild {:builds {:client
                       {:source-paths ["src"]
                        :compiler {:output-dir "target/public/client"
                                   :asset-path "client"
                                   :output-to "target/public/client.js"
                                   :main "test.core"}}}}
  )

当评论:RING HANDLER选项时,所有内容已正确编译,并且client.cljs正确地呈现标题“测试环形手柄”。<<<<<<<<<<<<<<<< br> 如果:激活了Ringhandler,则汇编失败:

$ lein figwheel
Figwheel: Validating the configuration found in project.clj
Figwheel: Configuration Valid ;)
java.lang.IllegalArgumentException: unable to require the namespace of the handler test.core/handler for :ring-handler
    at figwheel_sidecar.utils$illegal_argument.invokeStatic(utils.clj:53)
    at figwheel_sidecar.utils$illegal_argument.doInvoke(utils.clj:52)
    at clojure.lang.RestFn.invoke(RestFn.java:421)
    at figwheel_sidecar.components.figwheel_server$create_initial_state$fn__19826.invoke(figwheel_server.clj:323)
...

要使用的语法是什么,以便将服务器请求重定向到test.core/handler

My clojure/reagent project was initiated using a static resources/public/index.html, where src/test/client.cljs provides the rendering function.
Now I would like to switch the server side to a dynamic renderer (in order to provide a REST API to the client).
This is the structure of the project:

test
 |___src
 |    |___test
 |         |___core.clj
 |         |___client.cljs
 |___resources
 |    |___public
 |         |___index.html
 |         |___style.css
 |___project.clj

And the main files:

;; src/test/core.clj
(ns test.core
  (:use compojure.core)
  (:require
   [ring.adapter.jetty :as jetty]
   [clostache.parser :as clostache]
   [compojure.route :as route]
   [compojure.handler]
   [clojure.data.json :as json]
   ))

(def home
  "<!DOCTYPE html>
<html>
  <head>
    <meta charset=\"UTF-8\">
    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">
    <link href=\"style.css\" rel=\"stylesheet\" type=\"text/css\">
    <link rel=\"icon\" href=\"https://clojurescript.org/images/cljs-logo-icon-32.png\">
  </head>
  <body>
    <div id=\"container\"></div>
    <script src=\"/client.js\" type=\"text/javascript\"></script>
  </body>
</html>")

(defn handler [request]
  (if (and (= :get (:request-method request))
           (= "/"  (:uri request)))
    {:status 200
     :headers {"Content-Type" "text/html"}
     :body home}
    {:status 404
     :headers {"Content-Type" "text/plain"}
     :body "Not Found"}))

;; src/test/client.cljs
(ns test.client
  (:require
   [reagent.dom :as rdom]))

(defn test-container []
  [:div
   [:h1 "Test ring-handler"]
  ])

(defn ^:export run []
  (rdom/render [test-container] (js/document.getElementById "container")))

;; project.clj
(defproject test-ring-handler "0.1.2-SNAPSHOT"
  :min-lein-version "2.0.0"
  :description "test-ring-handler"
  :license {:name "GNU GPL v3+"
            :url "http://www.gnu.org/licenses/gpl-3.0.en.html"}
  :dependencies [[org.clojure/clojure "1.11.1"]
                 [org.clojure/clojurescript "1.11.54"]
                 [reagent "1.1.1"]
                 [cljsjs/react "18.0.0-rc.0-0"]
                 [cljsjs/react-dom "18.0.0-rc.0-0"]
                 [ring "1.9.5"]
                 [ring/ring-core "1.9.5"]
                 [figwheel "0.5.20"]
                 ]
  :plugins [[lein-cljsbuild "1.1.7"]
            [lein-figwheel "0.5.19"]
            ]
  
  :resource-paths ["resources" "target"]
  :clean-targets ^{:protect false} [:target-path]

  :profiles {:dev {:cljsbuild
                   {:builds {:client
                             {:figwheel {:on-jsload "test.client/run"}
                              :compiler {:main "test.client"
                                         :optimizations :none}}}}}}

  :figwheel {:repl false
;;             :http-server-root "public"
             :ring-handler test.core/handler
             }
  
  :cljsbuild {:builds {:client
                       {:source-paths ["src"]
                        :compiler {:output-dir "target/public/client"
                                   :asset-path "client"
                                   :output-to "target/public/client.js"
                                   :main "test.core"}}}}
  )

When the :ring-handler option is commented, everything is compiled correctly and client.cljs correctly renders the title "Test ring-handler".
If :ring-handler is activated, the compilation fails:

$ lein figwheel
Figwheel: Validating the configuration found in project.clj
Figwheel: Configuration Valid ;)
java.lang.IllegalArgumentException: unable to require the namespace of the handler test.core/handler for :ring-handler
    at figwheel_sidecar.utils$illegal_argument.invokeStatic(utils.clj:53)
    at figwheel_sidecar.utils$illegal_argument.doInvoke(utils.clj:52)
    at clojure.lang.RestFn.invoke(RestFn.java:421)
    at figwheel_sidecar.components.figwheel_server$create_initial_state$fn__19826.invoke(figwheel_server.clj:323)
...

What is the syntax to use so that the server requests are redirected to test.core/handler?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文