Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Sep 16, 2024
1 parent e158261 commit c2ec1b2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
12 changes: 7 additions & 5 deletions src/nextjournal/clerk/builder.clj
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@
:building (str "🔨 Building \"" (:file doc) "\"")
:compiling-css "🎨 Compiling CSS… "
:ssr "🧱 Server Side Rendering… "
:downloading-cache (str "⏬ Downloading distributed cache… ")
:uploading-cache (str "⏫ Uploading distributed cache… ")
:downloading-cache "⏬ Downloading distributed cache… "
:uploading-cache "⏫ Uploading distributed cache… "
:finished (str "📦 Static app bundle created in " duration ". Total build time was " (-> event :total-duration format-duration) ".\n"))))

(defn ^:private print+flush [x]
Expand Down Expand Up @@ -188,8 +188,6 @@

(defn write-static-app!
[opts docs]
;; TODO: inspect docs keys
(prn :keys (map keys docs))
(let [{:keys [package out-path browse? ssr?]} opts
index-html (str out-path fs/file-separator "index.html")
{:as static-app-opts :keys [path->doc]} (build-static-app-opts opts docs)]
Expand Down Expand Up @@ -300,6 +298,8 @@
(report-fn {:stage :downloading-cache})
(let [{duration :time-ms} (eval/time-ms (download-cache-fn state))]
(report-fn {:stage :done :duration duration})))
single-file? (= :single-file (:package opts))
dedupe-cljs (when single-file? (atom #{}))
state (mapv (fn [{:as doc :keys [file]} idx]
(report-fn {:stage :building :doc doc :idx idx})
(let [{result :result duration :time-ms} (eval/time-ms
Expand All @@ -308,7 +308,9 @@
paths/*build-opts* opts
viewer/doc-url (partial doc-url opts file)]
(let [doc (eval/eval-analyzed-doc doc)]
(assoc doc :viewer (view/doc->viewer (assoc opts :file-path (str file)) doc))))
(assoc doc :viewer (view/doc->viewer (assoc opts
:file-path (str file)
:dedupe-cljs dedupe-cljs) doc))))
(catch Exception e
{:error e})))]
(report-fn (merge {:stage :built :duration duration :idx idx}
Expand Down
9 changes: 7 additions & 2 deletions src/nextjournal/clerk/cljs_libs.clj
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
str)
(slurp resource)))

(defn prepend-required-cljs [doc]
(defn prepend-required-cljs [doc {:keys [dedupe-cljs]}]
(let [state (new-cljs-state)]
(w/postwalk (fn [v]
(if-let [viewer (v/get-safe v :nextjournal/viewer)]
Expand All @@ -181,7 +181,12 @@
v)
v))
doc)
(if-let [cljs-sources (not-empty (mapv slurp-resource (keep ns->resource (all-ns state))))]
(if-let [cljs-sources (not-empty
(filter #(or (not dedupe-cljs)
(when-not (contains? @dedupe-cljs %)
(swap! dedupe-cljs conj %)
true))
(mapv slurp-resource (keep ns->resource (all-ns state)))))]
(-> doc
;; make sure :cljs-libs is the first key, so these are read + evaluated first
(aam/assoc-before :cljs-libs (mapv (fn [code-str] (v/->ViewerEval `(load-string ~code-str))) cljs-sources))
Expand Down
2 changes: 1 addition & 1 deletion src/nextjournal/clerk/view.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
([doc] (doc->viewer {} doc))
([opts {:as doc :keys [ns file]}]
(binding [*ns* ns]
(-> (merge doc opts) v/notebook v/present cljs-libs/prepend-required-cljs))))
(-> (merge doc opts) v/notebook v/present (cljs-libs/prepend-required-cljs opts)))))

#_(doc->viewer (nextjournal.clerk/eval-file "notebooks/hello.clj"))
#_(nextjournal.clerk/show! "notebooks/test.clj")
Expand Down
15 changes: 8 additions & 7 deletions test/nextjournal/clerk/builder_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@
(first (map fs/file-name (fs/list-dir (fs/file temp-dir "_data") "**.png"))))))
(testing "SCI .cljs sources are deduplicated"
(fs/with-temp-dir [temp-dir {}]
(builder/build-static-app! {:paths ["test/nextjournal/clerk/fixtures/require_cljs_bundle_dedupe_1.clj"
"test/nextjournal/clerk/fixtures/require_cljs_bundle_dedupe_2.clj"]
:package :single-file
:out-path temp-dir
:report-fn identity})
(let [build (slurp (fs/file temp-dir "index.html"))]
(is (= 1 (count (re-find #"(prn ::identity)" build))))))))
(let [temp-dir "/tmp"]
(builder/build-static-app! {:paths ["test/nextjournal/clerk/fixtures/require_cljs_bundle_dedupe_1.clj"
"test/nextjournal/clerk/fixtures/require_cljs_bundle_dedupe_2.clj"]
:package :single-file
:out-path temp-dir
:report-fn identity})
(let [build (slurp (fs/file temp-dir "index.html"))]
(is (= 1 (count (re-find #"(prn ::identity)" build)))))))))

(deftest process-build-opts
(testing "assigns index when only one path is given"
Expand Down

0 comments on commit c2ec1b2

Please sign in to comment.