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 CHANGED
@@ -1,4 +1,5 @@
1
- # vite-plugin-hyperapp
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
- ## Quickstart
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
- This will set you up with all the necessary configuration described below.
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
- To manually set up your vite-project for use with Hyperapp, install this plugin:
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
- And add it to the plugins in your `vite.config.js`:
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
- You can pass an options object to the plugin, e.g:
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
- plugins: [hyperapp({
42
- optionA: 'foo',
43
- optionB: 'bar',
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
- ...but most of the time you won't need it. See "Hot-module-reloading" below for some options that might be relevant.
58
+ ## JSX/TSX
48
59
 
49
- ## Typescript
60
+ Our JSX/TSX works the same as React/Preact, however, note that when you pass children to a component, like:
50
61
 
51
- To make sure you get proper typing support in your `.tsx` files, make sure to add these compiler options to your `tsconfig.json`:
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
- /* Necessary for tsx to work with vite-plugin-hyperapp*/
55
- "jsx": "preserve",
56
- "jsxImportSource": "vite-plugin-hyperapp"
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
- 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:
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
- - It must contain:
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
- import {..., app [as something],...} from 'hyperapp'
100
+ //vite.config.js
101
+ export default defineConfig({
102
+ plugins: [hyperapp({
103
+ dispatchExport: "myDispatchAlias"
104
+ })],
105
+ })
67
106
  ```
68
107
 
69
- - It must _export_ a _named_ (not default) constant/variable `dispatch`, which should be the function retuned from calling `app({...})`.
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
- 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`
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
- 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)
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
- Here are some specifics you'll need to know when using hyperapp and this plugin:
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
- 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.
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
@@ -0,0 +1,10 @@
1
+ import { type PluginOption } from "vite"
2
+
3
+ export interface HyperappPluginOptions {
4
+ hmr?: boolean
5
+ dispatchExport?: string
6
+ }
7
+
8
+ export default function hyperapp(
9
+ options?: HyperappIntegrationOptions,
10
+ ): PluginOption
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 nodeify = x =>
4
- (typeof x === "string") | (typeof x === "number") ? text(x) : x
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 jsxNode = (tag, props, ...children) =>
8
+ export const jsx = (tag, props, ...children) =>
7
9
  typeof tag === "function"
8
- ? tag(props, children)
9
- : h(tag, props || {}, children.flat(2).map(nodeify))
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.1.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
@@ -2,7 +2,7 @@ import { Subscription } from "hyperapp"
2
2
 
3
3
  type Defs<S = any> = {
4
4
  init?: Dispatchable<S>
5
- view?: (state: S) => ElementVNode<S>
5
+ view?: (state: S) => VNode<S>
6
6
  subscriptions?: (state: S) => Subscription<S>[]
7
7
  dispatch?: (d: Dispatch<S>) => Dispatch<S>
8
8
  }
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
- }