@revojs/vue 0.1.49 → 0.1.51

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,26 +1,35 @@
1
+ import { Component, MaybeRefOrGetter, Ref, ShallowRef } from "vue";
1
2
  import * as _$revojs from "revojs";
2
- import { Scope } from "revojs";
3
- import { MaybeRefOrGetter, Ref } from "vue";
3
+ import { RouterContext, Scope } from "revojs";
4
4
 
5
5
  //#region src/client/index.d.ts
6
6
  interface AsyncOptions<T> {
7
7
  catch?: (error: T) => void | Promise<void>;
8
8
  }
9
9
  declare function useScope(): Scope;
10
- declare function useState<T>(scope: Scope, name: MaybeRefOrGetter<string>): Ref<T | undefined>;
11
- declare function useState<T>(scope: Scope, name: MaybeRefOrGetter<string>, value: T): Ref<T>;
12
- declare function useAsync<T, TError = Error>(scope: Scope, name: MaybeRefOrGetter<string>, invoke: () => Promise<T>, defaultOptions?: AsyncOptions<TError>): Promise<{
10
+ declare function useError(scope?: Scope): ShallowRef<Error>;
11
+ declare function useRouter(): {
12
+ context: ShallowRef<RouterContext<Component>>;
13
+ navigate: (path: string) => void;
14
+ anchorNavigate: (event: Event) => void;
15
+ };
16
+ declare function useState<T>(name: MaybeRefOrGetter<string>): Ref<T | undefined>;
17
+ declare function useState<T>(name: MaybeRefOrGetter<string>, value: T): Ref<T>;
18
+ declare function useAsync<T, TError = Error>(name: MaybeRefOrGetter<string>, invoke: () => Promise<T>, defaultOptions?: AsyncOptions<TError>): Promise<{
13
19
  state: Ref<T | undefined, T | undefined>;
14
20
  isLoading: Ref<boolean, boolean>;
15
21
  refresh: (options?: AsyncOptions<TError>) => Promise<T | undefined>;
16
22
  }>;
17
- declare function useFetch<T, TError = Error>(scope: Scope, input: MaybeRefOrGetter<string>, options?: RequestInit & AsyncOptions<TError>): Promise<{
23
+ declare function useFetch<T, TError = Error>(input: MaybeRefOrGetter<string>, options?: RequestInit & AsyncOptions<TError>): Promise<{
18
24
  state: Ref<T | undefined, T | undefined>;
19
25
  isLoading: Ref<boolean, boolean>;
20
26
  refresh: (options?: AsyncOptions<TError> | undefined) => Promise<T | undefined>;
21
27
  }>;
22
- declare function refreshAsync(scope: Scope, input?: string | Array<string>): Promise<void[] | undefined>;
28
+ declare function refreshAsync(input?: string | Array<string>): Promise<void[] | undefined>;
23
29
  declare const isServerSideRendering: any;
24
30
  declare const REFRESH_ASYNC_HOOK: _$revojs.Descriptor<(names?: Array<string>) => Promise<void>>;
31
+ declare const NAVIGATE_HOOK: _$revojs.Descriptor<_$revojs.Invoke>;
32
+ declare const CLIENT_ERROR_CONTEXT: _$revojs.Descriptor<ShallowRef<Error>>;
33
+ declare const CLIENT_ROUTER_CONTEXT: _$revojs.Descriptor<ShallowRef<RouterContext<Component>>>;
25
34
  //#endregion
26
- export { AsyncOptions, REFRESH_ASYNC_HOOK, isServerSideRendering, refreshAsync, useAsync, useFetch, useScope, useState };
35
+ export { AsyncOptions, CLIENT_ERROR_CONTEXT, CLIENT_ROUTER_CONTEXT, NAVIGATE_HOOK, REFRESH_ASYNC_HOOK, isServerSideRendering, refreshAsync, useAsync, useError, useFetch, useRouter, useScope, useState };
@@ -1,12 +1,36 @@
1
- import { $fetch, defineHook, getState, isClient, isServer, setState } from "revojs";
2
1
  import { customRef, inject, isRef, onScopeDispose, ref, toValue, watch } from "vue";
2
+ import { defineContext, defineHook, fetch, getState, isClient, isServer, sendRedirect, setState } from "revojs";
3
3
  //#region src/client/index.ts
4
4
  function useScope() {
5
5
  const scope = inject("REVOJS_SCOPE");
6
6
  if (scope) return scope;
7
7
  throw new Error();
8
8
  }
9
- function useState(scope, name, value) {
9
+ function useError(scope = useScope()) {
10
+ return scope.getContext(CLIENT_ERROR_CONTEXT);
11
+ }
12
+ function useRouter() {
13
+ const scope = useScope();
14
+ const context = scope.getContext(CLIENT_ROUTER_CONTEXT);
15
+ const navigate = (path) => {
16
+ if (isClient) {
17
+ if (window.location.pathname != path) window.history.pushState(window.history.state, "", path);
18
+ scope.dispatchHook(NAVIGATE_HOOK);
19
+ }
20
+ if (isServer) throw sendRedirect(scope, path);
21
+ };
22
+ const anchorNavigate = (event) => {
23
+ event.preventDefault();
24
+ navigate(event.currentTarget?.getAttribute("href") ?? "/");
25
+ };
26
+ return {
27
+ context,
28
+ navigate,
29
+ anchorNavigate
30
+ };
31
+ }
32
+ function useState(name, value) {
33
+ const scope = useScope();
10
34
  return customRef((track, trigger) => ({
11
35
  get() {
12
36
  track();
@@ -18,8 +42,9 @@ function useState(scope, name, value) {
18
42
  }
19
43
  }));
20
44
  }
21
- async function useAsync(scope, name, invoke, defaultOptions) {
22
- const state = useState(scope, name);
45
+ async function useAsync(name, invoke, defaultOptions) {
46
+ const scope = useScope();
47
+ const state = useState(name);
23
48
  const isLoading = ref(false);
24
49
  const refresh = async (options) => {
25
50
  const onCatch = options?.catch ?? defaultOptions?.catch;
@@ -45,8 +70,9 @@ async function useAsync(scope, name, invoke, defaultOptions) {
45
70
  refresh
46
71
  };
47
72
  }
48
- async function useFetch(scope, input, options) {
49
- const { state, isLoading, refresh } = await useAsync(scope, input, () => $fetch(scope, toValue(input), options), options);
73
+ async function useFetch(input, options) {
74
+ const scope = useScope();
75
+ const { state, isLoading, refresh } = await useAsync(input, () => fetch(scope, toValue(input), options), options);
50
76
  if (isClient && (isRef(input) || typeof input === "function")) watch(input, async (next, previous) => {
51
77
  if (next !== previous) await refresh();
52
78
  });
@@ -56,10 +82,14 @@ async function useFetch(scope, input, options) {
56
82
  refresh
57
83
  };
58
84
  }
59
- async function refreshAsync(scope, input) {
85
+ async function refreshAsync(input) {
86
+ const scope = useScope();
60
87
  if (isClient) return await scope.dispatchHook(REFRESH_ASYNC_HOOK, input === void 0 ? void 0 : Array.isArray(input) ? input : [input]);
61
88
  }
62
89
  const isServerSideRendering = import.meta.SERVER_SIDE_RENDERING ?? globalThis?.import?.meta?.SERVER_SIDE_RENDERING;
63
90
  const REFRESH_ASYNC_HOOK = defineHook("REFRESH_ASYNC_HOOK");
91
+ const NAVIGATE_HOOK = defineHook("NAVIGATE_HOOK");
92
+ const CLIENT_ERROR_CONTEXT = defineContext("CLIENT_ERROR_CONTEXT");
93
+ const CLIENT_ROUTER_CONTEXT = defineContext("CLIENT_ROUTER_CONTEXT");
64
94
  //#endregion
65
- export { REFRESH_ASYNC_HOOK, isServerSideRendering, refreshAsync, useAsync, useFetch, useScope, useState };
95
+ export { CLIENT_ERROR_CONTEXT, CLIENT_ROUTER_CONTEXT, NAVIGATE_HOOK, REFRESH_ASYNC_HOOK, isServerSideRendering, refreshAsync, useAsync, useError, useFetch, useRouter, useScope, useState };
package/dist/index.js CHANGED
@@ -29,7 +29,7 @@ function vue(options) {
29
29
  return {
30
30
  variables: { SERVER_SIDE_RENDERING: options?.serverSideRendering ?? true },
31
31
  sources: { pages: {
32
- match: "**/{page,layout}.vue",
32
+ match: ["**/page.vue", "**/+layout.vue"],
33
33
  entries: ["./routes"]
34
34
  } },
35
35
  vite: {
@@ -37,7 +37,8 @@ function vue(options) {
37
37
  resolve: { alias: {
38
38
  "#vue/app": fromModule("./runtime/app.js"),
39
39
  "#vue/main": fromModule("./runtime/main.vue")
40
- } }
40
+ } },
41
+ optimizeDeps: { exclude: ["vue"] }
41
42
  }
42
43
  };
43
44
  } };
@@ -1,5 +1,5 @@
1
- import { Scope } from "revojs";
2
1
  import * as _$vue from "vue";
2
+ import { Scope } from "revojs";
3
3
 
4
4
  //#region src/runtime/app.d.ts
5
5
  declare const _default: (scope: Scope) => Promise<_$vue.App<Element>>;
@@ -1,55 +1,14 @@
1
- import { Radix, isServer, useUrl } from "revojs";
2
- import { createApp, createSSRApp } from "vue";
3
- import pages from "#pages";
4
1
  import Main from "#vue/main";
5
- import { RouterView, createMemoryHistory, createRouter, createWebHistory } from "vue-router";
2
+ import { createApp, createSSRApp, warn } from "vue";
6
3
  import { isServerSideRendering } from "../client";
7
4
  //#region src/runtime/app.ts
8
- function toRoute(path, node, index) {
9
- const layout = node.children["layout"];
10
- if (layout) delete node.children["layout"];
11
- const children = Object.entries(node.children).map(([path, node]) => toRoute(path, node, index + 1));
12
- if (node.value && (children.length || layout)) children.push({
13
- path: "",
14
- component: node.value
15
- });
16
- if (node.type === "WILDCARD") path = ":" + node.parameter + "(.*)*";
17
- if (node.type === "OPTIONAL-WILDCARD") path = ":" + node.parameter + "(.*)?";
18
- if (node.type === "OPTIONAL-PARAMETER") path = ":" + node.parameter + "?";
19
- if (node.type === "PARAMETER") path = ":" + node.parameter;
20
- return {
21
- path,
22
- children,
23
- component: children.length ? layout?.value ?? RouterView : node.value ?? RouterView
24
- };
25
- }
26
5
  var app_default = async (scope) => {
27
- const radix = new Radix();
28
- for (const path in pages) {
29
- const segments = path.split("/");
30
- for (const attribute of segments.pop()?.split(".") ?? []) {
31
- if (attribute === "page" || attribute === "vue") continue;
32
- segments.push(attribute);
33
- }
34
- radix.use(segments, pages[path]);
35
- }
36
- const rootNode = toRoute("/", radix.rootNode, 0);
37
- const optionalNode = rootNode.children?.find((route) => route.path.includes("?"));
38
- if (optionalNode) rootNode.children?.push({
39
- ...optionalNode,
40
- path: ""
41
- });
42
- const router = createRouter({
43
- history: isServer ? createMemoryHistory() : createWebHistory(),
44
- routes: [rootNode]
45
- });
46
- const app = (isServerSideRendering ? createSSRApp(Main) : createApp(Main)).use(router).provide("REVOJS_SCOPE", scope);
6
+ const app = (isServerSideRendering ? createSSRApp(Main) : createApp(Main)).provide("REVOJS_SCOPE", scope);
7
+ app.config.warnHandler = (message, instance, trace) => {
8
+ if (message.includes("Invalid vnode type when creating vnode") || message.includes("Component <Anonymous> is missing template or render function")) return;
9
+ warn(message, instance, trace);
10
+ };
47
11
  app.config.throwUnhandledErrorInProduction = true;
48
- if (isServer) {
49
- const { pathname } = useUrl(scope);
50
- await router.push(pathname);
51
- }
52
- await router.isReady();
53
12
  return app;
54
13
  };
55
14
  //#endregion
@@ -1,5 +1,5 @@
1
- import { Scope } from "revojs";
2
1
  import createApp from "#vue/app";
2
+ import { Scope } from "revojs";
3
3
  //#region src/runtime/entry.ts
4
4
  await createApp(new Scope()).then((app) => app.mount("#app"));
5
5
  //#endregion
@@ -1,9 +1,71 @@
1
1
  <template>
2
2
  <Suspense>
3
- <RouterView />
3
+ <component :is="route" v-if="route" />
4
4
  </Suspense>
5
5
  </template>
6
6
 
7
7
  <script setup lang="ts">
8
- import { RouterView } from "vue-router";
8
+ import pages from "#pages";
9
+ import { isClient, Router, RouterContext, useUrl } from "revojs";
10
+ import { computed, h, onErrorCaptured, onScopeDispose, shallowRef, type Component } from "vue";
11
+ import { CLIENT_ERROR_CONTEXT, CLIENT_ROUTER_CONTEXT, NAVIGATE_HOOK, useScope } from "../client";
12
+
13
+ const scope = useScope();
14
+
15
+ const error = shallowRef<Error>();
16
+ const context = shallowRef<RouterContext<Component>>();
17
+
18
+ const router = new Router<Component>();
19
+
20
+ for (const path in pages) {
21
+ const segments = path.split("/");
22
+
23
+ for (const attribute of segments.pop()?.split(".") ?? []) {
24
+ if (attribute === "page" || attribute === "vue") {
25
+ continue;
26
+ }
27
+
28
+ segments.push(attribute);
29
+ }
30
+
31
+ router.use(segments, pages[path]);
32
+ }
33
+
34
+ const route = computed(() => {
35
+ if (context.value?.route.value) {
36
+ const route = h(context.value.route.value);
37
+
38
+ return context.value.hooks.reduceRight(
39
+ (previous, hook) =>
40
+ h(hook, null, {
41
+ default: () => (error.value === undefined ? previous : undefined),
42
+ }),
43
+ route,
44
+ );
45
+ }
46
+ });
47
+
48
+ const fetch = () => {
49
+ const next = useUrl(scope);
50
+
51
+ context.value = router.match(next.pathname.slice(1).split("/"));
52
+ };
53
+
54
+ scope.registerHook(NAVIGATE_HOOK, fetch);
55
+
56
+ if (isClient) {
57
+ window.addEventListener("popstate", fetch);
58
+ onScopeDispose(() => window.removeEventListener("popstate", fetch));
59
+ }
60
+
61
+ fetch();
62
+
63
+ scope.setContext(CLIENT_ERROR_CONTEXT, error);
64
+ scope.setContext(CLIENT_ROUTER_CONTEXT, context);
65
+
66
+ onErrorCaptured((value) => {
67
+ if (error.value === undefined) {
68
+ error.value = value;
69
+ }
70
+ });
9
71
  </script>
@@ -1,6 +1,6 @@
1
- import { defineRoute, sendOk, useServer } from "revojs";
2
- import { isServerSideRendering } from "../../../client";
1
+ import { isServerSideRendering, useError } from "../../../client";
3
2
  import createApp from "#vue/app";
3
+ import { defineRoute, sendOk, useServer } from "revojs";
4
4
  import client from "#client";
5
5
  import { renderToString } from "vue/server-renderer";
6
6
  //#region src/runtime/routes/[...]/get.ts
@@ -9,10 +9,9 @@ var get_default = defineRoute({ async fetch(scope) {
9
9
  let content = client;
10
10
  if (isServerSideRendering) {
11
11
  const app = await createApp(scope);
12
- let exception;
13
- app.config.errorHandler = (value) => exception = value;
14
12
  content = content.replace("<!-- APP -->", await renderToString(app));
15
- if (exception) throw exception;
13
+ const error = useError(scope);
14
+ if (error.value) throw error.value;
16
15
  }
17
16
  const value = JSON.stringify(states).replace(/</g, "\\u003c").replace(/>/g, "\\u003e").replace(/&/g, "\\u0026").replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029");
18
17
  return sendOk(scope, content.replace(`<!-- STATES -->`, value));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@revojs/vue",
3
- "version": "0.1.49",
3
+ "version": "0.1.51",
4
4
  "license": "MIT",
5
5
  "repository": "tellua/revojs",
6
6
  "files": [
@@ -29,8 +29,7 @@
29
29
  "dependencies": {
30
30
  "@vitejs/plugin-vue": "^6.0.5",
31
31
  "revojs": "*",
32
- "vue": "^3.5.31",
33
- "vue-router": "^5.0.4"
32
+ "vue": "^3.5.32"
34
33
  },
35
34
  "devDependencies": {
36
35
  "@revojs/tsconfig": "*"
@@ -1,25 +0,0 @@
1
- declare module "#vue/app" {
2
- import type { Scope } from "revojs";
3
- import type { App } from "vue";
4
-
5
- export default function createApp(scope: Scope): Promise<App>;
6
- }
7
-
8
- declare module "#vue/main" {
9
- import type { Component } from "vue";
10
-
11
- const main: Component;
12
-
13
- export default app;
14
- }
15
-
16
- declare module "*.vue" {
17
- import type { DefineComponent } from "vue";
18
-
19
- const component: DefineComponent<Record<string, never>, Record<string, never>, any>;
20
- export default component;
21
- }
22
-
23
- interface ImportMeta {
24
- readonly SERVER_SIDE_RENDERING: boolean;
25
- }
@@ -1,7 +0,0 @@
1
- declare module "#pages" {
2
- import type { Component } from "vue";
3
-
4
- const entries: Record<string, Component>;
5
-
6
- export default entries;
7
- }