From 95fc0cde2bb9409d31c3094470d4b55653b51d68 Mon Sep 17 00:00:00 2001 From: Juho Teperi Date: Thu, 5 Sep 2024 14:12:01 +0300 Subject: [PATCH 1/2] Without hooks https://shadow-cljs.github.io/docs/UsersGuide.html#clj-run --- package.json | 4 ++-- shadow-cljs.edn | 9 ++------- src/dev/build.clj | 23 ++++++++++++++++++++++- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 434579a..4110aa1 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "name": "uix-starter", "scripts": { - "dev": "shadow-cljs -A:dev watch app & yarn styles-dev", - "release": "shadow-cljs -A:dev release app && yarn styles-release", + "dev": "shadow-cljs -A:dev run dev.build/start & yarn styles-dev", + "release": "shadow-cljs -A:dev run dev.build/release && yarn styles-release", "styles-dev": "onchange -o '> public/main.css && lightningcss --bundle public/main.css -o public/main.css' -i src/**/*.css -- cat src/**/*.css", "styles-release": "cat src/**/*.css > public/main.css && lightningcss --minify --bundle public/main.css -o public/main.css", "start:libs": "mkdir -p target/gen && touch target/gen/libs.js && node watch.mjs", diff --git a/shadow-cljs.edn b/shadow-cljs.edn index 20f7f3c..65fd891 100644 --- a/shadow-cljs.edn +++ b/shadow-cljs.edn @@ -8,15 +8,10 @@ :js-options {:js-provider :external :external-index "target/gen/libs.js" :external-index-format :esm} - :release {:build-hooks [;; Esbuild must run after shadow-cljs flush so no way to optimize. - ;; You can replace yarn with npm. - (dev.build/run-cmd-flush {:cmd ["yarn" "build:libs"] - :once true})] - ;; If you want to enable module-hash-names you need to fix the references in the index.html to refer to + :release {;; If you want to enable module-hash-names you need to fix the references in the index.html to refer to ;; the new names. ;; ESBuild can also enable this. ;; Shadow-cljs would write information about created output files in public/js/manifest.edn and ESBuild in ;; public/js/manifest.json (if you enable that in build.mjs) ;; :module-hash-names true - } - :dev {:build-hooks [(dev.build/run-cmd-configure {:cmd ["yarn" "start:libs"]})]}}}} + }}}} diff --git a/src/dev/build.clj b/src/dev/build.clj index 5f9722d..9e50f88 100644 --- a/src/dev/build.clj +++ b/src/dev/build.clj @@ -1,4 +1,5 @@ -(ns dev.build) +(ns dev.build + (:require [shadow.cljs.devtools.api :as shadow])) (defn start-process [cmd] (let [;; Redirect output to stdout. @@ -11,6 +12,26 @@ (.addShutdownHook (Runtime/getRuntime) (Thread. (fn [] (.destroy process)))) process)) +;; New version - main function + +(defn start + {:shadow/requires-server true} + [& _args] + ;; TODO: Create libs.js file if doesn't exist here instead of package.json? + (start-process ["yarn" "start:libs"]) + (shadow/watch :app) + ::started) + +(defn release + [] + (shadow/release :app) + (.waitFor (start-process ["yarn" "build:libs"]))) + +;; Old version - hooks + +;; TODO: Hooks might run multiple times, for example if the shadow-cljs.edn is changed +;; -> multiple ESBuild processes +;; Consider "main ns" to run the process & Shadow-cljs, so no need for hooks? (defn run-cmd-configure {:shadow.build/stage :configure} [build-state {:keys [cmd]}] From 508b5101e1a3ec6f7a97eb62a16a1481ffe4ae55 Mon Sep 17 00:00:00 2001 From: Juho Teperi Date: Fri, 6 Sep 2024 11:05:44 +0300 Subject: [PATCH 2/2] Start esbuild after shadow watch is running --- package.json | 2 +- src/dev/build.clj | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 4110aa1..bbcfe52 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "release": "shadow-cljs -A:dev run dev.build/release && yarn styles-release", "styles-dev": "onchange -o '> public/main.css && lightningcss --bundle public/main.css -o public/main.css' -i src/**/*.css -- cat src/**/*.css", "styles-release": "cat src/**/*.css > public/main.css && lightningcss --minify --bundle public/main.css -o public/main.css", - "start:libs": "mkdir -p target/gen && touch target/gen/libs.js && node watch.mjs", + "start:libs": "node watch.mjs", "build:libs": "node build.mjs" }, "dependencies": { diff --git a/src/dev/build.clj b/src/dev/build.clj index 9e50f88..c29738f 100644 --- a/src/dev/build.clj +++ b/src/dev/build.clj @@ -17,9 +17,10 @@ (defn start {:shadow/requires-server true} [& _args] - ;; TODO: Create libs.js file if doesn't exist here instead of package.json? - (start-process ["yarn" "start:libs"]) + ;; Watch call blocks until the first build is done -> the index file + ;; will be available when ESBuild is started (shadow/watch :app) + (start-process ["yarn" "start:libs"]) ::started) (defn release