From d34312b4d51120014ac66183d5e56e2ddfdb495d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1aki=20Arenaza?= Date: Wed, 3 Feb 2021 12:47:19 +0100 Subject: [PATCH] Fix NullPointerException when processing externs files lein-figwheel expects that all files with .js extension inside its source directories are foreign libraries. And foreign libraries must declare a namespace. In fact, lein-figwheel assumes all .js files have such a namespace declared. And it blindly tries to use it to map the .js file back to a source .cljs file. When the namespace is not declared in the .js file, lein-figwheel bombs out with a NullPointerException when it tries to check if the the source .cljs file exists. This might happen when you put your externs file(s) inside the source directories (this is our case, e.g. https://github.com/magnetcoop/hydrogen-ce/blob/master/src/hyd/client/externs.js) lein-figwheel doesn't try to process such files by default on its own. But when using Duct server.figwheel, it tells lein-figwheel to process all files inside the configured source directories (see https://github.com/duct-framework/server.figwheel/blob/master/src/duct/server/figwheel.clj#L54-L55). Clearly lein-figwheel should be more robust and handle that situation in a more graceful way[1]. On the other hand Duct server.figwheel shouldn't be telling lein-figwheel to process absolutely all files in source directories. It doesn't make sense to process .clj files, .edn files, etc. Probably just those having .cljs/.cljc extension and those declared as foreign libraries. In issue #7 James Reeves suggested that instead of hard-coding the list of files to process by lein-figwheel, we should have configurable options with default values. And he suggested using a regular expression for matching filenames. [1] We opened a pull request in lein-figwheel regarding this behaviour, see bhauman/lein-figwheel#739. It has been fixed since then and included in figwheel-sidecar 0.5.20. But Duct server.figwheel is still using figwheel-sidecar 0.5.18 --- README.md | 27 +++++++++++++++++++++++++++ src/duct/server/figwheel.clj | 29 +++++++++++++++++++++-------- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index b7feced..eb8ccf2 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,33 @@ same options as Figwheel. :optimizations :none}}]}} ``` +By default the library only takes into account files ending in ".css" +when processing the directories specified in the `:css-dirs` key. If +you want to process other files as CSS files, you can add the +`:css-files-pattern` key and specify a string that will be used as a +regular expression that the relevant files have to match. + +Similarly only files ending in ".cljs" or "cljc" will be taken into +account when processing the directories specified in the +`:source-paths` key. Again, you can add a `:source-files-pattern` key +and specify a string that will be used as a regular expression that +the relevant files have to match. In this case, you need to specify +the key for each build you specify in the `:builds` vector. + +Here is an example with the default patterns: + +```edn +{:duct.server/figwheel + {:css-dirs ["dev/resources"] + :css-files-pattern "\\.css$" + :builds [{:id :dev + :source-paths ["src"] + :source-files-pattern "\\.clj[sc]$" + :build-options {:output-to "target/js/public/main.js" + :output-dir "target/js/public" + :optimizations :none}}]}} +``` + See the [Figwheel README][] for more information. [figwheel readme]: https://github.com/bhauman/lein-figwheel/blob/master/README.md diff --git a/src/duct/server/figwheel.clj b/src/duct/server/figwheel.clj index e4d2e0b..3730acc 100644 --- a/src/duct/server/figwheel.clj +++ b/src/duct/server/figwheel.clj @@ -17,6 +17,9 @@ [org.httpkit.server :as httpkit] [ring.middleware.cors :as cors])) +(def ^:const default-source-files-pattern "\\.clj[sc]$") +(def ^:const default-css-files-pattern "\\.css$") + (defrecord FigwheelBuild []) (defrecord FigwheelServer [] @@ -51,8 +54,11 @@ server (figwheel-server state)] (map->FigwheelServer (assoc state :http-server server)))) -(defn- find-files [paths] - (mapcat (comp file-seq io/file) paths)) +(defn- find-files [{:keys [paths files-pattern]}] + (let [files (mapcat (comp file-seq io/file) paths)] + (if files-pattern + (filter #(re-find (re-pattern files-pattern) (.getPath %)) files) + files))) (defn- watch-paths [paths] (let [time (volatile! 0)] @@ -63,11 +69,14 @@ (vreset! time now) (filter #(> (.lastModified %) then) (find-files paths))))))) -(defn- prep-build [{:keys [compiler-env source-paths] :as build}] +(defn- prep-build [{:keys [compiler-env source-paths source-files-pattern] :as build}] (-> build + (dissoc :source-files-pattern) (cond-> (not (fig-config/prepped? build)) fig-config/prep-build) (cond-> (not compiler-env) fig-build/add-compiler-env) - (assoc :watcher (watch-paths source-paths)) + (assoc :watcher (watch-paths {:paths source-paths + :files-pattern (or source-files-pattern + default-source-files-pattern)})) (map->FigwheelBuild))) (defn- clean-build [build] @@ -83,8 +92,8 @@ "Tell a Figwheel server to rebuild all ClojureScript source files, and to send the new code to the connected clients." [{:keys [server prepped]}] - (doseq [{:keys [source-paths] :as build} prepped] - (let [files (map str (find-files source-paths))] + (doseq [{:keys [source-paths source-files-pattern] :as build} prepped] + (let [files (map str (find-files {:path source-paths, :files-pattern source-files-pattern}))] (fig-util/clean-cljs-build* build) (start-build build server files)))) @@ -101,10 +110,14 @@ [{:keys [server css-watch]}] (fig-css/handle-css-notification {:figwheel-server server} (css-watch)) nil) -(defmethod ig/init-key :duct.server/figwheel [_ {:keys [builds css-dirs] :as opts}] +(defmethod ig/init-key :duct.server/figwheel [_ {:keys [builds css-dirs css-files-pattern] :as opts}] (doto {:server (start-figwheel-server opts) :prepped (mapv prep-build builds) - :css-watch (if css-dirs (watch-paths css-dirs) (fn []))} + :css-watch (if css-dirs + (watch-paths {:paths css-dirs + :files-pattern (or css-files-pattern + default-css-files-pattern)}) + (fn []))} (build-cljs) (refresh-css)))