@revojs/vue 0.1.48 → 0.1.50

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