vite-plugin-vanjs 0.1.15 → 0.1.16

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-vanjs",
3
- "version": "0.1.15",
3
+ "version": "0.1.16",
4
4
  "author": "thednp",
5
5
  "license": "MIT",
6
6
  "description": "A mini meta-framework for VanJS powered by Vite",
@@ -185,10 +185,15 @@ declare module "@vanjs/router" {
185
185
  * A reactive object that holds the current router state.
186
186
  * This state is maintained by both server and client.
187
187
  */
188
+ // export const routerState: {
189
+ // pathname: State<RouterState.pathname>;
190
+ // searchParams: State<RouterState.searchParams>;
191
+ // params?: State<RouterState.params>;
192
+ // };
188
193
  export const routerState: {
189
- pathname: van.state<RouterState.pathname>;
190
- searchParams: van.state<RouterState.searchParams>;
191
- params?: van.state<RouterState.params>;
194
+ pathname: RouterState["pathname"];
195
+ searchParams: RouterState["searchParams"];
196
+ params?: RouterState["params"];
192
197
  };
193
198
 
194
199
  /**
@@ -16,7 +16,7 @@ import { matchRoute } from "./matchRoute.mjs";
16
16
  * @returns {boolean}
17
17
  */
18
18
  export const isCurrentPage = (pageName) => {
19
- return routerState.pathname.val === pageName;
19
+ return routerState.pathname === pageName;
20
20
  };
21
21
 
22
22
  /**
package/router/router.mjs CHANGED
@@ -52,7 +52,7 @@ const initClient = () => {
52
52
  /** @param {Event & {target: globalThis}} e */
53
53
  (e) => {
54
54
  const location = e.target.location;
55
- const oldPath = routerState.pathname._oldVal;
55
+ const oldPath = routerState.pathname;
56
56
  // istanbul ignore next - cannot test
57
57
  if (location.pathname !== oldPath) {
58
58
  setRouterState(location.pathname, location.search);
@@ -73,11 +73,11 @@ export const Router = (initialProps = /* istanbul ignore next */ {}) => {
73
73
  // Initialize Head BEFORE any route matching or lifecycle execution
74
74
  if (!isServer) initClient();
75
75
 
76
- const route = matchRoute(routerState.pathname.val);
76
+ const route = matchRoute(routerState.pathname);
77
77
  /* istanbul ignore else */
78
78
  if (!route) return van.add(wrapper, div("No Route Found"));
79
79
 
80
- routerState.params.val = route.params || {};
80
+ Object.assign(routerState.params, route.params || {});
81
81
 
82
82
  // Server-side rendering
83
83
  if (isServer) {
@@ -108,7 +108,7 @@ export const Router = (initialProps = /* istanbul ignore next */ {}) => {
108
108
 
109
109
  // SPA path - reactive routing
110
110
  van.derive(() => {
111
- const r = matchRoute(routerState.pathname.val);
111
+ const r = matchRoute(routerState.pathname);
112
112
  if (!r) {
113
113
  wrapper.replaceChildren(div("No Route Found"));
114
114
  return;
package/router/state.mjs CHANGED
@@ -18,23 +18,86 @@ export const fixRouteUrl = (url) => {
18
18
  const initialPath = !isServer ? globalThis.location.pathname : "/";
19
19
  const initialSearch = !isServer ? globalThis.location.search : "";
20
20
 
21
+ const STATE_PROXY = "_proxy";
22
+ const proxyProps = {
23
+ value: 1,
24
+ enumerable: false,
25
+ configurable: false,
26
+ writable: false,
27
+ };
28
+
21
29
  /**
22
- * @type {typeof import("./types.d.ts").routerState}
30
+ * @param {number | Omit<keyof T, "symbol">} key
31
+ * @param {T[keyof T]} value
32
+ * @param {Record<string, string | number>} target
33
+ * @returns {T}
23
34
  */
24
- export const routerState = {
25
- pathname: van.state(initialPath),
26
- searchParams: van.state(new URLSearchParams(initialSearch)),
27
- params: van.state({}),
35
+ const defineProxy = (key, value, target) => {
36
+ const stateObj = van.state(value);
37
+
38
+ const getter = () => stateObj.val;
39
+ const setter = (newVal) => {
40
+ stateObj.val = newVal;
41
+ };
42
+ stateObj.val = value;
43
+
44
+ Object.defineProperties(target, {
45
+ [STATE_PROXY]: proxyProps,
46
+ [key]: {
47
+ get: getter,
48
+ set: setter,
49
+ enumerable: true,
50
+ },
51
+ });
52
+
53
+ return stateObj;
28
54
  };
29
55
 
56
+ /**
57
+ * @param {T extends Record<string, unknown>} init
58
+ * @returns {T}
59
+ */
60
+ export function miniStore(init) {
61
+ const target = {};
62
+ for (const [prop, value] of Object.entries(init)) {
63
+ if (
64
+ value && typeof value === "object" &&
65
+ Object.getPrototypeOf(value) === Object
66
+ ) {
67
+ for (const [sp, sv] of Object.entries(value)) {
68
+ target[prop] = defineProxy(sp, sv, {});
69
+ }
70
+ } else if (!Array.isArray(value)) {
71
+ defineProxy(prop, value, target);
72
+ } else {
73
+ console.warn(typeof value + " is not supported.");
74
+ }
75
+ }
76
+ return target;
77
+ }
78
+
79
+ /**
80
+ * @type {typeof import("./types.d.ts").routerState}
81
+ */
82
+ // export const routerState = {
83
+ // pathname: van.state(initialPath),
84
+ // searchParams: van.state(new URLSearchParams(initialSearch)),
85
+ // params: van.state({}),
86
+ // };
87
+ export const routerState = miniStore({
88
+ pathname: initialPath,
89
+ searchParams: new URLSearchParams(initialSearch),
90
+ params: {},
91
+ });
92
+
30
93
  /**
31
94
  * @type {typeof import("./types.d.ts").setRouterState}
32
95
  */
33
96
  export const setRouterState = (path, search = undefined, params) => {
34
97
  const [pathname, searchParams] = fixRouteUrl(path).split("?");
35
- routerState.pathname.val = pathname;
36
- routerState.searchParams.val = new URLSearchParams(
98
+ routerState.pathname = pathname;
99
+ routerState.searchParams = new URLSearchParams(
37
100
  search || searchParams || "",
38
101
  );
39
- routerState.params.val = params || {};
102
+ Object.assign(routerState.params, params || {});
40
103
  };
package/router/types.d.ts CHANGED
@@ -4,7 +4,7 @@ import type {
4
4
  Element as VElement,
5
5
  TagFunc as IsoTagFunc,
6
6
  } from "mini-van-plate/van-plate";
7
- import van from "vanjs-core";
7
+ // import van from "vanjs-core";
8
8
  import type {
9
9
  ChildDom,
10
10
  Primitive,
@@ -203,9 +203,9 @@ export type RouterState = {
203
203
  * This state is maintained by both server and client.
204
204
  */
205
205
  export const routerState: {
206
- pathname: van.state<RouterState.pathname>;
207
- searchParams: van.state<RouterState.searchParams>;
208
- params?: van.state<RouterState.params>;
206
+ pathname: RouterState["pathname"];
207
+ searchParams: RouterState["searchParams"];
208
+ params?: RouterState["params"];
209
209
  };
210
210
 
211
211
  /**