Disclaimer: I am very new to clojure.
I am suddenly running into a issue where loading my clojurescript app takes over 15 seconds just to load all of the library code. A second project, set up the same way, is not having these issues.
CLJS build:
:cljsbuild {:builds [{:id "dev"
:source-paths ["src/cljs"]
:compiler {:output-to "resources/public/app/js/app.js"
:output-dir "resources/public/app/js/out"
:optimizations :none
:source-map true}}]}
handler.clj
(GET "/" [] (resource-response "index.html" {:root "public/app"}))
(route/resources "/" {:root "public/app"})
(route/not-found "Not Found"))
My first thought is, that it somehow re-compiles everything every time I access the page, but the file timestamps of unchanged libraries didn't change.
Second thought was, that it is probably because of cache killing in my browser, but even after allowing cache through the developer tools, file loading time is still snailspeed.
Third thought was the compojure version difference between the 2 projects, but even after upgrading the 2nd project to latest, or downgrading 1st project to previous version, the issue still persists.
When monitoring, I also noticed the java process to jump to 350% CPU on page access.
I tried to revert all changes I made but can't figure out where the problem is. Being very new to clojure and clojurescript, I am out of ideas. I obviously can't wait 15 seconds with every page load just to see a message in the console.
/ EDIT:
Project.clj
(defproject picky "0.1.0-SNAPSHOT"
:description "project"
:url "http://example.com/FIXME"
:source-paths ["src/clj"]
:dependencies [[org.clojure/clojure "1.6.0"]
[org.clojure/tools.reader "0.8.2"]
[org.clojure/clojurescript "0.0-2371"]
[org.clojure/core.async "0.1.346.0-17112a-alpha"]
[ring/ring-core "1.3.1"]
[ring/ring-json "0.3.1"]
[compojure "1.2.1"]
[korma "0.4.0"]
[org.postgresql/postgresql "9.2-1002-jdbc4"]
[com.cemerick/friend "0.2.1"]
[lobos "1.0.0-beta3"]
[cljs-http "0.1.20"]
[secretary "1.2.1"]
[om "0.3.6"]
[com.facebook/react "0.8.0.1"]
[hiccup "1.0.5"]]
:plugins [[lein-cljsbuild "1.0.3"]
[lein-ring "0.8.13"]
[lein-pdo "0.1.1"]]
:aliases {"up" ["pdo" "cljsbuild" "auto" "dev," "ring" "server-headless"]}
:ring {:handler myapp.handler/app}
:profiles
{:dev {:dependencies [[javax.servlet/servlet-api "2.5"]
[ring-mock "0.1.5"]]}}
:cljsbuild {:builds [{:id "dev"
:source-paths ["src/cljs"]
:compiler {:output-to "resources/public/js/app.js"
:output-dir "resources/public/js/out"
:optimizations :none
:source-map true}}]}
Middlewares are wrap-json-body and wrap-json-response, although I tried already disabling both.
I found the culprit of this huge performance loss. After a ton of debugging I found out that the only difference between project 1 and project 2 is the resources folder.
Project 1 includes a entire frontend project. A lot of files through bower, scss, compiled css, compiled cljs and so on. Everything you need for good frontend development. In total 10506 files.
Project 2 was a test project including a few HTML, javascript and compiled cljs files. 142 files in total.
For testing, I moved some files out of the resources folder and ta-daa, file load is down to a few milliseconds. Move the files back into the resources folder, performance get tanked up.
I am going to file a bug report at the compojure project. Might be something they weren't aware of yet.
Related
I have been dealing with this Clojure/Clojurescript project called clojurescript.csv.
On the project.clj file there is a special form of construction for :cljsbuild which I haven't seen yet.
:cljsbuild {:builds [{:id "whitespace"
:source-paths ["src" "test"]
:compiler {:output-to "target/js/whitespace.js"
:optimizations :whitespace
:pretty-print true}}
{:id "simple"
:source-paths ["src" "test"]
:compiler {:output-to "target/js/simple.js"
:optimizations :simple
:pretty-print true}}
{:id "advanced"
:source-paths ["src" "test"]
:compiler {:output-to "target/js/advanced.js"
:optimizations :advanced
:pretty-print false}}]
Usually, I see a brief declaration, such as:
:source-paths ["src"]
On this repository the approach is different. Although each "build path" uses the same source-path there are different ids and optimizations route.
1 - What is the point on having these different ids for builds? How can this be useful?
I do not see it.
2 - Also, I would like to extend this file for Continuous Deployment (publishing a Maven package on GitHub registry). Usually, below the source-paths, I add the following:
source-paths ["src"]
;; Change your environment variables (maybe editing .zshrc or .bashrc) to have:
;; export LEIN_USERNAME="pdelfino"
;; export LEIN_PASSWORD="your-personal-access-token-the-same-used-on-.npmrc"
;; LEIN_PASSWORD should use the same Token used by .npmrc
;; Also, do "LEIN_SNAPSHOTS_IN_RELEASE=true lein install" or edit your .zshrc:
;; export LEIN_SNAPSHOTS_IN_RELEASE=true
:repositories {"releases" {:url "https://maven.pkg.github.com/tallyfor/*"
:username :env/LEIN_USERNAME ;; change your env
:password :env/LEIN_PASSWORD}}
:pom-addition [:distribution-management [:repository [:id "github"]
[:name "GitHub Packages"]
[:url "https://maven.pkg.github.com/my-organization/repository-name"]]]
Should I add it 3 times? One for every id?
It feels very repetitive.
lein-cljsbuild allows you to specify multiple build configurations like this. When compiling you may provide the id of the build you want to build. So, instead of just lein cljsbuild once you do lein cljsbuild once advanced.
This is common so you can have an unoptimized development build and a :advanced optimized release build. Usually you'd have a few more differences in build configs, eg. at least the advanced build not including "test" source path. Since this is a library project, this however is fine. The authors likely wanted to test with different optimization levels easily.
:repositories and :pom-addition are top level or alias/profile definitions in project.clj. They do not go into the :cljsbuild config map.
I'm working on two related web applications that both depend on a third local project for the code they have in common.
How can I get figwheel to rebuild and reload the code when the checkout dependency is edited?
At the moment, Figwheel doesn't automatically detect leiningen checkouts. You need to add the source paths of your checkout sources directly to your cljsbuild :source-paths. For example, if you had something like
:cljsbuild {:builds [{:id "dev"
:source-paths ["src" "dev"]
:figwheel {:on-jsload 'my.main/mount-gui}
:compiler {:output-to ...
:output-dir ...
:main 'my.main
...
then you would need to change it to
:cljsbuild {:builds [{:id "dev"
;; Add checkouts path here
:source-paths ["src" "dev" "checkouts/my-project/src"]
:figwheel {:on-jsload 'my.main/mount-gui}
:compiler {:output-to ...
:output-dir ...
:main 'my.main
...
Once figwheel knows about your checkout project source paths, it should automatically recompile after any changes, and reload the code, as it would for code in your main project.
I'm working on a pull request to fix this issue, which should make it work automatically in the future.
I recently implemented my first ".cljc" file that is supposed to bridge between clojure and clojurescript.
All went well, also figwheel is picking up the changes and nicely refreshes the new code, however in the clojure side the file is not hot-reloaded.
I'm using the usual
[ring.middleware.reload :refer [wrap-reload]]
in my development middleware.
In my project.clj, I have:
:source-paths ["src/clj" "src/cljc"]
Any ideas?
Make sure that source paths for both .clj and .cljc files are set at the top level in project.clj for the JVM compilation:
:source-paths ["src/clj" "src/cljc"]
And for the ClojureScript side, ensure that source paths are set anywhere that you have compilation directives for Figwheel e.g.:
; this might be your from your dev profile cljs config:
:cljsbuild
{:builds
{:app
{:source-paths ["src/cljs" "src/cljc" "env/dev/cljs"]
:compiler
{:main "my-project.app"
:asset-path "/js/out"
:output-to "target/cljsbuild/public/js/app.js"
:output-dir "target/cljsbuild/public/js/out"
:source-map true
:optimizations :none
:pretty-print true}}}}
Sounds like your Figwheel config is good, though.
using lein for clojure, attempting to use the clojurescript plugin.
followed all readme.md install steps, project.clj has
:dependencies [[org.clojure/clojure "1.7.0"]
[org.clojure/clojurescript "0.0-3126"]]
;; lein-cljsbuild plugin to build a CLJS project
:plugins [[lein-cljsbuild "1.0.6"]]
:hooks [leiningen.cljsbuild]
I cannot seem to get lein to recognize the plugin and am not sure what is being the gremlin.
C:\Functional_Languages\Clojure\clojurescript_master\!work\modern-cljs>lein cljsbuild once
'cljsbuild' is not a task. See 'lein help'.
C:\Functional_Languages\Clojure\clojurescript_master\!work\modern-cljs>lein compile
C:\Functional_Languages\Clojure\clojurescript_master\!work\modern-cljs>lein cljsbuild once
'cljsbuild' is not a task. See 'lein help'.
C:\Functional_Languages\Clojure\clojurescript_master\!work\modern-cljs>lein -v
Leiningen 2.5.1 on Java 1.8.0_51 Java HotSpot(TM) 64-Bit Server VM
C:\Functional_Languages\Clojure\clojurescript_master\!work\modern-cljs>
If you use lein new mies ... for getting the project file, and execute the command, the automatically generated project.clj file should be modified.
This is an example that shows the change:
Before:
(defproject simple "0.1.0-SNAPSHOT"
:description "FIXME: write this!"
:url "http://example.com/FIXME"
:dependencies [[org.clojure/clojure "1.7.0"]
[org.clojure/clojurescript "1.7.122" :classifier "aot"
:exclusion [org.clojure/data.json]]
[org.clojure/data.json "0.2.6" :classifier "aot"]]
:jvm-opts ^:replace ["-Xmx1g" "-server"]
:plugins [[lein-npm "0.6.1"]]
:npm {:dependencies [[source-map-support "0.3.2"]]}
:source-paths ["src" "target/classes"]
:clean-targets ["out" "release"]
:target-path "target")
After
(defproject simple "0.1.0-SNAPSHOT"
:description "FIXME: write this!"
:url "http://example.com/FIXME"
:dependencies [[org.clojure/clojure "1.7.0"]
[org.clojure/clojurescript "1.7.122" :classifier "aot"
:exclusion [org.clojure/data.json]]
[org.clojure/data.json "0.2.6" :classifier "aot"]]
:node-dependencies [[source-map-support "0.2.8"]]
:jvm-opts ^:replace ["-Xmx1g" "-server"]
:plugins [[lein-npm "0.6.1"]
[lein-cljsbuild "1.0.4"]]
:npm {:dependencies [[source-map-support "0.3.2"]]}
:source-paths ["src" "target/classes"]
:clean-targets ["out" "release"]
:target-path "target"
:cljsbuild {
:builds [{:id "simple"
:source-paths ["src"]
:compiler {
:main simple.core
:output-to "out/simple.js"
:output-dir "out"
:optimizations :none
:target :nodejs
:cache-analysis true
:source-map true}}]})
As you see, you need to add lein-cljsbuild plugins with build information. For further explanation, refer to http://www.mase.io/code/clojure/node/2015/01/24/getting-started-with-clojurecript-and-node/
If you don't want the change, just run ./scripts/build.
I think the problem is that your project.clj file is lacking a :cljsbuild stanza/key which defines the various parameters required to compile the clojurescript source files.
Have a look at Modern Clojurescript Tutorial for more details or you can check out my clojurescript file upload example to get an idea of how you can define :cljsbuild targets.
Okay. I'm trying to muck about with twitter4j inside a Clojure REPL, provided by Leiningen. I've specified twitter4j as a build dependency:
(defproject testproject "0.1.0-SNAPSHOT"
:description "Tryin stuff"
:repositories {
"twitter4j" "http://twitter4j.org/maven2"
}
:dependencies [[org.clojure/clojure "1.5.1"]
[compojure "1.1.6"]
[org.twitter4j/twitter4j-core "3.0.5"]
[org.twitter4j/twitter4j-stream "3.0.5"]]
:plugins [[lein-ring "0.8.8"]]
:ring {:handler testproject.core/app}
:profiles {:dev
{:dependencies [[javax.servlet/servlet-api "2.5"]
[ring-mock "0.1.5"]]}})
So far, so good. lein deps downloads everything without complaint into the default repo in ~/.m2. Awesome. I fire up the REPL, and I get this and only this:
user=> (import '(org.twitter4j.conf ConfigurationBuilder))
ClassNotFoundException org.twitter4j.conf.ConfigurationBuilder java.net.URLClassLoader$1.run (URLClassLoader.java:202)
The twitter4j jars are all present and accounted for, in ~/.m2/org/twitter4j/twitter4j-core/3.0.5/. Is there... something I don't get about importing Java classes? Some extra config I need to provide?
Try this (the proper package name):
user=> (import '(twitter4j.conf ConfigurationBuilder))