@thyn/core 0.0.346 → 0.0.348

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.
@@ -1,5 +1,5 @@
1
1
  export declare const router: {
2
- path: import("./signals.js").Signal<string>;
2
+ readonly path: any;
3
3
  param: (name: string) => string | undefined;
4
4
  };
5
5
  interface Route {
@@ -1,11 +1,27 @@
1
1
  import { component, show } from "./element.js";
2
2
  import { $signal, staticEffect } from "./signals.js";
3
- const params = $signal({});
3
+ let params = null;
4
+ let routerPath = null;
5
+ let initialized = false;
6
+ function initRouter() {
7
+ if (initialized)
8
+ return;
9
+ params = $signal({});
10
+ routerPath = $signal(location.pathname);
11
+ initialized = true;
12
+ }
4
13
  export const router = {
5
- path: $signal(location.pathname),
6
- param: (name) => params()[name],
14
+ get path() {
15
+ initRouter();
16
+ return routerPath;
17
+ },
18
+ param: (name) => {
19
+ initRouter();
20
+ return params()[name];
21
+ },
7
22
  };
8
23
  export function Router({ routes }) {
24
+ initRouter();
9
25
  const current = $signal(null);
10
26
  const compiledRoutes = routes.map(route => {
11
27
  const compiledRoute = {
@@ -21,7 +37,7 @@ export function Router({ routes }) {
21
37
  return compiledRoute;
22
38
  });
23
39
  staticEffect(() => {
24
- const pn = router.path();
40
+ const pn = routerPath();
25
41
  if (pn !== location.pathname) {
26
42
  history.pushState({}, "", pn);
27
43
  }
@@ -48,6 +64,7 @@ export function Router({ routes }) {
48
64
  })));
49
65
  }
50
66
  export function Link({ slot, to }) {
67
+ initRouter();
51
68
  const a = document.createElement("a");
52
69
  a.href = to;
53
70
  for (const ch of slot) {
@@ -59,7 +76,7 @@ export function Link({ slot, to }) {
59
76
  !(e.metaKey || e.ctrlKey || e.shiftKey || e.altKey)) {
60
77
  e.preventDefault();
61
78
  history.pushState({}, "", to);
62
- router.path(to);
79
+ routerPath(to);
63
80
  }
64
81
  };
65
82
  return a;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,3 @@
1
1
  export * from "./core/signals.js";
2
2
  export * from "./core/element.js";
3
3
  export * from "./core/router.js";
4
- export { default } from "./plugin/index.js";
5
- export { transformSFC, compileSFC } from "./plugin/index.js";
package/dist/index.js CHANGED
@@ -1,5 +1,3 @@
1
1
  export * from "./core/signals.js";
2
2
  export * from "./core/element.js";
3
3
  export * from "./core/router.js";
4
- export { default } from "./plugin/index.js";
5
- export { transformSFC, compileSFC } from "./plugin/index.js";
package/dist/node.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ export * from "./core/signals.js";
2
+ export * from "./core/element.js";
3
+ export * from "./core/router.js";
4
+ export { default } from "./plugin/index.js";
5
+ export { transformSFC, compileSFC } from "./plugin/index.js";
package/dist/node.js ADDED
@@ -0,0 +1,5 @@
1
+ export * from "./core/signals.js";
2
+ export * from "./core/element.js";
3
+ export * from "./core/router.js";
4
+ export { default } from "./plugin/index.js";
5
+ export { transformSFC, compileSFC } from "./plugin/index.js";
package/package.json CHANGED
@@ -1,11 +1,14 @@
1
1
  {
2
2
  "name": "@thyn/core",
3
- "version": "0.0.346",
3
+ "version": "0.0.348",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "exports": {
7
- ".": "./dist/index.js",
8
- "./router": "./dist/router.js"
7
+ ".": {
8
+ "node": "./dist/node.js",
9
+ "default": "./dist/index.js"
10
+ },
11
+ "./plugin": "./dist/plugin/index.js"
9
12
  },
10
13
  "sideEffects": false,
11
14
  "scripts": {
@@ -1,20 +1,35 @@
1
1
  import { component, show } from "./element.js";
2
2
  import { $signal, staticEffect } from "./signals.js";
3
3
 
4
- const params = $signal({} as any);
4
+ let params = null;
5
+ let routerPath = null;
6
+ let initialized = false;
7
+
8
+ function initRouter() {
9
+ if (initialized) return;
10
+ params = $signal({} as any);
11
+ routerPath = $signal(location.pathname);
12
+ initialized = true;
13
+ }
5
14
 
6
15
  export const router = {
7
- path: $signal(location.pathname),
8
- param: (name: string): string | undefined => params()[name],
16
+ get path() {
17
+ initRouter();
18
+ return routerPath;
19
+ },
20
+ param: (name: string): string | undefined => {
21
+ initRouter();
22
+ return params()[name];
23
+ },
9
24
  };
10
25
 
11
-
12
26
  interface Route {
13
27
  path: string;
14
28
  component: () => Node;
15
29
  }
16
30
 
17
31
  export function Router({ routes }: { routes: Route[] }) {
32
+ initRouter();
18
33
  const current = $signal(null);
19
34
  const compiledRoutes = routes.map(route => {
20
35
  const compiledRoute = {
@@ -31,7 +46,7 @@ export function Router({ routes }: { routes: Route[] }) {
31
46
  });
32
47
 
33
48
  staticEffect(() => {
34
- const pn = router.path();
49
+ const pn = routerPath();
35
50
  if (pn !== location.pathname) {
36
51
  history.pushState({}, "", pn);
37
52
  }
@@ -61,6 +76,7 @@ export function Router({ routes }: { routes: Route[] }) {
61
76
  }
62
77
 
63
78
  export function Link({ slot, to }) {
79
+ initRouter();
64
80
  const a = document.createElement("a");
65
81
  a.href = to;
66
82
  for (const ch of slot) {
@@ -74,7 +90,7 @@ export function Link({ slot, to }) {
74
90
  ) {
75
91
  e.preventDefault();
76
92
  history.pushState({}, "", to);
77
- router.path(to);
93
+ routerPath(to);
78
94
  }
79
95
  };
80
96
  return a;
package/src/index.ts CHANGED
@@ -1,5 +1,3 @@
1
1
  export * from "./core/signals.js";
2
2
  export * from "./core/element.js";
3
3
  export * from "./core/router.js";
4
- export { default } from "./plugin/index.js";
5
- export { transformSFC, compileSFC } from "./plugin/index.js";
package/src/node.ts ADDED
@@ -0,0 +1,5 @@
1
+ export * from "./core/signals.js";
2
+ export * from "./core/element.js";
3
+ export * from "./core/router.js";
4
+ export { default } from "./plugin/index.js";
5
+ export { transformSFC, compileSFC } from "./plugin/index.js";