vite-plugin-vanjs 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 thednp
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,145 @@
1
+ ## vite-plugin-vanjs
2
+
3
+ [![NPM Version](https://img.shields.io/npm/v/vite-plugin-vanjs.svg)](https://www.npmjs.com/package/vite-plugin-vanjs)
4
+ [![NPM Downloads](https://img.shields.io/npm/dm/vite-plugin-vanjs.svg)](http://npm-stat.com/charts.html?package=vite-plugin-vanjs)
5
+ [![typescript version](https://img.shields.io/badge/typescript-5.6.2-brightgreen)](https://www.typescriptlang.org/)
6
+ [![vanjs-core version](https://img.shields.io/badge/vanjs--core-1.5.2-brightgreen)](https://github.com/vanjs-org/van)
7
+ [![mini-van-plate version](https://img.shields.io/badge/mini--van--plate-0.6.1-brightgreen)](https://github.com/vanjs-org/mini-van-plate)
8
+ [![vite version](https://img.shields.io/badge/vite-6.0.5-brightgreen)](https://github.com/vitejs)
9
+
10
+ A Vite plugin for VanJS to enable JSX transformation and simplify application development.
11
+
12
+ This plugin uses and requires the `mini-van-plate` package in order to register the Van and VanX objects in an isomorphic server/client enviroment. If you're planning to do SSR apps, the `van-ext` is also required.
13
+
14
+ The recommended templates for you to use are the [vite-starter-vanjs-ssr](https://github.com/thednp/vite-starter-vanjs-ssr) and [vite-starter-vanjs-ssr-jsx](https://github.com/thednp/vite-starter-vanjs-ssr-jsx) which already feature this plugin.
15
+
16
+ If you don't need an SSR/SSG application, you simply make use of the JSX transformation capability.
17
+
18
+ ### Install
19
+
20
+ 1) Install the plugin:
21
+
22
+ ```bash
23
+ pnpm add vite-plugin-vanjs
24
+ ```
25
+
26
+ ```bash
27
+ npm install vite-plugin-vanjs
28
+ ```
29
+
30
+ ```bash
31
+ deno add npm:vite-plugin-vanjs
32
+ ```
33
+
34
+ 2) To add typescript support, edit your `src/vite-env.d.ts` as follows:
35
+
36
+ ```ts
37
+ /// <reference types="vite/client" />
38
+ /// <reference types="vite-plugin-vanjs" />
39
+ ```
40
+
41
+
42
+ ### Usage
43
+
44
+ Update your `vite.config.mts` file:
45
+ ```ts
46
+ // vite.config.mts
47
+ import { defineConfig } from 'vite';
48
+ import vanjs from 'vite-plugin-vanjs';
49
+
50
+ export default defineConfig({
51
+ plugins: [vanjs()],
52
+ });
53
+ ```
54
+
55
+
56
+ **Example**:
57
+
58
+ For your convenience and for your SSR applications/extensions expecially, you need to import from `@vanjs/van` virtual module, so **vite-plugin-vanjs** makes sure to load the right modules where needed.
59
+
60
+ ```ts
61
+ // my-component.ts
62
+ import van from '@vanjs/van';
63
+
64
+ // use van as usual
65
+ const MyComponent = () => {
66
+ return van.div("Hello from VanJS!");
67
+ };
68
+ ```
69
+ The plugin also exports `vanX`.
70
+
71
+ ```ts
72
+ // my-list.ts
73
+ import van from '@vanjs/van';
74
+ import vanX from '@vanjs/vanX';
75
+
76
+ type ListItem = { text: string, done: boolean };
77
+
78
+ // use VanJS as usual
79
+ const MyList = () => {
80
+ const items = vanX.reactive<ListItem[]>([]);
81
+ const { div, button, span, a, input, del } = van.tags;
82
+ const inputDom = input({ type: "text", placeholder: "your new item" });
83
+ return div(
84
+ inputDom,
85
+ button({onclick: () => items.push({text: inputDom.value, done: false})}, "Add"),
86
+ vanX.list(div, items, ({val: v}, deleter) => div(
87
+ input({type: "checkbox", checked: () => v.done, onclick: (e: TodoClickEvent) => v.done = e.target.checked}),
88
+ () => (v.done ? del : span)(v.text),
89
+ button({ onclick: deleter }, "Remove"),
90
+ )),
91
+ )
92
+ };
93
+ ```
94
+
95
+
96
+ ### JSX Transformation
97
+
98
+ To enable JSX transformation, you have to edit your `tsconfig.json` as follows:
99
+
100
+ ```json
101
+ {
102
+ "compilerOptions": {
103
+ /* {... your other compiler options } */
104
+ "jsx": "preserve",
105
+ "jsxImportSource": "@vanjs/jsx"
106
+ }
107
+ }
108
+ ```
109
+
110
+ **Example**:
111
+ ```tsx
112
+ // App.tsx
113
+ import type { ChildDom } from "vanjs-core";
114
+ import van from '@vanjs/van';
115
+
116
+ const App = () => {
117
+ const count = van.state(0);
118
+
119
+ return (
120
+ <button onClick={() => count.val++}>{count}</button>
121
+ );
122
+ }
123
+
124
+ van.add(document.getElementById("app")!, <App /> as ChildDom);
125
+
126
+ ```
127
+
128
+ **Notes**:
129
+ * in some cases like this one, enforcing a certain typescript type via `as` might be in good order depending on who renders your component;
130
+ * you can use `ref` as a `van.state`, `class` instead of `className`, `for` instead of `htmlFor`;
131
+ * you can use style as both an object and a string;
132
+ * for a JSX started template, check out the [vite-starter-vanjs-ssr-jsx](https://github.com/thednp/vite-starter-vanjs-ssr-jsx).
133
+ * for a pure started template, check out the [vite-starter-vanjs-ssr](https://github.com/thednp/vite-starter-vanjs-ssr).
134
+
135
+
136
+
137
+ ### Credits
138
+ * [van-jsx](https://github.com/herudi/van-jsx) a simple Vanilla JSX implementation;
139
+ * [vanjs-jsx](https://github.com/vanjs-org/van/tree/main/addons/van_jsx) the official VanJS addon;
140
+ * [surplus](https://github.com/adamhaile/surplus/blob/master/index.d.ts) for Typescript definitions also used by SolidJS;
141
+ * [inferno](https://github.com/infernojs/inferno/blob/master/packages/inferno/src/core/types.ts) also typescript definitions.
142
+
143
+
144
+ ### License
145
+ Released under [MIT](LICENSE).
@@ -0,0 +1,8 @@
1
+ import type { ChildDom } from "vanjs-core";
2
+ import type { JSX } from "./jsx";
3
+
4
+ export type FragmentProps = {
5
+ children?: JSX.Element;
6
+ };
7
+
8
+ export type Fragment = (props: FragmentProps) => JSX.Element;
@@ -0,0 +1 @@
1
+ export const Fragment = ({ children }) => children;
@@ -0,0 +1,28 @@
1
+ declare module "@vanjs/jsx" {
2
+ import type { ChildDom, Primitive } from "vanjs-core";
3
+ export const Fragment = ({ children }: { children?: JSX.Element }) =>
4
+ JSX.Element;
5
+ export type VanElement = Element;
6
+ export type VanNode =
7
+ | Primitive
8
+ | ChildDom
9
+ | VanElement
10
+ | VanNode[]
11
+ | (() => VanNode)
12
+ | null;
13
+
14
+ export function jsx(
15
+ tag: string | ((props: any) => VanNode),
16
+ props: {
17
+ children?: VanNode;
18
+ style?: string | CSSProperties | (() => CSSProperties);
19
+ ref?: State<Element>;
20
+ [key: string]: any;
21
+ },
22
+ ): VanNode;
23
+
24
+ export { jsx as createElement, jsx as jsxDEV, jsx as jsxs };
25
+ }
26
+
27
+ declare module "@vanjs/jsx/jsx-runtime" {}
28
+ declare module "@vanjs/jsx/jsx-dev-runtime" {}
package/jsx/index.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ /// <reference path="global.d.ts" />
2
+ import type { JSX } from "./jsx.d.ts";
3
+ import type { Fragment } from "./fragment.d.ts";
4
+
5
+ declare function jsx(
6
+ type: any,
7
+ props: any,
8
+ ): () =>
9
+ | (Node & {
10
+ [key: string]: any;
11
+ })
12
+ | (Node & {
13
+ [key: string]: any;
14
+ })[];
15
+
16
+ export { Fragment, jsx, jsx as jsxDEV, jsx as jsxs };
package/jsx/index.mjs ADDED
@@ -0,0 +1,4 @@
1
+ // Export everything through the main entry point
2
+ export { Fragment } from "./fragment.mjs";
3
+ export { jsx } from "./jsx.mjs";
4
+ export { jsx as createElement, jsx as jsxDEV, jsx as jsxs } from "./jsx.mjs";
@@ -0,0 +1 @@
1
+ export * from "./jsx.d.ts";
@@ -0,0 +1,7 @@
1
+ export {
2
+ Fragment,
3
+ jsx,
4
+ jsx as createElement,
5
+ jsx as jsxDEV,
6
+ jsx as jsxs,
7
+ } from "./index.mjs";
@@ -0,0 +1 @@
1
+ export * from "./jsx.d.ts";
@@ -0,0 +1,7 @@
1
+ export {
2
+ Fragment,
3
+ jsx,
4
+ jsx as createElement,
5
+ jsx as jsxDEV,
6
+ jsx as jsxs,
7
+ } from "./index.mjs";