vite-plugin-hyperapp 1.0.3 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +136 -1
- package/jsx.js +3 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,3 +1,138 @@
|
|
|
1
1
|
# vite-plugin-hyperapp
|
|
2
2
|
|
|
3
|
-
Use
|
|
3
|
+
Use vite as a dev-server and bundler for your hyperapp projects. Supports:
|
|
4
|
+
|
|
5
|
+
- Views in JSX/TSX
|
|
6
|
+
- Hot module reloading (HMR)
|
|
7
|
+
- Server-side rendering (SSR)
|
|
8
|
+
|
|
9
|
+
## Quickstart
|
|
10
|
+
|
|
11
|
+
To get up and running with Hyperapp and Vite in a few seconds, use [create-vite-hyperapp](https://github.com/zaceno/create-vite-hyperapp):
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
> npm create vite-hyperapp
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
This will set you up with all the necessary configuration described below.
|
|
18
|
+
|
|
19
|
+
## Config
|
|
20
|
+
|
|
21
|
+
To manually set up your vite-project for use with Hyperapp, install this plugin:
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
> npm install vite-plugin-hyperapp
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
And add it to the plugins in your `vite.config.js`:
|
|
28
|
+
|
|
29
|
+
```
|
|
30
|
+
import { defineConfig } from "vite"
|
|
31
|
+
import hyperapp from "vite-plugin-hyperapp"
|
|
32
|
+
|
|
33
|
+
export default defineConfig({
|
|
34
|
+
plugins: [hyperapp()],
|
|
35
|
+
})
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
You can pass an options object to the plugin, e.g:
|
|
39
|
+
|
|
40
|
+
```
|
|
41
|
+
plugins: [hyperapp({
|
|
42
|
+
optionA: 'foo',
|
|
43
|
+
optionB: 'bar',
|
|
44
|
+
})]
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
...but most of the time you won't need it. See "Hot-module-reloading" below for some options that might be relevant.
|
|
48
|
+
|
|
49
|
+
## Typescript
|
|
50
|
+
|
|
51
|
+
To make sure you get proper typing support in your `.tsx` files, make sure to add these compiler options to your `tsconfig.json`:
|
|
52
|
+
|
|
53
|
+
```
|
|
54
|
+
/* Necessary for tsx to work with vite-plugin-hyperapp*/
|
|
55
|
+
"jsx": "preserve",
|
|
56
|
+
"jsxImportSource": "vite-plugin-hyperapp"
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Hot-module-reloading (HMR)
|
|
60
|
+
|
|
61
|
+
For hot-module-reloading to work, the plugin injects some code in the module where you initiate the app. It only works if the module follows these two rules:
|
|
62
|
+
|
|
63
|
+
- It must contain:
|
|
64
|
+
|
|
65
|
+
```js
|
|
66
|
+
import {..., app [as something],...} from 'hyperapp'
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
- It must _export_ a _named_ (not default) constant/variable `dispatch`, which should be the function retuned from calling `app({...})`.
|
|
70
|
+
|
|
71
|
+
If you'd rather name the `dispatch` export something else, you can tell the plugin the name you're using as a plugin option: `dispatchExport: "myPreferredName"`
|
|
72
|
+
|
|
73
|
+
And if you'd rather use Vite's HMR api directly, or forego HMR entirely, you can use the plugin option: `hmr: false`
|
|
74
|
+
|
|
75
|
+
## Server-side Rendering (SSR)
|
|
76
|
+
|
|
77
|
+
Generally, in order to set up SSR in your project you can follow Vite's instructions here: [https://vitejs.dev/guide/ssr.html#server-side-rendering](https://vitejs.dev/guide/ssr.html#server-side-rendering)
|
|
78
|
+
|
|
79
|
+
Here are some specifics you'll need to know when using hyperapp and this plugin:
|
|
80
|
+
|
|
81
|
+
### Redering your initial app to raw html
|
|
82
|
+
|
|
83
|
+
This plugin provides a function exported from `vite-plugin-hyperapp/ssr`, which can render your initial app as static html.
|
|
84
|
+
|
|
85
|
+
For example:
|
|
86
|
+
|
|
87
|
+
```js
|
|
88
|
+
import { init, view } from "./main"
|
|
89
|
+
import renderApp from "vite-plugin-hyperapp/ssr"
|
|
90
|
+
export async function render() {
|
|
91
|
+
return await renderApp({ init, view })
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
`renderApp` is an async function that takes the same options object as `hyperapp.app` - except the `node` option - and returns a promise which resolves to the html-string.
|
|
96
|
+
|
|
97
|
+
Note: it is important that the server-entry module has `import ... from 'vite-plugin-hyperapp/ssr'` somewhere in it, in order for HMR to continue working on SSR enabled apps.
|
|
98
|
+
|
|
99
|
+
### Mountpoint defined by the view
|
|
100
|
+
|
|
101
|
+
Unlike many other framework, Hyperapp _replaces_ the given mount-node with the root node rended from the view.
|
|
102
|
+
|
|
103
|
+
This means that if your non-ssr `index.html` has this:
|
|
104
|
+
|
|
105
|
+
```html
|
|
106
|
+
<p>Static html here</p>
|
|
107
|
+
<div id="app"></div>
|
|
108
|
+
<p>More static html</p>
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
and you start your app like this:
|
|
112
|
+
|
|
113
|
+
```js
|
|
114
|
+
app({
|
|
115
|
+
...,
|
|
116
|
+
node: document.querySelector('#app')
|
|
117
|
+
})
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Then you _DON'T_ add your ssr outlet like this:
|
|
121
|
+
|
|
122
|
+
```html
|
|
123
|
+
<p>Static html here</p>
|
|
124
|
+
<div id="app">
|
|
125
|
+
<!--ssr-outlet-->
|
|
126
|
+
</div>
|
|
127
|
+
<p>More static html</p>
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
...but rather like this:
|
|
131
|
+
|
|
132
|
+
```html
|
|
133
|
+
<p>Static html here</p>
|
|
134
|
+
<!--ssr-outlet-->
|
|
135
|
+
<p>More static html</p>
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
And you make sure that the root-node of your view has `id="app"`.
|
package/jsx.js
CHANGED
|
@@ -8,4 +8,6 @@ export const jsxNode = (tag, props, ...children) =>
|
|
|
8
8
|
? tag(props, children)
|
|
9
9
|
: h(tag, props || {}, children.flat().map(nodeify))
|
|
10
10
|
|
|
11
|
-
export const jsxFragment = (
|
|
11
|
+
export const jsxFragment = (_, children) => {
|
|
12
|
+
return children.flat(2).map(nodeify)
|
|
13
|
+
}
|