This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-10-04
Channels
- # announcements (6)
- # babashka (7)
- # beginners (2)
- # biff (5)
- # calva (2)
- # cherry (17)
- # cider (3)
- # clj-kondo (8)
- # clojure (202)
- # clojure-brasil (8)
- # clojure-europe (20)
- # clojure-norway (23)
- # clojure-uk (4)
- # clojuredesign-podcast (5)
- # conjure (1)
- # cursive (9)
- # eastwood (22)
- # events (8)
- # fulcro (3)
- # hyperfiddle (22)
- # introduce-yourself (7)
- # lsp (67)
- # malli (1)
- # matrix (1)
- # meander (6)
- # off-topic (76)
- # pedestal (8)
- # polylith (17)
- # quil (12)
- # re-frame (2)
- # reagent (8)
- # releases (3)
- # shadow-cljs (67)
- # sql (93)
- # squint (39)
- # tools-deps (46)
- # vim (7)
whatever you want. note that the output is ES6. you can check the source of https://squint-cljs.github.io/squint/ how to import that in your page
oh right, that sort of import clauses wouldn’t be suitable for onclick
handlers or the like
not really no, but you can elide the import and export stuff, but if something is used from squint's standard library, your on your own
it is possible to configure the squint stdlib prefix, so if you would import it yourself and define it as a global, that could work
I came to a similar conclusion regarding the onClick handler. What you can do is have a macro (in clojure) like below and have an evalSquint function in javascript. There are probably better and more efficient ways than this idea
(defmacro js [& body]
(let [body (list (list (apply list 'fn '[_] body)))]
`(str
"evalSquint('" (pr-str (quote ~@body)) "')")))
Somewhere in the html (hiccup here):
[:script {:async true :src ""}]
[:script {:type "importmap"}
(json/generate {"imports" {"squint-cljs" "",
"squint-cljs/core.js" "",
"squint-cljs/string.js" ""}})]
[:script {:type "module"}
"import { compileString } from 'squint-cljs'
var counter = 0;
window.evalSquint = (code) => {
//console.log(\"code\", code);
counter = counter + 1;
let js = compileString(code)
// console.log(\"squint-compile\",js)
const encodedJs = encodeURIComponent(js);
const dataUri = 'data:text/javascript;charset=utf-8;eval=' + counter + ',' + encodedJs;
import(dataUri);
}"]
Yes, because I didn't find a way to deal with the modules
user=> (squint.compiler/compile-string* "(map inc [1 2 3])")
{:imports "import * as squint_core from 'squint-cljs/core.js';\n", :exports "", :body "squint_core.map(squint_core.inc, [1, 2, 3]);\n", :javascript "import * as squint_core from 'squint-cljs/core.js';\nsquint_core.map(squint_core.inc, [1, 2, 3]);\n", :jsx false, :ns user}
so if you just take :body
then you can directly use this, provided that you define squint_core
yourself somewhere
but one of the issues is that squint/core.js is also ES6, I could do an UMD build of that perhaps
This works:
<html>
<head>
<title>Squint</title>
<script type="importmap">
{
"imports": {
"squint-cljs": "",
"squint-cljs/core.js": "",
"squint-cljs/string.js": ""
}
}
</script>
<script type="module">
import * as squint_core from 'squint-cljs/core.js';
globalThis.squint_core = squint_core;
</script>
<script type="module">
squint_core.prn(squint_core.map(squint_core.inc, [1, 2, 3]));
</script>
</head>
<body>
<button onClick="squint_core.prn(squint_core.map(squint_core.inc, [1, 2, 3]));">
Click me
</button>
</body>
</html>
but note that script type="module"
is evaluated asynchronously, so any code which uses squint but must evaluated after that
I used globalThis.squint_core = squint_core;
as a hack to make squint core globally defined
Looks good to me! (I don't what I was doing things with the data uri's. Working with javascript modules is new to me)
can you customize what the “squint_core” is called? like if I want it to be _sq
to generate smaller snippets
yes:
user=> (:body (squint.compiler/compile-string* "(require '[squint.string :as str]) (prn (str/join \",\" (map inc [1 2 3])))" {:core-alias "squint.core"}))
"squint.core.prn(str.join(\",\", squint.core.map(squint.core.inc, [1, 2, 3])));\n"
https://cdn.jsdelivr.net/npm/[email protected]/lib/squint.core.umd.js
this defines a global squint.core
object
This is now the entire "demo":
<html>
<head>
<title>Squint</title>
<script src=""></script>
<!-- rename squint.core to a shorter alias at your convenience: -->
<script>globalThis._sc = squint.core;</script>
<!-- compile JS on the server using: (squint.compiler/compile-string* "(prn (map inc [1 2 3]))" {:core-alias "_sc"}) -->
<script>
_sc.prn(_sc.map(_sc.inc, [1, 2, 3]));
</script>
</head>
<body>
<button onClick="_sc.prn(_sc.map(_sc.inc, [1, 2, 3]));">
Click me
</button>
</body>
</html>
Full docs: https://github.com/squint-cljs/squint#compile-on-a-server-use-in-a-browser
I eased the way in which squint can be compiled on a JVM server and then using the output in the browser: https://github.com/squint-cljs/squint#compile-on-a-server-use-in-a-browser