Skip to content

Commit

Permalink
Fix #572: prevent vite page reload (#573)
Browse files Browse the repository at this point in the history
* Fix #572: prevent vite page reload

* changelog

* fix class in REPL
  • Loading branch information
borkdude authored Nov 5, 2024
1 parent ea5bc93 commit 75bdd6a
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

[Squint](https://github.com/squint-cljs/squint): Light-weight ClojureScript dialect

## v0.8.123 (2024-11-05)

- Fix [#572](https://github.com/squint-cljs/squint/issues/572): prevent vite page reload

## v0.8.122 (2024-10-19)

- Fix watcher and compiler not overriding `squint.edn` configurations with
Expand Down
2 changes: 1 addition & 1 deletion examples/vite-react/bb.edn
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{:tasks
{dev:squint (shell "npx squint watch")
{dev:squint (shell "npx squint watch --repl true")
dev:vite (shell "npx vite --config=vite.config.js public")
-dev {:depends [dev:squint dev:vite]}
dev (run '-dev {:parallel true})
Expand Down
2 changes: 1 addition & 1 deletion examples/vite-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"dependencies": {
"react": "^18.3.1",
"react-dom": "^18.3.1",
"squint-cljs": "latest"
"squint-cljs": "../.."
}
}
8 changes: 5 additions & 3 deletions examples/vite-react/src/my_component.cljs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
(ns my-component
(:require ["react" :refer [useState]]))

(defn adder [& n]
(apply + n))
;; this needs to be private since non-component exports break vite react HMR
(defonce ^:private x 10)

(defn MyComponent []
(let [[state setState] (useState 0)]
#jsx [:div "You clicked " state " times"
[:button {:onClick #(setState (inc state))}
[:button {:onClick #(do
(set! x (inc x))
(setState (inc state)))}
"Click me"]]))
4 changes: 2 additions & 2 deletions src/squint/compiler.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -453,10 +453,10 @@
(str
(when-let [vars (disj @public-vars "default$")]
(when (seq vars)
(if cc/*repl*
(if false #_cc/*repl*
(str/join "\n"
(map (fn [var]
(str "export const " var " = " (munge cc/*cljs-ns*) "." var ";"))
(str "export " var ";"))
vars))
(format "\nexport { %s }\n"
(str/join ", " vars)))))
Expand Down
8 changes: 4 additions & 4 deletions src/squint/compiler_common.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -475,13 +475,13 @@
(let [expr (if (= 3 (count expr))
?expr ?doc)
env (no-top-level env)]
(str (if *repl*
(str (str "var " (munge name)) " = "
(emit expr (expr-env env)) ";\n"
(when *repl*
(str "globalThis."
(when *cljs-ns*
(str (munge *cljs-ns*) ".") #_"var ")
(munge name))
(str "var " (munge name))) " = "
(emit expr (expr-env env)) ";\n")))
(munge name) " = " (munge name) ";\n")))))

(defmethod emit-special 'def [_type env [_const & more :as expr]]
(let [name (first more)]
Expand Down
7 changes: 4 additions & 3 deletions src/squint/defclass.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
(let [env (assoc env* :context :statement)
{:keys [classname extends extend constructor fields protocols] :as _all} (parse-class (rest form))
[_ ctor-args & ctor-body] constructor
classname* (symbol (str classname "$"))
_ (assert (pos? (count ctor-args)) "contructor requires at least one argument name for this")

[this-sym & ctor-args] ctor-args
Expand Down Expand Up @@ -189,7 +190,7 @@
(mapcat identity)))]
(str
"class "
(munge classname)
(munge classname*)
(when extends
(str " extends "
(emit-fn extends env)))
Expand All @@ -206,8 +207,8 @@
(str (emit-fn extend-form fields-env))
(when extend
(str extend))
(when (:repl env)
(emit-fn (list 'def classname (list 'js* (munge classname))) env))
(when true #_(:repl env)
(emit-fn (list 'def classname (list 'js* (munge classname*))) env))
(when (= :return (:context env*))
(str "return " (munge classname) ";")))))

Expand Down

0 comments on commit 75bdd6a

Please sign in to comment.