简体   繁体   English

如何设置figwheel-main重新加载clojure后端提供的代码

[英]How to setup figwheel-main to reload code served by clojure backend

I have looked for an answer to this online, google etc..but I can't find anything to help me do what I'm trying : 我已经在网上,谷歌等网站上找到了答案,但是找不到任何可以帮助我完成尝试的事情:

Here's the scenario - I've clojure code running a jetty server instance and serving content including html generated by hiccup. 这是场景-我已经运行了一个码头服务器实例并提供了打ic生成的html内容的clojure代码。

I've added clojurescript into the mix to serve frontend using reagent. 我已将clojurescript添加到混合物中以使用试剂服务于前端。 Eg the page served by jetty has and div id="app" and the reagent compiled app.js rewrites the dom for this. 例如,jetty服务的页面具有div id =“ app”,试剂编译的app.js为此重写了dom。

So far everything works ..I've configured lein to run the clojure code and build clojurescript code into app.js and put it in asset path in my lein config. 到目前为止,一切正常。.我已配置lein运行clojure代码,并将clojurescript代码构建到app.js中,并将其放在lein配置中的资产路径中。

Now I'm trying hot reloading of this app.js code by using figwheel-main..but I'm not getting anywhere. 现在,我正在尝试使用figwheel-main ..来热重载此app.js代码。 Using figwheel-main, it opens a separate browser url which can hot reload stuff..but this is not connected to the clojure backend which is serving the content. 使用figwheel-main,它会打开一个单独的浏览器URL,该URL可以热加载内容。.但是它未连接到提供内容的clojure后端。 ie figwheel-main showing the clojurescript code on localhost:9500 whereas jetty is running on port 3000. 即figwheel-main在localhost:9500上显示clojurescript代码,而jetty在端口3000上运行。

I want to be able to see hot reloading of app.js served by the jetty instance when i modify clojurescript code. 我希望能够在修改clojurescript代码时看到码头实例提供的app.js的热重载。

I can't figure out how to get this done. 我不知道如何完成这项工作。

I've tried the configs from figwheel-main tutorial -ie deps.edn etc. but can't figure out how to intergrate it with the existing clojure code. 我已经尝试过figwheel-main教程中的配置-ie deps.edn等,但无法弄清楚如何将其与现有clojure代码集成在一起。

(defproject myproject "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
            :url "https://www.eclipse.org/legal/epl-2.0/"}
  :dependencies [[org.clojure/clojure "1.10.0"]
                 [metosin/reitit "0.3.7"]
                 [ring "1.7.1"]
                 [migratus "1.2.3"]
                 [com.h2database/h2 "1.4.199"]
                 [org.postgresql/postgresql "42.2.5"]
                 [org.clojure/java.jdbc "0.7.9"]
                 [cumulus "0.1.2"]
                 [com.layerware/hugsql "0.4.9"]
                 [selmer "1.12.12"]
                 [ring-webjars "0.2.0"]
                 [ring/ring-anti-forgery "1.3.0"]
                 [hiccup "1.0.5"]
                 [org.clojure/clojurescript "1.10.520" :scope "provided"]
                 [reagent "0.8.1"]
                 [cljs-ajax "0.8.0"]
                 [org.webjars.npm/bulma "0.7.4"]
                 [org.webjars/font-awesome "5.3.1"]]
  :main ^:skip-aot myproject.core
  :source-paths ["src/clj"]
  :resource-paths ["resources" "target/public"]
  :target-path "target/%s"
  :clean-targets ^{:protect false}
  [:target-path
   [:cljsbuild :builds :app :compiler :output-dir]
   [:cljsbuild :builds :app :compiler :output-to]]
  :cljsbuild {:builds
              [{:source-paths ["src/cljs"]
                :compiler {:main "myproject.core"
                           :asset-path "js/out"
                           :optimizations :none
                           :source-map true
                           :pretty-print true
                           :output-to "target/cljsbuild/public/js/app.js"
                           :output-dir "target/cljsbuild/public/js/out"}}]}
  :plugins [[lein-cljsbuild "1.1.7"]]
  :profiles {:uberjar {:aot :all}
             :dev {:repl-options
                   {:init-ns myproject.core}}

I expect figwheel-main to work with the content already being served by jetty 我希望figwheel-main可以处理码头已经提供的内容

Please keep in mind that you have two programs running now, one in CLJS in the browser and another one in CLJ on the backend. 请记住,您现在正在运行两个程序,一个在浏览器中的CLJS中运行,另一个在后端的CLJ中运行。 They communicate over the network which adds in more complexity. 它们通过网络进行通信,这增加了更多的复杂性。

If you are already using Reagent in your CLJS code, you should add the hiccup there (as a Reagent component) instead of on the backed. 如果您已经在CLJS代码中使用了Reagent,则应在此处添加打ic(作为Reagent组件),而不要在背面添加打h。

If you have data on the backend, then grab it from the backend CLJ code via an AJAX call, then render it on the frontend via hiccup, SVG, etc. We do this with a large CLJ + CLJS app at work, where the backend code is only accessed via AJAX calls (basically an asynchronous subroutine call). 如果您在后端有数据,请通过AJAX调用从后端CLJ代码中获取数据,然后通过打ic,SVG等在前端将其呈现。我们使用大型的CLJ + CLJS应用程序来完成此任务,在后端仅通过AJAX调用(基本上是异步子例程调用)访问代码。

One option could be to have a web server (say, Nginx listening on port 9000) running during development that would mask the different ports where Figwheel and Jetty are running. 一种选择是在开发过程中运行一个Web服务器(例如Nginx在端口9000上监听),该服务器将掩盖Figwheel和Jetty运行的不同端口。

You could configure Nginx serve the location / for static files by forwarding the request to Figwheel on port 3449 and the location /api to Jetty on port 3000 (where your backend is served by Jetty). 您可以通过将请求转发到端口3449上的Figwheel以及将位置/api转发到端口3000上的Jetty(将后端由Jetty服务)来配置Nginx为静态文件提供位置/的静态文件。

During development, you'd connect to port 9000 and it would look like everything is running on that port. 在开发过程中,您将连接到端口9000,看起来一切都在该端口上运行。 One thing that I'm not sure is the websocket that Figwheel uses, but it's worth trying. 我不确定的一件事是Figwheel使用的websocket,但是值得尝试。

You can look for an example in the Nginx documentation: https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/ but an example configuration would look like this: 您可以在Nginx文档中查找示例: https : //docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/,但是示例配置如下所示:

server {
  listen 9000;
  listen [::]:9000;

  location / {
      proxy_pass http://localhost:3449/;
  }

  location /api {
      proxy_pass http://localhost:3000/;
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM