Skip to content

Commit

Permalink
jvm ssr v1
Browse files Browse the repository at this point in the history
  • Loading branch information
roman01la committed Jun 24, 2022
1 parent 3d24a1a commit 4a10c9b
Show file tree
Hide file tree
Showing 12 changed files with 649 additions and 42 deletions.
22 changes: 12 additions & 10 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
version: 2.1

orbs:
orbs:
browser-tools: circleci/[email protected]
github-cli: circleci/[email protected]

jobs:
create-release:
docker:
- image: 'cimg/base:stable'
- image: "cimg/base:stable"

working_directory: ~/repo

steps:
- checkout
- attach_workspace:
Expand All @@ -20,7 +20,7 @@ jobs:
name: Create Release
command: |
gh release create $(git rev-parse --short HEAD) build_info
build:
docker:
- image: cimg/clojure:1.11-browsers
Expand All @@ -36,12 +36,12 @@ jobs:

- restore_cache:
keys:
- v1-dependencies-{{ checksum "core/deps.edn" }}
- v1-dependencies-{{ checksum "core/deps.edn" }}-{{ checksum "dom/deps.edn" }}
- v1-dependencies-

- restore_cache:
keys:
- v1-npm-deps-{{ checksum "core/yarn.lock" }}
- v1-npm-deps-{{ checksum "core/yarn.lock" }}-{{ checksum "dom/yarn.lock" }}
- v1-npm-deps-

- run: cd core && yarn install --frozen-lockfile
Expand All @@ -52,12 +52,14 @@ jobs:

- run: cd core && scripts/test | tee ../build_info && curl -sSLo ../build_info_master https://github.com/pitch-io/uix/releases/latest/download/build_info

- run: cd dom && scripts/test

- when:
condition:
not:
equal: [ master, << pipeline.git.branch >> ]
equal: [master, << pipeline.git.branch >>]
steps:
- run:
- run:
name: Comment on PR
command: |
# Extract the PR number from the URL. https://support.circleci.com/hc/en-us/articles/360047521451-Why-is-CIRCLE-PR-NUMBER-empty-
Expand All @@ -72,12 +74,12 @@ jobs:
- save_cache:
paths:
- ~/.m2
key: v1-dependencies-{{ checksum "core/deps.edn" }}
key: v1-dependencies-{{ checksum "core/deps.edn" }}-{{ checksum "dom/deps.edn" }}

- save_cache:
paths:
- ~/.cache/yarn
key: v1-npm-deps-{{ checksum "core/yarn.lock" }}
key: v1-npm-deps-{{ checksum "core/yarn.lock" }}-{{ checksum "dom/yarn.lock" }}

workflows:
version: 2
Expand Down
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@
.nrepl-port
.clj-kondo
.lsp
node_modules
.cpcache
.shadow-cljs
out
server_render_test
2 changes: 1 addition & 1 deletion core/scripts/fmt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env bash

clojure -A:dev -m cljfmt.main fix --indents indentation.clj
clojure -A:dev -m cljfmt.main fix --indents indentation.clj ./src ./test ../dom/src ../dom/test
16 changes: 11 additions & 5 deletions core/src/uix/compiler/aot.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"Compiler code that translates HyperScript into React calls at compile-time."
(:require [uix.compiler.js :as js]
[uix.compiler.attributes :as attrs]
[cljs.analyzer :as ana]))
[cljs.analyzer :as ana]
[uix.lib]))

(defmulti compile-attrs
"Compiles a map of attributes into JS object,
Expand Down Expand Up @@ -68,15 +69,15 @@
(into [(first v) {}] (rest v))
v))

(defmulti compile-element
(defmulti compile-element*
"Compiles UIx elements into React.createElement"
(fn [[tag] _]
(cond
(= :<> tag) :fragment
(keyword? tag) :element
:else :component)))

(defmethod compile-element :element [v {:keys [env]}]
(defmethod compile-element* :element [v {:keys [env]}]
(let [[tag attrs & children] (normalize-element env v)
tag-id-class (attrs/parse-tag tag)
attrs-children (compile-attrs :element attrs {:tag-id-class tag-id-class})
Expand All @@ -86,14 +87,19 @@
`(>el ~tag-str ~attrs-children (cljs.core/array ~@children)))]
ret))

(defmethod compile-element :component [v {:keys [env]}]
(defmethod compile-element* :component [v {:keys [env]}]
(let [[tag props & children] (normalize-element env v)
tag (vary-meta tag assoc :tag 'js)
props-children (compile-attrs :component props nil)]
`(uix.compiler.alpha/component-element ~tag ~props-children (cljs.core/array ~@children))))

(defmethod compile-element :fragment [v _]
(defmethod compile-element* :fragment [v _]
(let [[_ attrs & children] v
attrs (compile-attrs :fragment attrs nil)
ret `(>el fragment ~attrs (cljs.core/array ~@children))]
ret))

(defn compile-element [v {:keys [env] :as opts}]
(if (uix.lib/cljs-env? env)
(compile-element* v opts)
v))
3 changes: 3 additions & 0 deletions core/src/uix/compiler/attributes.clj
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@
(case %2
:class :className
:for :htmlFor
:charset :charSet
:class-id :classID
:item-id :itemID
(camel-case-dom %2)))
(compile-config-kv %2 %3))
{}
Expand Down
4 changes: 3 additions & 1 deletion core/src/uix/compiler/attributes.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
(def prop-name-cache
#js {:class "className"
:for "htmlFor"
:charset "charSet"})
:charset "charSet"
:class-id "classID"
:item-id "itemID"})

(def custom-prop-name-cache #js {})

Expand Down
23 changes: 13 additions & 10 deletions core/src/uix/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,19 @@
A component should have a single argument of props."
[sym & fdecl]
(let [[fname args fdecl] (parse-sig sym fdecl)]
(let [sym (with-meta sym {:tag 'js})
body (uix.dev/with-fast-refresh sym fdecl)]
(hooks.linter/lint! sym fdecl &env)
`(do
~(if (empty? args)
(no-args-component fname body)
(with-args-component fname args body))
(set! (.-uix-component? ~sym) true)
(set! (.-displayName ~sym) ~(str (-> &env :ns :name) "/" sym))
~(uix.dev/fast-refresh-signature sym body)))))
(hooks.linter/lint! sym fdecl &env)
(if (uix.lib/cljs-env? &env)
(let [sym (with-meta sym {:tag 'js})
body (uix.dev/with-fast-refresh sym fdecl)]
`(do
~(if (empty? args)
(no-args-component fname body)
(with-args-component fname args body))
(set! (.-uix-component? ~sym) true)
(set! (.-displayName ~sym) ~(str (-> &env :ns :name) "/" sym))
~(uix.dev/fast-refresh-signature sym body)))
`(defn ~fname ~args
~@fdecl))))

(defmacro source
"Returns source string of UIx component"
Expand Down
4 changes: 4 additions & 0 deletions core/src/uix/lib.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@
o)
#js {}
m)))

#?(:clj
(defn cljs-env? [env]
(boolean (:ns env))))
17 changes: 10 additions & 7 deletions core/test/uix/aot_test.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ns uix.aot-test
(:require [clojure.test :refer :all]
[uix.compiler.aot :as aot]
[uix.compiler.attributes :as attrs]))
[uix.compiler.attributes :as attrs]
[cljs.analyzer :as ana]))

