Skip to content

Commit

Permalink
umd
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Oct 4, 2023
1 parent 0baf274 commit 9ae9743
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,50 @@ The following searches for a `foo/macros.cljc` file in the `:paths` described in

A svelte pre-processor for squint can be found [here](https://github.com/jruz/svelte-preprocess-cljs).

## Compile on a server, use in a browser

This is a small demo of how to leverage squint from a JVM to compile snippets of
JavaScript that you can use in the browser.

``` clojure
(require '[squint.compiler])
(-> (squint.compiler/compile-string* "(prn (map inc [1 2 3]))" {:core-alias "_sc"}) :body)
;;=> "_sc.prn(_sc.map(_sc.inc, [1, 2, 3]));\n"
```

The `:core-alias` option takes care of prefixing any `squint.core` function with an alias, in the example `_sc`.

In HTML, to avoid any async ES6, there is also a UMD build of `squint.core`
available. See the below HTML how it is used. We alias the core library to our
shorter `_sc` alias ourselves using

``` html
<script>globalThis._sc = squint.core;</script>
```

to make it all work.

``` html
<!DOCTYPE html>
<html>
<head>
<title>Squint</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/squint.core.umd.js"></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>
```

License
=======

Expand Down

0 comments on commit 9ae9743

Please sign in to comment.