vite-plugin-hyperapp 1.1.1 → 1.2.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 +74 -29
- package/index.d.ts +10 -0
- package/jsx.d.ts +59 -0
- package/jsx.js +8 -8
- package/package.json +37 -2
- package/ssr.d.ts +1 -1
- package/jsx-runtime.d.ts +0 -11
package/README.md
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
|
+
# Vite Hyperapp Plugin & Starter
|
|
2
3
|
|
|
3
4
|
Use vite as a dev-server and bundler for your hyperapp projects. Supports:
|
|
4
5
|
|
|
@@ -6,7 +7,7 @@ Use vite as a dev-server and bundler for your hyperapp projects. Supports:
|
|
|
6
7
|
- Hot module reloading (HMR)
|
|
7
8
|
- Server-side rendering (SSR)
|
|
8
9
|
|
|
9
|
-
##
|
|
10
|
+
## Get Started
|
|
10
11
|
|
|
11
12
|
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
|
|
|
@@ -14,19 +15,21 @@ To get up and running with Hyperapp and Vite in a few seconds, use [create-vite-
|
|
|
14
15
|
> npm create vite-hyperapp
|
|
15
16
|
```
|
|
16
17
|
|
|
17
|
-
|
|
18
|
+
> Use `npm create vite-hyperapp -- --help` for some command line options.
|
|
19
|
+
|
|
20
|
+
The starter will ask you for a folder in which to scaffold the project, and it will also ask if you would like to use typescript and/or SSR.
|
|
18
21
|
|
|
19
|
-
## Config
|
|
20
22
|
|
|
21
|
-
|
|
23
|
+
### Manual setup
|
|
24
|
+
|
|
25
|
+
In case you want to install manually, you need a project folder with `vite` installed, and a `vite.config.js` ready to go. Then you can install the vite-plugin for hyperapp, and enable it in the config:
|
|
22
26
|
|
|
23
27
|
```sh
|
|
24
28
|
> npm install vite-plugin-hyperapp
|
|
25
29
|
```
|
|
26
30
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
```
|
|
31
|
+
```js
|
|
32
|
+
// vite.config.js
|
|
30
33
|
import { defineConfig } from "vite"
|
|
31
34
|
import hyperapp from "vite-plugin-hyperapp"
|
|
32
35
|
|
|
@@ -34,49 +37,90 @@ export default defineConfig({
|
|
|
34
37
|
plugins: [hyperapp()],
|
|
35
38
|
})
|
|
36
39
|
```
|
|
40
|
+
> Options are passed as an object to `hyperapp({...})`. Available options described below.
|
|
37
41
|
|
|
38
|
-
|
|
42
|
+
In order to enable build TSX files to build, please add the following options to `compilerOptions` in your `tsconfig.json`:
|
|
39
43
|
|
|
44
|
+
```json
|
|
45
|
+
/* Necessary for tsx to work with vite-plugin-hyperapp */
|
|
46
|
+
"jsx": "preserve",
|
|
47
|
+
"jsxFactory": "jsx",
|
|
48
|
+
"jsxFragmentFactory": "jsxFragment"
|
|
40
49
|
```
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
50
|
+
|
|
51
|
+
and to make sure your editor's TS language server picks up the right types for JSX/TSX,
|
|
52
|
+
add this line in your `src/vite-env.d.ts`:
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
/// <reference types="vite-plugin-hyperapp/jsx" />
|
|
45
56
|
```
|
|
46
57
|
|
|
47
|
-
|
|
58
|
+
## JSX/TSX
|
|
48
59
|
|
|
49
|
-
|
|
60
|
+
Our JSX/TSX works the same as React/Preact, however, note that when you pass children to a component, like:
|
|
50
61
|
|
|
51
|
-
|
|
62
|
+
```jsx
|
|
63
|
+
<MyComponent>
|
|
64
|
+
<p>foo</p>
|
|
65
|
+
<p>bar</p>
|
|
66
|
+
</MyComponent>
|
|
67
|
+
```
|
|
52
68
|
|
|
69
|
+
they will arrive as an array of virtual nodes in the _second_ argument to the component.
|
|
70
|
+
|
|
71
|
+
```jsx
|
|
72
|
+
const MyComponent = (props, children) => {
|
|
73
|
+
//children will be: [<p>foo</p>, <p>bar</p>]
|
|
74
|
+
}
|
|
53
75
|
```
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
76
|
+
|
|
77
|
+
It's ok to put this array of children among other children, because the jsx transform will flatten the child-list:
|
|
78
|
+
|
|
79
|
+
```jsx
|
|
80
|
+
const MyComponent = (props, children) => (
|
|
81
|
+
<div>
|
|
82
|
+
<h1>Header</h1>
|
|
83
|
+
{children} {/* <--- this is fine */}
|
|
84
|
+
</div>
|
|
85
|
+
)
|
|
57
86
|
```
|
|
58
87
|
|
|
59
88
|
## Hot-module-reloading (HMR)
|
|
60
89
|
|
|
61
|
-
|
|
90
|
+
> The following is taken care of already, when you scaffold your app with `npm create vite-hyperapp`, but it is useful to be aware of
|
|
91
|
+
|
|
92
|
+
For hot-module-reloading to work, the plugin injects some code in the module where you initiate the app. In order for this to work, the app-initiation module must follow these two rules:
|
|
93
|
+
|
|
94
|
+
1. It must import `app` from `"hyperapp"` using named imports (i.e. with `{..app...}`, not `*`
|
|
95
|
+
2. The dispatch function returned from the app-call must be exported as a _named_ (not default) export.
|
|
62
96
|
|
|
63
|
-
|
|
97
|
+
By default, HMR expects the exported dispatch function to be named `dispatch`. If you choose to name it something else, let the plugin know by providing it to the plugin options:
|
|
64
98
|
|
|
65
99
|
```js
|
|
66
|
-
|
|
100
|
+
//vite.config.js
|
|
101
|
+
export default defineConfig({
|
|
102
|
+
plugins: [hyperapp({
|
|
103
|
+
dispatchExport: "myDispatchAlias"
|
|
104
|
+
})],
|
|
105
|
+
})
|
|
67
106
|
```
|
|
68
107
|
|
|
69
|
-
|
|
108
|
+
And if you'd rather use Vite's HMR api directly, or forego HMR entirely, you can use set that as an option in the plugin as well:
|
|
70
109
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
110
|
+
```js
|
|
111
|
+
//vite.config.js
|
|
112
|
+
export default defineConfig({
|
|
113
|
+
plugins: [hyperapp({
|
|
114
|
+
hmr: false
|
|
115
|
+
})],
|
|
116
|
+
})
|
|
117
|
+
```
|
|
74
118
|
|
|
75
119
|
## Server-side Rendering (SSR)
|
|
76
120
|
|
|
77
|
-
|
|
121
|
+
Server side rendering with Vite in general is described here: https://vitejs.dev/guide/ssr.html#server-side-rendering Below are some important details specific to Hyperapp and this plugin.
|
|
78
122
|
|
|
79
|
-
|
|
123
|
+
> When you use `npm create vite-hyperapp` to scaffold a project with SSR, these details are set up for you. But they're useful to be aware of as you build out your app.
|
|
80
124
|
|
|
81
125
|
### Redering your initial app to raw html
|
|
82
126
|
|
|
@@ -94,7 +138,7 @@ export async function render() {
|
|
|
94
138
|
|
|
95
139
|
`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
140
|
|
|
97
|
-
|
|
141
|
+
> 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
142
|
|
|
99
143
|
### Mountpoint defined by the view
|
|
100
144
|
|
|
@@ -136,3 +180,4 @@ Then you _DON'T_ add your ssr outlet like this:
|
|
|
136
180
|
```
|
|
137
181
|
|
|
138
182
|
And you make sure that the root-node of your view has `id="app"`.
|
|
183
|
+
|
package/index.d.ts
ADDED
package/jsx.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { Props, MaybeVNode, ElementVNode } from "hyperapp"
|
|
2
|
+
|
|
3
|
+
type Component<S, P> = (
|
|
4
|
+
props: P,
|
|
5
|
+
children: MaybeVNode<S>[],
|
|
6
|
+
) => MaybeVNode<S> | MaybeVNode<S>[]
|
|
7
|
+
|
|
8
|
+
type _JSXChild<S> = string | number | MaybeVNode<S>
|
|
9
|
+
type JSXChild<S> = _JSXChild<S> | _JSXChild<S>[]
|
|
10
|
+
|
|
11
|
+
export declare function jsx<S>(
|
|
12
|
+
type: string,
|
|
13
|
+
props?: Props<S>,
|
|
14
|
+
...children: JSXChild<S>[]
|
|
15
|
+
): ElementVNode<S>
|
|
16
|
+
|
|
17
|
+
export declare function jsx<S, P>(
|
|
18
|
+
type: Component<S, P>,
|
|
19
|
+
props?: P | null,
|
|
20
|
+
...children: JSXChild<S>[]
|
|
21
|
+
): ReturnType<Component<S, P>>
|
|
22
|
+
|
|
23
|
+
export declare function jsxFragment<S>(
|
|
24
|
+
_: any,
|
|
25
|
+
children: JSXChild<S>[],
|
|
26
|
+
): MaybeVNode<S>[]
|
|
27
|
+
|
|
28
|
+
declare global {
|
|
29
|
+
namespace JSX {
|
|
30
|
+
/** Result of a JSX expression. Components may return arrays. */
|
|
31
|
+
type Element = MaybeVNode<any> | MaybeVNode<any>[]
|
|
32
|
+
|
|
33
|
+
/** What you can put in the `<X />` position */
|
|
34
|
+
type ElementType = string | Component<any, any>
|
|
35
|
+
|
|
36
|
+
/** Allow `<>...</>` fragments; returns an array */
|
|
37
|
+
type Fragment = MaybeVNode<any>[]
|
|
38
|
+
|
|
39
|
+
/** Intrinsic (HTML/SVG) elements. */
|
|
40
|
+
interface IntrinsicElements {
|
|
41
|
+
[elem: string]: Props<any>
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** prevents errors about children missing from props in components */
|
|
45
|
+
type LibraryManagedAttributes<P> = P & { children?: JSXChild<any>[] }
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// make jsx and jsxFragment ambient globals because
|
|
50
|
+
// ts server doesn't know I'll add them later in transform
|
|
51
|
+
type JsxFn = typeof jsx
|
|
52
|
+
type JsxFragmentFn = typeof jsxFragment
|
|
53
|
+
|
|
54
|
+
declare global {
|
|
55
|
+
const jsx: JsxFn
|
|
56
|
+
const jsxFragment: JsxFragmentFn
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export {}
|
package/jsx.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { h, text } from "hyperapp"
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
|
|
3
|
+
const cproc = children =>
|
|
4
|
+
children
|
|
5
|
+
.flat()
|
|
6
|
+
.map(x => ((typeof x === "string") | (typeof x === "number") ? text(x) : x))
|
|
5
7
|
|
|
6
|
-
export const
|
|
8
|
+
export const jsx = (tag, props, ...children) =>
|
|
7
9
|
typeof tag === "function"
|
|
8
|
-
? tag(props, children)
|
|
9
|
-
: h(tag, props || {}, children
|
|
10
|
+
? tag(props, cproc(children))
|
|
11
|
+
: h(tag, props || {}, cproc(children))
|
|
10
12
|
|
|
11
|
-
export const jsxFragment = (_, children) =>
|
|
12
|
-
return children.flat(2).map(nodeify)
|
|
13
|
-
}
|
|
13
|
+
export const jsxFragment = (_, children) => cproc(children)
|
package/package.json
CHANGED
|
@@ -1,16 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-hyperapp",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Plugin and helpers for using Hyperapp with Vite",
|
|
6
|
-
"main": "index.js",
|
|
6
|
+
"main": "./index.js",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
9
9
|
},
|
|
10
10
|
"author": "Zacharias Enochsson",
|
|
11
11
|
"license": "MIT",
|
|
12
|
+
"files": [
|
|
13
|
+
"*.js",
|
|
14
|
+
"*.d.ts"
|
|
15
|
+
],
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./index.d.ts",
|
|
19
|
+
"import": "./index.js",
|
|
20
|
+
"default": "./index.js"
|
|
21
|
+
},
|
|
22
|
+
"./jsx": {
|
|
23
|
+
"types": "./jsx.d.ts",
|
|
24
|
+
"import": "./jsx.js",
|
|
25
|
+
"default": "./jsx.js"
|
|
26
|
+
},
|
|
27
|
+
"./jsx.js": {
|
|
28
|
+
"types": "./jsx.d.ts",
|
|
29
|
+
"import": "./jsx.js",
|
|
30
|
+
"default": "./jsx.js"
|
|
31
|
+
},
|
|
32
|
+
"./ssr": {
|
|
33
|
+
"types": "./ssr.d.ts",
|
|
34
|
+
"import": "./ssr.js",
|
|
35
|
+
"default": "./ssr.js"
|
|
36
|
+
},
|
|
37
|
+
"./ssr.js": {
|
|
38
|
+
"types": "./ssr.d.ts",
|
|
39
|
+
"import": "./ssr.js",
|
|
40
|
+
"default": "./ssr.js"
|
|
41
|
+
},
|
|
42
|
+
"./package.json": "./package.json"
|
|
43
|
+
},
|
|
12
44
|
"dependencies": {
|
|
13
45
|
"jsdom": "^24.1.0",
|
|
14
46
|
"hyperapp": "^2.0.22"
|
|
47
|
+
},
|
|
48
|
+
"peerDependencies": {
|
|
49
|
+
"vite": "^4.5.14"
|
|
15
50
|
}
|
|
16
51
|
}
|
package/ssr.d.ts
CHANGED
package/jsx-runtime.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
This type declaration is named 'jsx-runtime.d.tx' even though
|
|
3
|
-
jsx is compiled in the classic way. It's only named this to make
|
|
4
|
-
it easier for tsconfig.json to be ok with jsx in .tsx-files.
|
|
5
|
-
*/
|
|
6
|
-
import { Props } from "hyperapp"
|
|
7
|
-
declare global {
|
|
8
|
-
module JSX {
|
|
9
|
-
type IntrinsicElements = Record<string, Props<any>>
|
|
10
|
-
}
|
|
11
|
-
}
|