(deftest test-parse-tag
(is (= (attrs/parse-tag (name :div))
Expand Down Expand Up @@ -35,9 +36,11 @@
(attrs/compile-attrs '{:style {:pointer-events (when x :none)}}))))

(deftest test-compile-html
(is (= (aot/compile-element [:h1] nil)
'(uix.compiler.aot/>el "h1" (cljs.core/array nil) (cljs.core/array))))
(is (= (aot/compile-element '[x {} 1 2] nil)
'(uix.compiler.alpha/component-element x (cljs.core/array {}) (cljs.core/array 1 2))))
(is (= (aot/compile-element '[x {:x 1 :ref 2} 1 2] nil)
'(uix.compiler.alpha/component-element x (cljs.core/array {:x 1 :ref 2}) (cljs.core/array 1 2)))))
(with-redefs [uix.lib/cljs-env? (fn [_] true)
ana/resolve-var (fn [_ _] nil)]
(is (= (aot/compile-element [:h1] nil)
'(uix.compiler.aot/>el "h1" (cljs.core/array nil) (cljs.core/array))))
(is (= (aot/compile-element '[x {} 1 2] nil)
'(uix.compiler.alpha/component-element x (cljs.core/array {}) (cljs.core/array 1 2))))
(is (= (aot/compile-element '[x {:x 1 :ref 2} 1 2] nil)
'(uix.compiler.alpha/component-element x (cljs.core/array {:x 1 :ref 2}) (cljs.core/array 1 2))))))
29 changes: 22 additions & 7 deletions core/test/uix/core_test.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns uix.core-test
(:require [clojure.test :refer :all]
[uix.core]))
[uix.core]
[cljs.analyzer :as ana]))

(deftest test-parse-sig
(is (thrown-with-msg? AssertionError #"uix.core\/defui doesn't support multi-arity"
Expand All @@ -14,9 +15,23 @@
(is (nil? (uix.core/vector->js-array nil))))

(deftest test-$
(is (= (macroexpand-1 '(uix.core/$ :h1))
'(uix.compiler.aot/>el "h1" (cljs.core/array nil) (cljs.core/array))))
(is (= (macroexpand-1 '(uix.core/$ identity {} 1 2))
'(uix.compiler.alpha/component-element identity (cljs.core/array {}) (cljs.core/array 1 2))))
(is (= (macroexpand-1 '(uix.core/$ identity {:x 1 :ref 2} 1 2))
'(uix.compiler.alpha/component-element identity (cljs.core/array {:x 1 :ref 2}) (cljs.core/array 1 2)))))
(testing "in cljs env"
(with-redefs [uix.lib/cljs-env? (fn [_] true)
ana/resolve-var (fn [_ _] nil)]
(is (= (macroexpand-1 '(uix.core/$ :h1))
'(uix.compiler.aot/>el "h1" (cljs.core/array nil) (cljs.core/array))))
(is (= (macroexpand-1 '(uix.core/$ identity {} 1 2))
'(uix.compiler.alpha/component-element identity (cljs.core/array {}) (cljs.core/array 1 2))))
(is (= (macroexpand-1 '(uix.core/$ identity {:x 1 :ref 2} 1 2))
'(uix.compiler.alpha/component-element identity (cljs.core/array {:x 1 :ref 2}) (cljs.core/array 1 2))))))
(testing "in clj env"
(is (= (macroexpand-1 '(uix.core/$ :h1))
[:h1]))
(is (= (macroexpand-1 '(uix.core/$ identity {} 1 2))
'[identity {} 1 2]))
(is (= (macroexpand-1 '(uix.core/$ identity {:x 1 :ref 2} 1 2))
'[identity {:x 1 :ref 2} 1 2]))))

(uix.core/defui clj-component [props] props)
(deftest test-defui
(is (= {:x 1} (clj-component {:x 1}))))
7 changes: 6 additions & 1 deletion dom/deps.edn
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{:deps {}
:paths ["src"]
:aliases {:dev {:extra-deps {uix.core/uix.core {:local/root "../core"}}}
:aliases {:dev {:extra-deps {org.clojure/clojure {:mvn/version "1.10.3"}
org.clojure/clojurescript {:mvn/version "1.10.879"}
uix.core/uix.core {:local/root "../core"}}}
:test {:extra-paths ["test"]
:extra-deps {clj-diffmatchpatch/clj-diffmatchpatch {:mvn/version "0.0.9.3"}
thheller/shadow-cljs {:mvn/version "2.15.5"}}}
:release {:extra-deps {appliedscience/deps-library {:mvn/version "0.3.4"}}
:main-opts ["-m" "deps-library.release"]}}}
Loading

0 comments on commit 4a10c9b

Please sign in to comment.