vite-plugin-vanjs 0.0.4 → 0.0.6
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 +0 -0
- package/README.md +31 -28
- package/client/global.d.ts +5 -1
- package/client/index.mjs +99 -5
- package/client/package.json +0 -0
- package/client/types.d.ts +5 -1
- package/jsx/fragment.d.ts +0 -0
- package/jsx/fragment.mjs +0 -0
- package/jsx/global.d.ts +3 -2
- package/jsx/index.mjs +3 -3
- package/jsx/jsx-dev-runtime.d.ts +0 -0
- package/jsx/jsx-dev-runtime.mjs +1 -1
- package/jsx/jsx-runtime.d.ts +0 -0
- package/jsx/jsx-runtime.mjs +1 -1
- package/jsx/jsx.d.ts +42 -9
- package/jsx/jsx.mjs +10 -10
- package/jsx/package.json +0 -0
- package/jsx/types.d.ts +0 -0
- package/jsx-dev-runtime.d.ts +0 -0
- package/jsx-runtime.d.ts +0 -0
- package/meta/Head.mjs +4 -4
- package/meta/global.d.ts +24 -12
- package/meta/helpers.mjs +5 -3
- package/meta/index.mjs +3 -3
- package/meta/package.json +0 -0
- package/meta/tags.mjs +6 -8
- package/meta/types.d.ts +7 -7
- package/package.json +20 -18
- package/router/a.mjs +16 -10
- package/router/global.d.ts +75 -16
- package/router/helpers.mjs +28 -9
- package/router/index.mjs +7 -6
- package/router/lazy.mjs +2 -2
- package/router/package.json +0 -0
- package/router/router.mjs +20 -13
- package/router/routes.mjs +2 -2
- package/router/state.mjs +2 -1
- package/router/types.d.ts +86 -22
- package/server/global.d.ts +1 -0
- package/server/index.mjs +3 -3
- package/server/package.json +0 -0
- package/server/types.d.ts +1 -1
- package/setup/global.d.ts +0 -0
- package/setup/index-debug.mjs +21 -0
- package/setup/index.mjs +0 -0
- package/setup/package.json +0 -0
- package/setup/types.d.ts +0 -0
- package/setup/van-debug.mjs +2 -0
- package/setup/van.d.ts +0 -0
- package/setup/van.mjs +0 -0
- package/setup/vanX.d.ts +0 -0
- package/setup/vanX.mjs +0 -0
- package/src/index.mjs +19 -4
- package/src/types.d.ts +2 -3
- package/tsconfig.json +1 -1
package/meta/tags.mjs
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import van from "vanjs-core";
|
|
2
|
-
import { addMeta } from "./Head";
|
|
2
|
+
import { addMeta } from "./Head.mjs";
|
|
3
3
|
|
|
4
4
|
/** @typedef {import("vanjs-core").PropsWithKnownKeys} PropsWithKnownKeys */
|
|
5
5
|
/** @typedef {import("./types.d.ts").TagProps} TagProps */
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
|
-
* Add a new title tag
|
|
8
|
+
* Add a new `<title>` tag
|
|
9
9
|
* @type {(props: PropsWithKnownKeys<HTMLTitleElement>, content?: string) => null}
|
|
10
10
|
*/
|
|
11
11
|
export const Title = (props, content) => {
|
|
@@ -15,7 +15,7 @@ export const Title = (props, content) => {
|
|
|
15
15
|
};
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
|
-
* Add a new meta tag
|
|
18
|
+
* Add a new `<meta>` tag
|
|
19
19
|
* @type {(props: PropsWithKnownKeys<HTMLMetaElement>) => null}
|
|
20
20
|
*/
|
|
21
21
|
export const Meta = (props) => {
|
|
@@ -25,7 +25,7 @@ export const Meta = (props) => {
|
|
|
25
25
|
};
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
|
-
* Add a new link tag
|
|
28
|
+
* Add a new `<link>` tag
|
|
29
29
|
* @type {(props: PropsWithKnownKeys<HTMLLinkElement>) => null}
|
|
30
30
|
*/
|
|
31
31
|
export const Link = (props) => {
|
|
@@ -35,19 +35,17 @@ export const Link = (props) => {
|
|
|
35
35
|
};
|
|
36
36
|
|
|
37
37
|
/**
|
|
38
|
-
* Add a new script tag
|
|
38
|
+
* Add a new `<script>` tag
|
|
39
39
|
* @type {(props: PropsWithKnownKeys<HTMLScriptElement>, content?: string) => null}
|
|
40
40
|
*/
|
|
41
41
|
export const Script = (props, content) => {
|
|
42
42
|
const { script } = van.tags;
|
|
43
|
-
// const realContent = content ? content.toString() : props;
|
|
44
|
-
// const realProps = content ? props : props;
|
|
45
43
|
addMeta(script(props, content));
|
|
46
44
|
return null;
|
|
47
45
|
};
|
|
48
46
|
|
|
49
47
|
/**
|
|
50
|
-
* Add a new style tag
|
|
48
|
+
* Add a new `<style>` tag
|
|
51
49
|
* @type {(props: PropsWithKnownKeys<HTMLStyleElement>, content: string) => null}
|
|
52
50
|
*/
|
|
53
51
|
export const Style = (props, content) => {
|
package/meta/types.d.ts
CHANGED
|
@@ -7,10 +7,10 @@ export const createHeadTags: () => Map<
|
|
|
7
7
|
SupportedTags | TagFunc<SupportedTags>
|
|
8
8
|
>;
|
|
9
9
|
|
|
10
|
-
export const getHeadTags =
|
|
11
|
-
Map<string, SupportedTags | TagFunc<SupportedTags
|
|
10
|
+
export const getHeadTags =
|
|
11
|
+
() => (Map<string, SupportedTags | TagFunc<SupportedTags>>);
|
|
12
12
|
export const resetHeadTags: () => void;
|
|
13
|
-
export const initializeHeadTags: () => void;
|
|
13
|
+
export const initializeHeadTags: () => void | (() => Promise<void>);
|
|
14
14
|
|
|
15
15
|
export type SupportedTags =
|
|
16
16
|
| HTMLTitleElement
|
|
@@ -21,13 +21,13 @@ export type SupportedTags =
|
|
|
21
21
|
|
|
22
22
|
export type TagProps = SupportedTags | PropsWithKnownKeys<SupportedTags>;
|
|
23
23
|
|
|
24
|
-
export type HeadTags = SupportedTags[] | TagFunc
|
|
24
|
+
export type HeadTags = SupportedTags[] | TagFunc[];
|
|
25
25
|
|
|
26
|
-
export
|
|
26
|
+
export const addMeta = (_tag: string | TagProps) => null;
|
|
27
27
|
|
|
28
|
-
export
|
|
28
|
+
export const Head: () => HeadTags;
|
|
29
29
|
|
|
30
|
-
export
|
|
30
|
+
export const parseAttributes: (
|
|
31
31
|
attributeString: string,
|
|
32
32
|
) => Record<string, string>;
|
|
33
33
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-vanjs",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
4
4
|
"author": "thednp",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "A mini meta-framework for VanJS powered by Vite",
|
|
@@ -74,6 +74,17 @@
|
|
|
74
74
|
"import": "./jsx/jsx-dev-runtime.mjs"
|
|
75
75
|
}
|
|
76
76
|
},
|
|
77
|
+
"scripts": {
|
|
78
|
+
"test": "vitest --config vitest.config.mts",
|
|
79
|
+
"test-ui": "vitest --config vitest.config.mts --ui=true",
|
|
80
|
+
"prepublishOnly": "pnpm up && pnpm lint && pnpm format && pnpm badges",
|
|
81
|
+
"badges": "npx -p dependency-version-badge update-badge vanjs-core mini-van-plate typescript vitest vite",
|
|
82
|
+
"format": "deno fmt src meta router setup client server jsx",
|
|
83
|
+
"lint": "pnpm lint:ts && pnpm check:ts",
|
|
84
|
+
"lint:ts": "deno lint src meta router setup client server jsx",
|
|
85
|
+
"fix:ts": "deno lint src meta router setup client server jsx --fix",
|
|
86
|
+
"check:ts": "tsc -noEmit"
|
|
87
|
+
},
|
|
77
88
|
"peerDependencies": {
|
|
78
89
|
"csstype": "*",
|
|
79
90
|
"mini-van-plate": "*",
|
|
@@ -87,26 +98,17 @@
|
|
|
87
98
|
"vanjs-ext": "^0.6.2"
|
|
88
99
|
},
|
|
89
100
|
"devDependencies": {
|
|
90
|
-
"@vitest/browser": "^3.0.
|
|
91
|
-
"@vitest/coverage-istanbul": "^3.0.
|
|
92
|
-
"@vitest/ui": "^3.0.
|
|
93
|
-
"happy-dom": "^16.
|
|
101
|
+
"@vitest/browser": "^3.0.8",
|
|
102
|
+
"@vitest/coverage-istanbul": "^3.0.8",
|
|
103
|
+
"@vitest/ui": "^3.0.8",
|
|
104
|
+
"happy-dom": "^16.8.1",
|
|
94
105
|
"typescript": "5.6.2",
|
|
95
|
-
"vite": "^6.
|
|
96
|
-
"vitest": "^3.0.
|
|
106
|
+
"vite": "^6.2.2",
|
|
107
|
+
"vitest": "^3.0.8"
|
|
97
108
|
},
|
|
109
|
+
"packageManager": "pnpm@8.6.12",
|
|
98
110
|
"engines": {
|
|
99
111
|
"node": ">=20",
|
|
100
112
|
"pnpm": ">=8.6.0"
|
|
101
|
-
},
|
|
102
|
-
"scripts": {
|
|
103
|
-
"test": "vitest --config vitest.config.mts",
|
|
104
|
-
"test-ui": "vitest --config vitest.config.mts --ui=true",
|
|
105
|
-
"badges": "npx -p dependency-version-badge update-badge vanjs-core mini-van-plate typescript vitest vite",
|
|
106
|
-
"format": "deno fmt src meta router setup client server jsx",
|
|
107
|
-
"lint": "pnpm lint:ts && pnpm check:ts",
|
|
108
|
-
"lint:ts": "deno lint src meta router setup client server jsx",
|
|
109
|
-
"fix:ts": "deno lint src meta router setup client server jsx --fix",
|
|
110
|
-
"check:ts": "tsc -noEmit"
|
|
111
113
|
}
|
|
112
|
-
}
|
|
114
|
+
}
|
package/router/a.mjs
CHANGED
|
@@ -1,19 +1,25 @@
|
|
|
1
1
|
// router/a.mjs
|
|
2
2
|
import van from "vanjs-core";
|
|
3
|
-
import setup from "../setup/index";
|
|
4
|
-
import { matchRoute } from "./routes";
|
|
5
|
-
import { executeLifecycle, isCurrentPage, navigate } from "./helpers";
|
|
3
|
+
import setup from "../setup/index.mjs";
|
|
4
|
+
import { matchRoute } from "./routes.mjs";
|
|
5
|
+
import { executeLifecycle, isCurrentPage, navigate } from "./helpers.mjs";
|
|
6
|
+
|
|
7
|
+
/** @typedef {typeof import("./types").A} A */
|
|
6
8
|
|
|
7
9
|
/**
|
|
8
|
-
* @
|
|
9
|
-
* @param {...(Element | Node)[]} children
|
|
10
|
-
* @returns {HTMLAnchorElement}
|
|
10
|
+
* @type {A}
|
|
11
11
|
*/
|
|
12
|
-
export const A = (
|
|
13
|
-
|
|
12
|
+
export const A = (
|
|
13
|
+
/* istanbul ignore next */ { href, children, ...rest } = {},
|
|
14
|
+
...otherChildren
|
|
15
|
+
) => {
|
|
16
|
+
/* istanbul ignore next - try again later */
|
|
17
|
+
const props = Object.fromEntries(
|
|
18
|
+
Object.entries(rest || {}).filter(([_, val]) => val),
|
|
19
|
+
);
|
|
14
20
|
const newProps = {
|
|
15
21
|
href,
|
|
16
|
-
...
|
|
22
|
+
...props,
|
|
17
23
|
};
|
|
18
24
|
|
|
19
25
|
van.derive(() => {
|
|
@@ -22,7 +28,7 @@ export const A = (props, ...children) => {
|
|
|
22
28
|
}
|
|
23
29
|
});
|
|
24
30
|
|
|
25
|
-
const anchor = van.tags.a(newProps, ...children);
|
|
31
|
+
const anchor = van.tags.a(newProps, [...(children || []), ...otherChildren]);
|
|
26
32
|
/* istanbul ignore else */
|
|
27
33
|
if (!setup.isServer) {
|
|
28
34
|
anchor.addEventListener("click", async (e) => {
|
package/router/global.d.ts
CHANGED
|
@@ -1,10 +1,54 @@
|
|
|
1
1
|
declare module "@vanjs/router" {
|
|
2
|
-
import type {
|
|
2
|
+
import type {
|
|
3
|
+
Element as VElement,
|
|
4
|
+
TagFunc as IsoTagFunc,
|
|
5
|
+
} from "mini-van-plate/van-plate";
|
|
3
6
|
import van from "vanjs-core";
|
|
4
|
-
import type {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
import type {
|
|
8
|
+
ChildDom,
|
|
9
|
+
Primitive,
|
|
10
|
+
Props,
|
|
11
|
+
PropsWithKnownKeys,
|
|
12
|
+
PropValueOrDerived,
|
|
13
|
+
State,
|
|
14
|
+
} from "vanjs-core";
|
|
15
|
+
|
|
16
|
+
// type VanElement = VElement | Exclude<Primitive, boolean | undefined>;
|
|
17
|
+
type DOMElement = globalThis.Element;
|
|
18
|
+
type PrimitiveChild = Primitive | State<Primitive | undefined | null>;
|
|
19
|
+
|
|
20
|
+
type VanElement =
|
|
21
|
+
| SVGElement
|
|
22
|
+
| HTMLElement
|
|
23
|
+
| DOMElement
|
|
24
|
+
| Node
|
|
25
|
+
| VElement
|
|
26
|
+
| PrimitiveChild;
|
|
27
|
+
export type VanNode =
|
|
28
|
+
| VanElement
|
|
29
|
+
| VanNode[]
|
|
30
|
+
| (() => VanNode)
|
|
31
|
+
| null
|
|
32
|
+
| undefined;
|
|
33
|
+
|
|
34
|
+
type ComponentProps1<T> =
|
|
35
|
+
& Props
|
|
36
|
+
& PropsWithKnownKeys<T>;
|
|
37
|
+
|
|
38
|
+
type ComponentProps<K extends keyof HTMLElementTagNameMap> =
|
|
39
|
+
& Props
|
|
40
|
+
& PropsWithKnownKeys<HTMLElementTagNameMap[K]>;
|
|
41
|
+
|
|
42
|
+
export type VanComponent<
|
|
43
|
+
K extends keyof HTMLElementTagNameMap = "div",
|
|
44
|
+
O extends (Record<string, PropValueOrDerived> | undefined) = undefined,
|
|
45
|
+
> = {
|
|
46
|
+
(
|
|
47
|
+
props?: O extends object ? ComponentProps<K> & Partial<O>
|
|
48
|
+
: ComponentProps<K>,
|
|
49
|
+
...children: ChildDom[]
|
|
50
|
+
): HTMLElementTagNameMap[K];
|
|
51
|
+
};
|
|
8
52
|
|
|
9
53
|
// router.mjs
|
|
10
54
|
/**
|
|
@@ -18,7 +62,14 @@ declare module "@vanjs/router" {
|
|
|
18
62
|
* return Router(); // or <Router /> for JSX
|
|
19
63
|
* }
|
|
20
64
|
*/
|
|
21
|
-
export const Router:
|
|
65
|
+
// export const Router: JSX.Component<HTMLElement> & VanComponent<HTMLElement>;
|
|
66
|
+
// export function Router(props?: ComponentProps<HTMLElement>): HTMLElement;
|
|
67
|
+
// export function Router(
|
|
68
|
+
// props?: JSX.IntrinsicElements["main"] & { children: null },
|
|
69
|
+
// ): HTMLElement;
|
|
70
|
+
export const Router:
|
|
71
|
+
& VanComponent<"main">
|
|
72
|
+
& JSX.Component<"main">;
|
|
22
73
|
|
|
23
74
|
// a.mjs
|
|
24
75
|
/**
|
|
@@ -38,10 +89,9 @@ declare module "@vanjs/router" {
|
|
|
38
89
|
* );
|
|
39
90
|
* }
|
|
40
91
|
*/
|
|
41
|
-
export const A:
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
) => HTMLAnchorElement;
|
|
92
|
+
export const A:
|
|
93
|
+
& VanComponent<"a">
|
|
94
|
+
& JSX.Component<"a">;
|
|
45
95
|
|
|
46
96
|
// helpers.mjs
|
|
47
97
|
/**
|
|
@@ -69,8 +119,6 @@ declare module "@vanjs/router" {
|
|
|
69
119
|
*/
|
|
70
120
|
export const redirect: (href?: string) => void | (() => void);
|
|
71
121
|
|
|
72
|
-
export type VanComponent = () => VanNode | VanNode[];
|
|
73
|
-
|
|
74
122
|
// routes.mjs
|
|
75
123
|
export type RouteEntry = {
|
|
76
124
|
path: string;
|
|
@@ -137,18 +185,23 @@ declare module "@vanjs/router" {
|
|
|
137
185
|
* @param {...VanNode[]} children
|
|
138
186
|
*/
|
|
139
187
|
export const unwrap: (
|
|
140
|
-
source:
|
|
188
|
+
source:
|
|
189
|
+
| VanNode
|
|
190
|
+
| IsoTagFunc
|
|
191
|
+
| VanNode[]
|
|
192
|
+
| IsoTagFunc[]
|
|
193
|
+
| (() => IsoTagFunc | IsoTagFunc[] | VanNode | VanNode[]),
|
|
141
194
|
...children: VanNode[]
|
|
142
195
|
) => VanNode;
|
|
143
196
|
|
|
144
197
|
export type ComponentModule = {
|
|
145
|
-
component: VanComponent;
|
|
198
|
+
component: VanComponent | JSX.Element;
|
|
146
199
|
route: Pick<RouteEntry, "load" | "preload">;
|
|
147
200
|
};
|
|
148
201
|
|
|
149
202
|
export type DynamicModule = {
|
|
150
|
-
Page: VanComponent;
|
|
151
|
-
default?: VanComponent;
|
|
203
|
+
Page: VanComponent | JSX.Element;
|
|
204
|
+
default?: VanComponent | JSX.Element;
|
|
152
205
|
route?: Pick<RouteEntry, "load" | "preload">;
|
|
153
206
|
};
|
|
154
207
|
|
|
@@ -159,4 +212,10 @@ declare module "@vanjs/router" {
|
|
|
159
212
|
* @param importFn
|
|
160
213
|
*/
|
|
161
214
|
export const lazy: (importFn: () => LazyComponent) => () => ComponentModule;
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Fixes the URL of a route.
|
|
218
|
+
* @param url
|
|
219
|
+
*/
|
|
220
|
+
export const fixRouteUrl: (url: string) => string;
|
|
162
221
|
}
|
package/router/helpers.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import { routerState, setRouterState } from "./state";
|
|
4
|
-
import { matchRoute } from "./routes";
|
|
1
|
+
// import van from "vanjs-core";
|
|
2
|
+
import setup from "../setup/index.mjs";
|
|
3
|
+
import { routerState, setRouterState } from "./state.mjs";
|
|
4
|
+
import { matchRoute } from "./routes.mjs";
|
|
5
5
|
|
|
6
6
|
/** @typedef {import("./types.d.ts").Route} Route */
|
|
7
7
|
/** @typedef {import("./types.d.ts").VanNode} VanNode */
|
|
@@ -32,14 +32,20 @@ export const unwrap = (source, ...children) => {
|
|
|
32
32
|
? [...source()?.children || source()]
|
|
33
33
|
: typeof HTMLElement !== "undefined" && source instanceof HTMLElement
|
|
34
34
|
? [...source.children]
|
|
35
|
-
: Array.isArray(source)
|
|
35
|
+
: /* istanbul ignore next */ Array.isArray(source)
|
|
36
36
|
? source
|
|
37
37
|
: [source];
|
|
38
38
|
|
|
39
|
-
return van.tags.fragment(
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
);
|
|
39
|
+
// return van.tags.fragment(
|
|
40
|
+
// ...(children || /* istanbul ignore next */ []),
|
|
41
|
+
// ...pageChildren,
|
|
42
|
+
// );
|
|
43
|
+
return {
|
|
44
|
+
children: [
|
|
45
|
+
...(children || /* istanbul ignore next */ []),
|
|
46
|
+
...pageChildren,
|
|
47
|
+
],
|
|
48
|
+
};
|
|
43
49
|
};
|
|
44
50
|
return layout();
|
|
45
51
|
};
|
|
@@ -138,6 +144,19 @@ export const extractParams = (pattern, path) => {
|
|
|
138
144
|
return params;
|
|
139
145
|
};
|
|
140
146
|
|
|
147
|
+
/**
|
|
148
|
+
* Fix the URL of a route
|
|
149
|
+
* @param {string=} url
|
|
150
|
+
* @returns
|
|
151
|
+
*/
|
|
152
|
+
export const fixRouteUrl = (url) => {
|
|
153
|
+
if (!url) return "/";
|
|
154
|
+
if (url.startsWith("/")) {
|
|
155
|
+
return url;
|
|
156
|
+
}
|
|
157
|
+
return `/${url}`;
|
|
158
|
+
};
|
|
159
|
+
|
|
141
160
|
/**
|
|
142
161
|
* Client only reload utility
|
|
143
162
|
* WORK IN PROGRESS
|
package/router/index.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
export * from "./router";
|
|
2
|
-
export * from "./routes";
|
|
3
|
-
export * from "./a";
|
|
4
|
-
export * from "./state";
|
|
5
|
-
export * from "./lazy";
|
|
6
|
-
export * from "./helpers";
|
|
1
|
+
export * from "./router.mjs";
|
|
2
|
+
export * from "./routes.mjs";
|
|
3
|
+
export * from "./a.mjs";
|
|
4
|
+
export * from "./state.mjs";
|
|
5
|
+
export * from "./lazy.mjs";
|
|
6
|
+
export * from "./helpers.mjs";
|
|
7
|
+
export * from "./cache.mjs";
|
package/router/lazy.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import setup from "
|
|
1
|
+
import setup from "../setup/index.mjs";
|
|
2
2
|
import van from "vanjs-core";
|
|
3
|
-
import { cache, getCached } from "./cache";
|
|
3
|
+
import { cache, getCached } from "./cache.mjs";
|
|
4
4
|
|
|
5
5
|
/** @typedef {import('./types').VanNode} VanNode */
|
|
6
6
|
/** @typedef {import('./types').ComponentModule} ComponentModule */
|
package/router/package.json
CHANGED
|
File without changes
|
package/router/router.mjs
CHANGED
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
import van from "vanjs-core";
|
|
2
|
-
import setup from "
|
|
3
|
-
import { routerState } from "./state";
|
|
4
|
-
import { matchRoute } from "./routes";
|
|
5
|
-
import { executeLifecycle, unwrap } from "./helpers";
|
|
6
|
-
|
|
7
|
-
export const Router = () => {
|
|
8
|
-
const { div } = van.tags;
|
|
9
|
-
// const meta = defaultMeta();
|
|
2
|
+
import setup from "../setup/index.mjs";
|
|
3
|
+
import { routerState } from "./state.mjs";
|
|
4
|
+
import { matchRoute } from "./routes.mjs";
|
|
5
|
+
import { executeLifecycle, unwrap } from "./helpers.mjs";
|
|
10
6
|
|
|
7
|
+
export const Router = (initialProps = /* istanbul ignore next */ {}) => {
|
|
8
|
+
const { div, main } = van.tags;
|
|
9
|
+
/* istanbul ignore next - try again later */
|
|
10
|
+
const props = Object.fromEntries(
|
|
11
|
+
Object.entries(initialProps).filter(([_, val]) => val),
|
|
12
|
+
);
|
|
13
|
+
const wrapper = main(props);
|
|
11
14
|
const mainLayout = () => {
|
|
12
15
|
const route = matchRoute(routerState.pathname.val);
|
|
13
16
|
/* istanbul ignore else */
|
|
14
|
-
if (!route) return div("404 - Not Found");
|
|
17
|
+
if (!route) return van.add(wrapper, div("404 - Not Found"));
|
|
15
18
|
|
|
16
19
|
routerState.params.val = route.params || {};
|
|
17
20
|
// Server-side or async component: use renderComponent
|
|
@@ -20,23 +23,27 @@ export const Router = () => {
|
|
|
20
23
|
try {
|
|
21
24
|
const module = await route.component();
|
|
22
25
|
const component = module.component();
|
|
26
|
+
|
|
23
27
|
await executeLifecycle(module, route.params);
|
|
24
|
-
return unwrap(component).children;
|
|
28
|
+
return van.add(wrapper, unwrap(component).children);
|
|
25
29
|
} catch (error) {
|
|
26
30
|
/* istanbul ignore next */
|
|
27
31
|
console.error("Router error:", error);
|
|
28
32
|
/* istanbul ignore next */
|
|
29
|
-
return div("Error loading page");
|
|
33
|
+
return van.add(wrapper, div("Error loading page"));
|
|
30
34
|
}
|
|
31
35
|
};
|
|
32
36
|
|
|
33
37
|
return renderComponent();
|
|
34
38
|
}
|
|
35
39
|
|
|
36
|
-
const module = route.component();
|
|
37
40
|
// Client-side lazy component, lifeCycle is already executed on the server
|
|
38
41
|
// or when A component has been clicked in the client
|
|
39
|
-
|
|
42
|
+
const module = route.component();
|
|
43
|
+
const children = module.component
|
|
44
|
+
? Array.from(unwrap(module.component).children)
|
|
45
|
+
: [];
|
|
46
|
+
return children.length ? van.add(wrapper, ...children) : wrapper;
|
|
40
47
|
};
|
|
41
48
|
|
|
42
49
|
return mainLayout();
|
package/router/routes.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// router/routes.mjs
|
|
2
|
-
import { extractParams, isLazyComponent } from "./helpers";
|
|
3
|
-
import { lazy } from "./lazy";
|
|
2
|
+
import { extractParams, isLazyComponent } from "./helpers.mjs";
|
|
3
|
+
import { lazy } from "./lazy.mjs";
|
|
4
4
|
|
|
5
5
|
/** @typedef {import("./types.d.ts").RouteEntry} RouteEntry */
|
|
6
6
|
/** @typedef {import("./types.d.ts").RouteProps} RouteProps */
|
package/router/state.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// router/state.js
|
|
2
2
|
import van from "vanjs-core";
|
|
3
3
|
import setup from "../setup/index.mjs";
|
|
4
|
+
import { fixRouteUrl } from "./helpers.mjs";
|
|
4
5
|
|
|
5
6
|
const initialPath = !setup.isServer ? globalThis.location.pathname : "/";
|
|
6
7
|
const initialSearch = !setup.isServer ? globalThis.location.search : "";
|
|
@@ -18,7 +19,7 @@ export const routerState = {
|
|
|
18
19
|
* @type {typeof import("./types.d.ts").setRouterState}
|
|
19
20
|
*/
|
|
20
21
|
export const setRouterState = (path, search = undefined, params) => {
|
|
21
|
-
const [pathname, searchParams] = path.split("?");
|
|
22
|
+
const [pathname, searchParams] = fixRouteUrl(path).split("?");
|
|
22
23
|
routerState.pathname.val = pathname;
|
|
23
24
|
routerState.searchParams.val = new URLSearchParams(
|
|
24
25
|
search || searchParams || "",
|
package/router/types.d.ts
CHANGED
|
@@ -1,10 +1,54 @@
|
|
|
1
1
|
/// <reference path="global.d.ts" />
|
|
2
|
-
import type {
|
|
2
|
+
import type {
|
|
3
|
+
Element as VElement,
|
|
4
|
+
TagFunc as IsoTagFunc,
|
|
5
|
+
} from "mini-van-plate/van-plate";
|
|
3
6
|
import van from "vanjs-core";
|
|
4
|
-
import type {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
import type {
|
|
8
|
+
ChildDom,
|
|
9
|
+
Primitive,
|
|
10
|
+
Props,
|
|
11
|
+
PropsWithKnownKeys,
|
|
12
|
+
PropValueOrDerived,
|
|
13
|
+
State,
|
|
14
|
+
} from "vanjs-core";
|
|
15
|
+
|
|
16
|
+
// type VanElement = VElement | Exclude<Primitive, boolean | undefined>;
|
|
17
|
+
type DOMElement = globalThis.Element;
|
|
18
|
+
type PrimitiveChild = Primitive | State<Primitive | undefined | null>;
|
|
19
|
+
|
|
20
|
+
type VanElement =
|
|
21
|
+
| SVGElement
|
|
22
|
+
| HTMLElement
|
|
23
|
+
| DOMElement
|
|
24
|
+
| Node
|
|
25
|
+
| VElement
|
|
26
|
+
| PrimitiveChild;
|
|
27
|
+
export type VanNode =
|
|
28
|
+
| VanElement
|
|
29
|
+
| VanNode[]
|
|
30
|
+
| (() => VanNode)
|
|
31
|
+
| null
|
|
32
|
+
| undefined;
|
|
33
|
+
|
|
34
|
+
type ComponentProps1<T> =
|
|
35
|
+
& Props
|
|
36
|
+
& PropsWithKnownKeys<T>;
|
|
37
|
+
|
|
38
|
+
type ComponentProps<K extends keyof HTMLElementTagNameMap> =
|
|
39
|
+
& Props
|
|
40
|
+
& PropsWithKnownKeys<HTMLElementTagNameMap[K]>;
|
|
41
|
+
|
|
42
|
+
export type VanComponent<
|
|
43
|
+
K extends keyof HTMLElementTagNameMap = "div",
|
|
44
|
+
O extends (Record<string, PropValueOrDerived> | undefined) = undefined,
|
|
45
|
+
> = {
|
|
46
|
+
(
|
|
47
|
+
props?: O extends object ? ComponentProps<K> & Partial<O>
|
|
48
|
+
: ComponentProps<K>,
|
|
49
|
+
...children: ChildDom[]
|
|
50
|
+
): HTMLElementTagNameMap[K];
|
|
51
|
+
};
|
|
8
52
|
|
|
9
53
|
// router.mjs
|
|
10
54
|
/**
|
|
@@ -18,7 +62,14 @@ type VanNode = SVGElement | HTMLElement | VanElement;
|
|
|
18
62
|
* return Router(); // or <Router /> for JSX
|
|
19
63
|
* }
|
|
20
64
|
*/
|
|
21
|
-
export const Router:
|
|
65
|
+
// export const Router: JSX.Component<HTMLElement> & VanComponent<HTMLElement>;
|
|
66
|
+
// export function Router(props?: ComponentProps<HTMLElement>): HTMLElement;
|
|
67
|
+
// export function Router(
|
|
68
|
+
// props?: JSX.IntrinsicElements["main"] & { children: null },
|
|
69
|
+
// ): HTMLElement;
|
|
70
|
+
export const Router:
|
|
71
|
+
& VanComponent<"main">
|
|
72
|
+
& JSX.Component<"main">;
|
|
22
73
|
|
|
23
74
|
// a.mjs
|
|
24
75
|
/**
|
|
@@ -38,10 +89,9 @@ export const Router: () => VanNode | VanNode[];
|
|
|
38
89
|
* );
|
|
39
90
|
* }
|
|
40
91
|
*/
|
|
41
|
-
export const A:
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
) => HTMLAnchorElement;
|
|
92
|
+
export const A:
|
|
93
|
+
& VanComponent<"a">
|
|
94
|
+
& JSX.Component<"a">;
|
|
45
95
|
|
|
46
96
|
// helpers.mjs
|
|
47
97
|
/**
|
|
@@ -51,7 +101,10 @@ export const A: (
|
|
|
51
101
|
* @param href the URL to navigate to
|
|
52
102
|
* @param options when true, will replace the current history entry
|
|
53
103
|
*/
|
|
54
|
-
export const navigate: (
|
|
104
|
+
export const navigate: (
|
|
105
|
+
href: string,
|
|
106
|
+
options?: { replace?: boolean },
|
|
107
|
+
) => void;
|
|
55
108
|
|
|
56
109
|
/**
|
|
57
110
|
* A client only helper function that reloads the current page.
|
|
@@ -66,8 +119,6 @@ export const reload: () => void;
|
|
|
66
119
|
*/
|
|
67
120
|
export const redirect: (href?: string) => void | (() => void);
|
|
68
121
|
|
|
69
|
-
export type VanComponent = () => VanNode | VanNode[];
|
|
70
|
-
|
|
71
122
|
// routes.mjs
|
|
72
123
|
export type RouteEntry = {
|
|
73
124
|
path: string;
|
|
@@ -103,7 +154,7 @@ export const Route: (route: RouteProps) => void;
|
|
|
103
154
|
export type RouterState = {
|
|
104
155
|
pathname: string;
|
|
105
156
|
searchParams: URLSearchParams;
|
|
106
|
-
params
|
|
157
|
+
params: Record<string, string>;
|
|
107
158
|
};
|
|
108
159
|
/**
|
|
109
160
|
* A reactive object that holds the current router state.
|
|
@@ -131,26 +182,39 @@ export const setRouterState: (
|
|
|
131
182
|
* Merge the children of an Element or an array of elements with an optional array of children
|
|
132
183
|
* into the childen of a single HTMLFragmentElement element.
|
|
133
184
|
* @param source
|
|
134
|
-
* @param children
|
|
185
|
+
* @param {...VanNode[]} children
|
|
135
186
|
*/
|
|
136
187
|
export const unwrap: (
|
|
137
|
-
source:
|
|
188
|
+
source:
|
|
189
|
+
| VanNode
|
|
190
|
+
| IsoTagFunc
|
|
191
|
+
| VanNode[]
|
|
192
|
+
| IsoTagFunc[]
|
|
193
|
+
| (() => IsoTagFunc | IsoTagFunc[] | VanNode | VanNode[]),
|
|
138
194
|
...children: VanNode[]
|
|
139
195
|
) => VanNode;
|
|
140
196
|
|
|
141
197
|
export type ComponentModule = {
|
|
142
|
-
component: VanComponent;
|
|
198
|
+
component: VanComponent | JSX.Element;
|
|
143
199
|
route: Pick<RouteEntry, "load" | "preload">;
|
|
144
200
|
};
|
|
145
201
|
|
|
146
202
|
export type DynamicModule = {
|
|
147
|
-
Page: VanComponent;
|
|
148
|
-
default?: VanComponent;
|
|
203
|
+
Page: VanComponent | JSX.Element;
|
|
204
|
+
default?: VanComponent | JSX.Element;
|
|
149
205
|
route?: Pick<RouteEntry, "load" | "preload">;
|
|
150
206
|
};
|
|
151
207
|
|
|
152
|
-
export type LazyComponent =
|
|
153
|
-
| Promise<DynamicModule>
|
|
154
|
-
| (() => Promise<DynamicModule>);
|
|
208
|
+
export type LazyComponent = Promise<DynamicModule>;
|
|
155
209
|
|
|
210
|
+
/**
|
|
211
|
+
* Registers a lazy component.
|
|
212
|
+
* @param importFn
|
|
213
|
+
*/
|
|
156
214
|
export const lazy: (importFn: () => LazyComponent) => () => ComponentModule;
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Fixes the URL of a route.
|
|
218
|
+
* @param url
|
|
219
|
+
*/
|
|
220
|
+
export const fixRouteUrl: (url: string) => string;
|