figwheel-main

figwheel-main http://figwheel.org
badchicken 2021-04-28T01:31:46.075700Z

Hi, so I'm new to clojurescript, and I like the leiningen figwheel-main template, but I'd like to deploy to Heroku. I think Heroku sets the port via a PORT environment variable. Is there a way to make an application created with the figwheel-main template use the PORT environment variable instead of the :port in :ring-server-options . The documentation mentions that if you find yourself using the :ring-server-options "you probably need to run your own server outside of figwheel.main". Maybe that's what I need to do. Does anyone have any good examples of how to do that? Thanks!

seancorfield 2021-04-28T02:05:11.076300Z

@badchicken2 Do you actually have a server-side piece to your app or is it pure cljs?

seancorfield 2021-04-28T02:05:58.077Z

(I haven’t used lein for years so I can’t help you with that — I’ve only used figwheel-main via the Clojure CLI/`deps.edn`)

badchicken 2021-04-28T04:02:42.080700Z

I'm going to have a server-side piece. I'm open to using something besides lein, but basically I want clojurescript/reagent in the UI with clojure server-side, plus figwheel watching/reloading changes during development... and deployable to Heroku. I've spent a lot of time looking at different options, but nothing seems to be quite what I'm looking for. It's pretty confusing as a beginner.

2021-04-28T07:32:08.084200Z

@badchicken2 You can check here https://github.com/prestancedesign/todo-backend-clojure-reitit/blob/master/src/todo_backend/core.clj#L72. This is how you can do. FYI, this todo app example run on Heroku. Check also the Procfile used by Heroku to run the JAR version of your app. https://github.com/prestancedesign/todo-backend-clojure-reitit/blob/master/Procfile Hope this can help.

2021-04-28T09:04:22.085Z

> The documentation mentions that if you find yourself using the :ring-server-options "you > probably need to run your own server outside of figwheel.main". Maybe > that's what I need to do. Does anyone have any good examples of how to > do that? This is what your need https://github.com/bhauman/figwheel-main/blob/ead06b94b1b2747ed37e5d8c37118d6e7ae77193/docs/docs/your_own_server.md

1👍
badchicken 2021-04-28T13:15:47.090200Z

Thanks for the link. On the page it says "If you don't already have a host page for your application and you are not sure how to create one, please see the documentation on this subject", but the links are broken so I'm not sure what I need to do with that. What is a host page?

2021-04-28T13:28:38.091900Z

Here is the working link: https://github.com/bhauman/figwheel-main/blob/master/docs/docs/your_own_page.md I'll do a PR to fix the document, the *.md extension is missing.

badchicken 2021-04-28T14:10:23.093600Z

Following the instructions from your_own_server.md results in an error when I try to run it: "java.io.FileNotFoundException: Could not locate ring/adapter/jetty__init.class, ring/adapter/jetty.clj or ring/adapter/jetty.cljc on classpath".

2021-04-28T14:23:04.094500Z

You should put Ring in your dependencies https://github.com/ring-clojure/ring

[ring/ring "1.9.3"]

2021-04-28T14:26:00.095300Z

Take a look at this template, it should help you: https://github.com/kliph/figwheel-heroku

emccue 2021-04-28T14:27:49.095800Z

@badchicken2 What you want is roughly a scheme like this

emccue 2021-04-28T14:28:35.096600Z

Figwheel runs as a server on port X. It watches your source directory and moves compiled artifacts to some directory /js/path

emccue 2021-04-28T14:28:54.097100Z

You have a separate server you have written running on port Y

emccue 2021-04-28T14:29:24.097800Z

this server includes all your api endpoints, features, and serves the javascript stored in /js/path when requested

emccue 2021-04-28T14:30:12.098700Z

you open your web browser to the server running on port Y, which serves some html that includes the "root" js stored at /js/path

emccue 2021-04-28T14:30:40.099300Z

when the code loads, it will look for port X and connect to the figwheel server running there

emccue 2021-04-28T14:31:04.099900Z

the code for doing that is injected by figwheel as part of compilation in dev mode

emccue 2021-04-28T14:32:03.100900Z

when you finally deploy your app, you deploy the separate server to heroku

emccue 2021-04-28T14:32:30.101600Z

and in the simplest configuration, include a production bundle of the code in /js/path within the jar's resources and serve appropriately

emccue 2021-04-28T14:33:37.102400Z

so the builtin server with figwheel that would serve html tends to have pretty limited uses if you are building the frontend in conjunction with a backend

1👍
badchicken 2021-04-28T14:35:46.102800Z

I'll take a look at figwheel-heroku. Adding ring to the dependencies didn't help.

badchicken 2021-04-28T14:47:49.103Z

Also, this problem happens whether I created the project with lein or with clj (using the examples on https://github.com/bhauman/figwheel-main-template)

2021-04-28T14:48:03.103300Z

Are your restart the REPL after the dependencie update?

badchicken 2021-04-28T14:48:28.103500Z

Yes

2021-04-28T14:53:25.103700Z

Did you had ring/ring not only ring/ring-core? Ring-core doesn't contains ring.adapter.jetty server

badchicken 2021-04-28T14:57:32.103900Z

I did. Here's an example of what I'm doing.

2021-04-28T15:23:24.104300Z

OK I see, the Figwheel doc is incomplete. With the scripts/server.cljs example, you need more dependencies:

{:deps {org.clojure/clojure {:mvn/version "1.10.0"}
        org.clojure/clojurescript {:mvn/version "1.10.773"}
        ring/ring {:mvn/version "1.9.3"}
        ring/ring-defaults {:mvn/version "0.3.2"}
        com.bhauman/figwheel-main {:mvn/version "0.2.13"}
        com.bhauman/rebel-readline-cljs {:mvn/version "0.1.4"}
        reagent {:mvn/version "0.10.0" }}
 :paths ["src" "resources" "target"]
 :aliases {:fig {:extra-deps
                  {com.bhauman/rebel-readline-cljs {:mvn/version "0.1.4"}
                   com.bhauman/figwheel-main {:mvn/version "0.2.11"}}
                 :extra-paths ["target" "test"]}
           :build {:main-opts ["-m" "figwheel.main" "-b" "dev" "-r"]}
           :min   {:main-opts ["-m" "figwheel.main" "-O" "advanced" "-bo" "dev"]}
           :test  {:main-opts ["-m" "figwheel.main" "-co" "test.cljs.edn" "-m" "yourname.test-runner"]}}}

badchicken 2021-04-28T16:16:37.104700Z

Hey, that worked! I also added "test" to the :paths to quell a warning. Thank you so much for all your help! This is one area that I think the Clojure ecosystem could use some improvement. There are so many different examples of how to cobble together an app and development environment, some of which don't quite work as described. It's challenging if you're new to Clojure.

2021-04-28T16:32:57.104900Z

You're welcome! Glad to know that works.

2021-04-28T16:35:50.105100Z

Yes I agree, there are room to some improvements here. The learning curve is steep at the beginning but once that is over it is much smoother. I think that at the moment there is a good progression on the resources (tutorials, articles, etc).