-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
37 changed files
with
736 additions
and
255 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
{:lint-as {squint.core/defclass clj-kondo.lint-as/def-catch-all}} | ||
{:lint-as {squint.core/defclass clj-kondo.lint-as/def-catch-all} | ||
:linters {:discouraged-var {clojure.core/gensym {:message "gensym makes compiler not deterministic"}}}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -214,10 +214,87 @@ REPL. REPL away! | |
|
||
Squint respect CLJS truth semantics: only `null`, `undefined` and `false` are non-truthy, `0` and `""` are truthy. | ||
|
||
## Macros | ||
|
||
To load macros, add a `squint.edn` file in the root of your project with | ||
`{:paths ["src-squint"]}` that describes where to find your macro files. Macros | ||
are written in `.cljs` or `.cljc` files and are executed using | ||
[SCI](https://github.com/babashka/sci). | ||
|
||
The following searches for a `foo/macros.cljc` file in the `:paths` described in `squint.edn`. | ||
|
||
``` clojure | ||
(ns foo (:require-macros [foo.macros :refer [my-macro]])) | ||
|
||
(my-macro 1 2 3) | ||
``` | ||
|
||
## `squint.edn` | ||
|
||
In `squint.edn` you can describe the following options: | ||
|
||
- `:paths`: the source paths to search for files. At the moment, only `.cljc` and `.cljs` are supported. | ||
- `:extension`: the preferred extension to output, which defaults to `.mjs`, but can be set to `.jsx` for React(-like) projects. | ||
|
||
See [examples/vite-react](examples/vite-react) for an example project which uses a `squint.edn`. | ||
|
||
## Watch | ||
|
||
Run `npx squint watch` to watch the source directories described in `squint.edn` and they will be (re-)compiled whenever they change. | ||
See [examples/vite-react](examples/vite-react) for an example project which uses this. | ||
|
||
## Svelte | ||
|
||
A svelte pre-processor for squint can be found [here](https://github.com/jruz/svelte-preprocess-cljs). | ||
|
||
## Vite | ||
|
||
See [examples/vite-react](examples/vite-react). | ||
|
||
## 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 | ||
======= | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Vite-react example | ||
|
||
To set up a project with vite + react, go through the following steps: | ||
|
||
- Install dependencies: | ||
|
||
``` | ||
$ npm install --save-dev vite | ||
$ npm install react-dom | ||
$ npm install squint-cljs | ||
``` | ||
|
||
- Create a `viteconfig.js` with the React plugin | ||
|
||
See [viteconfig.js](viteconfig.js) | ||
|
||
- Create a `squint.edn` to specify the source directories and to use the `.jsx` | ||
extension for outputted files | ||
|
||
See [squint.edn](squint.edn) | ||
|
||
- Run `npx squint watch` to start compiling `.cljs` -> `.jsx` | ||
|
||
E.g. see [src/MyComponent.cljs](src/MyComponent.cljs): | ||
|
||
``` clojure | ||
(ns my-component | ||
(:require ["react" :refer [useState]])) | ||
|
||
(defn MyComponent [] | ||
(let [[state setState] (useState 0)] | ||
#jsx [:div "You clicked " state "times" | ||
[:button {:onClick #(setState (inc state))} | ||
"Click me"]])) | ||
``` | ||
|
||
- Run `npx vite --config viteconfig.js public` to start a webserver and to hot-reload your React project! | ||
|
||
## Babashka tasks | ||
|
||
To run all of the above using one command, run `bb dev`. See [bb.edn](bb.edn). | ||
|
||
## Production | ||
|
||
To build your production website: | ||
|
||
``` | ||
$ npx vite --config viteconfig.js build public | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{:tasks | ||
{dev:squint (shell "npx squint watch") | ||
dev:vite (shell "npx vite --config=viteconfig.js public") | ||
-dev {:depends [dev:vite dev:squint]} | ||
dev (run '-dev {:parallel true})}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"devDependencies": { | ||
"@vitejs/plugin-react": "^4.1.0", | ||
"chokidar": "^3.5.3", | ||
"vite": "^4.4.11" | ||
}, | ||
"dependencies": { | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{:paths ["src"] | ||
:output-dir "public/js" | ||
:extension "jsx"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
(ns MyComponent | ||
(:require ["react" :refer [useState]])) | ||
|
||
(defn MyComponent [] | ||
#jsx [:div | ||
(let [[state setState] (useState 0)] | ||
#jsx [:div "You clicked " state "times" | ||
[:button {:onClick (fn [[_ _ _ ]] | ||
(setState (inc state)))} | ||
"Click me!"]])]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
(ns index | ||
(:require | ||
[MyComponent :as MyComponent] | ||
["react-dom/client" :as rdom])) | ||
|
||
(def root (rdom/createRoot (js/document.getElementById "app"))) | ||
(.render root #jsx [MyComponent/MyComponent]) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// vite.config.js | ||
import { defineConfig } from 'vite'; | ||
import react from '@vitejs/plugin-react'; | ||
|
||
export default defineConfig({ | ||
plugins: [react()] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.