dtwo 2.17.0 → 3.0.0-rc.14
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 +1 -1
- package/README.md +2 -41
- package/app.d.ts +1 -0
- package/bin/dtwo.mjs +2 -0
- package/config.cjs +7 -0
- package/config.d.ts +4 -0
- package/config.mjs +5 -0
- package/dist/app/compat/capi.d.ts +4 -0
- package/dist/app/compat/capi.mjs +19 -0
- package/dist/app/compat/kdu-demi.d.ts +4 -0
- package/dist/app/compat/kdu-demi.mjs +4 -0
- package/dist/app/components/client-only.d.mts +22 -0
- package/dist/app/components/client-only.mjs +75 -0
- package/dist/app/components/dev-only.d.mts +4 -0
- package/dist/app/components/dev-only.mjs +11 -0
- package/dist/app/components/dtwo-error-boundary.d.ts +8 -0
- package/dist/app/components/dtwo-error-boundary.mjs +21 -0
- package/dist/app/components/dtwo-error-page.kdu +46 -0
- package/dist/app/components/dtwo-link.d.ts +33 -0
- package/dist/app/components/dtwo-link.mjs +248 -0
- package/dist/app/components/dtwo-loading-indicator.d.ts +43 -0
- package/dist/app/components/dtwo-loading-indicator.mjs +107 -0
- package/dist/app/components/dtwo-root.kdu +35 -0
- package/dist/app/components/index.d.ts +2 -0
- package/dist/app/components/index.mjs +1 -0
- package/dist/app/components/layout.d.ts +17 -0
- package/dist/app/components/layout.mjs +66 -0
- package/dist/app/components/server-placeholder.d.ts +2 -0
- package/dist/app/components/server-placeholder.mjs +7 -0
- package/dist/app/components/utils.d.ts +11 -0
- package/dist/app/components/utils.mjs +9 -0
- package/dist/app/composables/asyncData.d.ts +40 -0
- package/dist/app/composables/asyncData.mjs +169 -0
- package/dist/app/composables/component.d.ts +3 -0
- package/dist/app/composables/component.mjs +46 -0
- package/dist/app/composables/cookie.d.ts +12 -0
- package/dist/app/composables/cookie.mjs +58 -0
- package/dist/app/composables/error.d.ts +17 -0
- package/dist/app/composables/error.mjs +31 -0
- package/dist/app/composables/fetch.d.ts +15 -0
- package/dist/app/composables/fetch.mjs +66 -0
- package/dist/app/composables/hydrate.d.ts +8 -0
- package/dist/app/composables/hydrate.mjs +14 -0
- package/dist/app/composables/index.d.ts +16 -0
- package/dist/app/composables/index.mjs +11 -0
- package/dist/app/composables/payload.d.ts +8 -0
- package/dist/app/composables/payload.mjs +58 -0
- package/dist/app/composables/preload.d.ts +17 -0
- package/dist/app/composables/preload.mjs +41 -0
- package/dist/app/composables/router.d.ts +28 -0
- package/dist/app/composables/router.mjs +111 -0
- package/dist/app/composables/ssr.d.ts +6 -0
- package/dist/app/composables/ssr.mjs +23 -0
- package/dist/app/composables/state.d.ts +9 -0
- package/dist/app/composables/state.mjs +27 -0
- package/dist/app/config.d.ts +13 -0
- package/dist/app/config.mjs +56 -0
- package/dist/app/dtwo.d.ts +122 -0
- package/dist/app/dtwo.mjs +168 -0
- package/dist/app/entry.async.d.ts +2 -0
- package/dist/app/entry.async.mjs +5 -0
- package/dist/app/entry.d.ts +3 -0
- package/dist/app/entry.mjs +59 -0
- package/dist/app/index.d.ts +10 -0
- package/dist/app/index.mjs +7 -0
- package/dist/app/plugins/cross-origin-prefetch.client.d.ts +2 -0
- package/dist/app/plugins/cross-origin-prefetch.client.mjs +28 -0
- package/dist/app/plugins/debug.d.ts +2 -0
- package/dist/app/plugins/debug.mjs +5 -0
- package/dist/app/plugins/payload.client.d.ts +2 -0
- package/dist/app/plugins/payload.client.mjs +22 -0
- package/dist/app/plugins/preload.server.d.ts +2 -0
- package/dist/app/plugins/preload.server.mjs +10 -0
- package/dist/app/plugins/router.d.ts +53 -0
- package/dist/app/plugins/router.mjs +176 -0
- package/dist/app/types/augments.d.ts +28 -0
- package/dist/core/runtime/hole/error.d.ts +3 -0
- package/dist/core/runtime/hole/error.mjs +58 -0
- package/dist/core/runtime/hole/paths.d.ts +4 -0
- package/dist/core/runtime/hole/paths.mjs +15 -0
- package/dist/core/runtime/hole/renderer.d.ts +16 -0
- package/dist/core/runtime/hole/renderer.mjs +219 -0
- package/dist/head/runtime/components.d.ts +796 -0
- package/dist/head/runtime/components.mjs +209 -0
- package/dist/head/runtime/composables.d.ts +10 -0
- package/dist/head/runtime/composables.mjs +4 -0
- package/dist/head/runtime/index.d.ts +4 -0
- package/dist/head/runtime/index.mjs +1 -0
- package/dist/head/runtime/lib/kdu-meta.plugin.d.ts +2 -0
- package/dist/head/runtime/lib/kdu-meta.plugin.mjs +31 -0
- package/dist/head/runtime/lib/kduse-head.plugin.d.ts +2 -0
- package/dist/head/runtime/lib/kduse-head.plugin.mjs +35 -0
- package/dist/head/runtime/types.d.ts +7 -0
- package/dist/head/runtime/types.mjs +0 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.mjs +2274 -0
- package/dist/pages/runtime/app.kdu +5 -0
- package/dist/pages/runtime/composables.d.ts +42 -0
- package/dist/pages/runtime/composables.mjs +8 -0
- package/dist/pages/runtime/index.d.ts +1 -0
- package/dist/pages/runtime/index.mjs +1 -0
- package/dist/pages/runtime/page-placeholder.d.ts +4 -0
- package/dist/pages/runtime/page-placeholder.mjs +10 -0
- package/dist/pages/runtime/page.d.ts +15 -0
- package/dist/pages/runtime/page.mjs +107 -0
- package/dist/pages/runtime/router.d.ts +8 -0
- package/dist/pages/runtime/router.mjs +152 -0
- package/dist/pages/runtime/router.options.d.ts +2 -0
- package/dist/pages/runtime/router.options.mjs +51 -0
- package/dist/pages/runtime/utils.d.ts +9 -0
- package/dist/pages/runtime/utils.mjs +12 -0
- package/dist/pages/runtime/validate.d.ts +2 -0
- package/dist/pages/runtime/validate.mjs +11 -0
- package/package.json +79 -42
- package/types.d.ts +15 -0
- package/bin/dtwo.js +0 -10
- package/dist/dtwo.js +0 -37
- package/webpack.config.js +0 -4
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { KeepAliveProps, TransitionProps, UnwrapRef } from 'kdu';
|
|
2
|
+
import type { RouteLocationNormalized, RouteLocationNormalizedLoaded, RouteRecordRedirectOption } from 'kdu-router';
|
|
3
|
+
import type { DtwoError } from '#app';
|
|
4
|
+
export interface PageMeta {
|
|
5
|
+
[key: string]: any;
|
|
6
|
+
/**
|
|
7
|
+
* Validate whether a given route can validly be rendered with this page.
|
|
8
|
+
*
|
|
9
|
+
* Return true if it is valid, or false if not. If another match can't be found,
|
|
10
|
+
* this will mean a 404. You can also directly return an object with
|
|
11
|
+
* statusCode/statusMessage to respond immediately with an error (other matches
|
|
12
|
+
* will not be checked).
|
|
13
|
+
*/
|
|
14
|
+
validate?: (route: RouteLocationNormalized) => boolean | Promise<boolean> | Partial<DtwoError> | Promise<Partial<DtwoError>>;
|
|
15
|
+
/**
|
|
16
|
+
* Where to redirect if the route is directly matched. The redirection happens
|
|
17
|
+
* before any navigation guard and triggers a new navigation with the new
|
|
18
|
+
* target location.
|
|
19
|
+
*/
|
|
20
|
+
redirect?: RouteRecordRedirectOption;
|
|
21
|
+
/**
|
|
22
|
+
* Aliases for the record. Allows defining extra paths that will behave like a
|
|
23
|
+
* copy of the record. Allows having paths shorthands like `/users/:id` and
|
|
24
|
+
* `/u/:id`. All `alias` and `path` values must share the same params.
|
|
25
|
+
*/
|
|
26
|
+
alias?: string | string[];
|
|
27
|
+
pageTransition?: boolean | TransitionProps;
|
|
28
|
+
layoutTransition?: boolean | TransitionProps;
|
|
29
|
+
key?: false | string | ((route: RouteLocationNormalizedLoaded) => string);
|
|
30
|
+
keepalive?: boolean | KeepAliveProps;
|
|
31
|
+
/** You may define a name for this page's route. */
|
|
32
|
+
name?: string;
|
|
33
|
+
/** You may define a path matcher, if you have a more complex pattern than can be expressed with the file name. */
|
|
34
|
+
path?: string;
|
|
35
|
+
/** Set to `false` to avoid scrolling to top on page navigations */
|
|
36
|
+
scrollToTop?: boolean;
|
|
37
|
+
}
|
|
38
|
+
declare module 'kdu-router' {
|
|
39
|
+
interface RouteMeta extends UnwrapRef<PageMeta> {
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export declare const definePageMeta: (meta: PageMeta) => void;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
const warnRuntimeUsage = (method) => console.warn(
|
|
2
|
+
`${method}() is a compiler-hint helper that is only usable inside the script block of a single file component which is also a page. Its arguments should be compiled away and passing it at runtime has no effect.`
|
|
3
|
+
);
|
|
4
|
+
export const definePageMeta = (meta) => {
|
|
5
|
+
if (process.dev) {
|
|
6
|
+
warnRuntimeUsage("definePageMeta");
|
|
7
|
+
}
|
|
8
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './composables';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./composables.mjs";
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
declare const _default: import("kdu").DefineComponent<{}, () => import("kdu").KNode<import("kdu").RendererNode, import("kdu").RendererElement, {
|
|
2
|
+
[key: string]: any;
|
|
3
|
+
}>[] | undefined, {}, {}, {}, import("kdu").ComponentOptionsMixin, import("kdu").ComponentOptionsMixin, {}, string, import("kdu").KNodeProps & import("kdu").AllowedComponentProps & import("kdu").ComponentCustomProps, Readonly<import("kdu").ExtractPropTypes<{}>>, {}>;
|
|
4
|
+
export default _default;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { defineComponent } from "kdu";
|
|
2
|
+
export default defineComponent({
|
|
3
|
+
name: "DtwoPage",
|
|
4
|
+
setup(_, props) {
|
|
5
|
+
if (process.dev) {
|
|
6
|
+
console.warn("Create a Kdu component in the `pages/` directory to enable `<DtwoPage>`");
|
|
7
|
+
}
|
|
8
|
+
return () => props.slots.default?.();
|
|
9
|
+
}
|
|
10
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { DefineComponent } from 'kdu';
|
|
2
|
+
declare const _default: DefineComponent<{
|
|
3
|
+
[key: string]: any;
|
|
4
|
+
name?: string | undefined;
|
|
5
|
+
route?: any;
|
|
6
|
+
pageKey?: string | ((route: RouteLocationNormalizedLoaded) => string) | undefined;
|
|
7
|
+
}, {}, {}, import("kdu").ComputedOptions, import("kdu").MethodOptions, import("kdu").ComponentOptionsMixin, import("kdu").ComponentOptionsMixin, {}, string, import("kdu").KNodeProps & import("kdu").AllowedComponentProps & import("kdu").ComponentCustomProps, Readonly<import("kdu").ExtractPropTypes<{
|
|
8
|
+
[key: string]: any;
|
|
9
|
+
name?: string | undefined;
|
|
10
|
+
route?: any;
|
|
11
|
+
pageKey?: string | ((route: RouteLocationNormalizedLoaded) => string) | undefined;
|
|
12
|
+
}>>, {
|
|
13
|
+
[x: string]: any;
|
|
14
|
+
}>;
|
|
15
|
+
export default _default;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { computed, defineComponent, h, provide, reactive, onMounted, nextTick, Suspense, Transition } from "kdu";
|
|
2
|
+
import { RouterView } from "kdu-router";
|
|
3
|
+
import { defu } from "defu";
|
|
4
|
+
import { generateRouteKey, wrapInKeepAlive } from "./utils.mjs";
|
|
5
|
+
import { useDtwoApp } from "#app";
|
|
6
|
+
import { _wrapIf } from "#app/components/utils";
|
|
7
|
+
import { appPageTransition as defaultPageTransition, appKeepalive as defaultKeepaliveConfig } from "#build/dtwo.config.mjs";
|
|
8
|
+
export default defineComponent({
|
|
9
|
+
name: "DtwoPage",
|
|
10
|
+
inheritAttrs: false,
|
|
11
|
+
props: {
|
|
12
|
+
name: {
|
|
13
|
+
type: String
|
|
14
|
+
},
|
|
15
|
+
transition: {
|
|
16
|
+
type: [Boolean, Object],
|
|
17
|
+
default: void 0
|
|
18
|
+
},
|
|
19
|
+
keepalive: {
|
|
20
|
+
type: [Boolean, Object],
|
|
21
|
+
default: void 0
|
|
22
|
+
},
|
|
23
|
+
route: {
|
|
24
|
+
type: Object
|
|
25
|
+
},
|
|
26
|
+
pageKey: {
|
|
27
|
+
type: [Function, String],
|
|
28
|
+
default: null
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
setup(props, { attrs }) {
|
|
32
|
+
const dtwoApp = useDtwoApp();
|
|
33
|
+
return () => {
|
|
34
|
+
return h(RouterView, { name: props.name, route: props.route, ...attrs }, {
|
|
35
|
+
default: (routeProps) => {
|
|
36
|
+
if (!routeProps.Component) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const key = generateRouteKey(props.pageKey, routeProps);
|
|
40
|
+
const done = dtwoApp.deferHydration();
|
|
41
|
+
const hasTransition = !!(props.transition ?? routeProps.route.meta.pageTransition ?? defaultPageTransition);
|
|
42
|
+
const transitionProps = hasTransition && _mergeTransitionProps([
|
|
43
|
+
props.transition,
|
|
44
|
+
routeProps.route.meta.pageTransition,
|
|
45
|
+
defaultPageTransition,
|
|
46
|
+
{ onAfterLeave: () => {
|
|
47
|
+
dtwoApp.callHook("page:transition:finish", routeProps.Component);
|
|
48
|
+
} }
|
|
49
|
+
].filter(Boolean));
|
|
50
|
+
return _wrapIf(
|
|
51
|
+
Transition,
|
|
52
|
+
hasTransition && transitionProps,
|
|
53
|
+
wrapInKeepAlive(
|
|
54
|
+
props.keepalive ?? routeProps.route.meta.keepalive ?? defaultKeepaliveConfig,
|
|
55
|
+
h(Suspense, {
|
|
56
|
+
onPending: () => dtwoApp.callHook("page:start", routeProps.Component),
|
|
57
|
+
onResolve: () => {
|
|
58
|
+
nextTick(() => dtwoApp.callHook("page:finish", routeProps.Component).finally(done));
|
|
59
|
+
}
|
|
60
|
+
}, { default: () => h(Component, { key, routeProps, pageKey: key, hasTransition }) })
|
|
61
|
+
)
|
|
62
|
+
).default();
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
function _toArray(val) {
|
|
69
|
+
return Array.isArray(val) ? val : val ? [val] : [];
|
|
70
|
+
}
|
|
71
|
+
function _mergeTransitionProps(routeProps) {
|
|
72
|
+
const _props = routeProps.map((prop) => ({
|
|
73
|
+
...prop,
|
|
74
|
+
onAfterLeave: _toArray(prop.onAfterLeave)
|
|
75
|
+
}));
|
|
76
|
+
return defu(..._props);
|
|
77
|
+
}
|
|
78
|
+
const Component = defineComponent({
|
|
79
|
+
props: ["routeProps", "pageKey", "hasTransition"],
|
|
80
|
+
setup(props) {
|
|
81
|
+
const previousKey = props.pageKey;
|
|
82
|
+
const previousRoute = props.routeProps.route;
|
|
83
|
+
const route = {};
|
|
84
|
+
for (const key in props.routeProps.route) {
|
|
85
|
+
route[key] = computed(() => previousKey === props.pageKey ? props.routeProps.route[key] : previousRoute[key]);
|
|
86
|
+
}
|
|
87
|
+
provide("_route", reactive(route));
|
|
88
|
+
let knode;
|
|
89
|
+
if (process.dev && process.client && props.hasTransition) {
|
|
90
|
+
onMounted(() => {
|
|
91
|
+
nextTick(() => {
|
|
92
|
+
if (["#comment", "#text"].includes(knode?.el?.nodeName)) {
|
|
93
|
+
const filename = (knode?.type).__file;
|
|
94
|
+
console.warn(`[dtwo] \`${filename}\` does not have a single root node and will cause errors when navigating between routes.`);
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
return () => {
|
|
100
|
+
if (process.dev && process.client) {
|
|
101
|
+
knode = h(props.routeProps.Component);
|
|
102
|
+
return knode;
|
|
103
|
+
}
|
|
104
|
+
return h(props.routeProps.Component);
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
});
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { computed, reactive, shallowRef } from "kdu";
|
|
2
|
+
import {
|
|
3
|
+
createRouter,
|
|
4
|
+
createWebHistory,
|
|
5
|
+
createMemoryHistory,
|
|
6
|
+
createWebHashHistory
|
|
7
|
+
} from "kdu-router";
|
|
8
|
+
import { createError } from "h3";
|
|
9
|
+
import { withoutBase, isEqual } from "ufo";
|
|
10
|
+
import { callWithDtwo, defineDtwoPlugin, useRuntimeConfig, showError, clearError, navigateTo, useError, useState } from "#app";
|
|
11
|
+
import _routes from "#build/routes";
|
|
12
|
+
import routerOptions from "#build/router.options";
|
|
13
|
+
import { globalMiddleware, namedMiddleware } from "#build/middleware";
|
|
14
|
+
function createCurrentLocation(base, location) {
|
|
15
|
+
const { pathname, search, hash } = location;
|
|
16
|
+
const hashPos = base.indexOf("#");
|
|
17
|
+
if (hashPos > -1) {
|
|
18
|
+
const slicePos = hash.includes(base.slice(hashPos)) ? base.slice(hashPos).length : 1;
|
|
19
|
+
let pathFromHash = hash.slice(slicePos);
|
|
20
|
+
if (pathFromHash[0] !== "/") {
|
|
21
|
+
pathFromHash = "/" + pathFromHash;
|
|
22
|
+
}
|
|
23
|
+
return withoutBase(pathFromHash, "");
|
|
24
|
+
}
|
|
25
|
+
const path = withoutBase(pathname, base);
|
|
26
|
+
return path + search + hash;
|
|
27
|
+
}
|
|
28
|
+
export default defineDtwoPlugin(async (dtwoApp) => {
|
|
29
|
+
let routerBase = useRuntimeConfig().app.baseURL;
|
|
30
|
+
if (routerOptions.hashMode && !routerBase.includes("#")) {
|
|
31
|
+
routerBase += "#";
|
|
32
|
+
}
|
|
33
|
+
const history = routerOptions.history?.(routerBase) ?? (process.client ? routerOptions.hashMode ? createWebHashHistory(routerBase) : createWebHistory(routerBase) : createMemoryHistory(routerBase));
|
|
34
|
+
const routes = routerOptions.routes?.(_routes) ?? _routes;
|
|
35
|
+
const initialURL = process.server ? dtwoApp.ssrContext.url : createCurrentLocation(routerBase, window.location);
|
|
36
|
+
const router = createRouter({
|
|
37
|
+
...routerOptions,
|
|
38
|
+
history,
|
|
39
|
+
routes
|
|
40
|
+
});
|
|
41
|
+
dtwoApp.kduApp.use(router);
|
|
42
|
+
const previousRoute = shallowRef(router.currentRoute.value);
|
|
43
|
+
router.afterEach((_to, from) => {
|
|
44
|
+
previousRoute.value = from;
|
|
45
|
+
});
|
|
46
|
+
Object.defineProperty(dtwoApp.kduApp.config.globalProperties, "previousRoute", {
|
|
47
|
+
get: () => previousRoute.value
|
|
48
|
+
});
|
|
49
|
+
const _route = shallowRef(router.resolve(initialURL));
|
|
50
|
+
const syncCurrentRoute = () => {
|
|
51
|
+
_route.value = router.currentRoute.value;
|
|
52
|
+
};
|
|
53
|
+
dtwoApp.hook("page:finish", syncCurrentRoute);
|
|
54
|
+
router.afterEach((to, from) => {
|
|
55
|
+
if (to.matched[0]?.components?.default === from.matched[0]?.components?.default) {
|
|
56
|
+
syncCurrentRoute();
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
const route = {};
|
|
60
|
+
for (const key in _route.value) {
|
|
61
|
+
route[key] = computed(() => _route.value[key]);
|
|
62
|
+
}
|
|
63
|
+
dtwoApp._route = reactive(route);
|
|
64
|
+
dtwoApp._middleware = dtwoApp._middleware || {
|
|
65
|
+
global: [],
|
|
66
|
+
named: {}
|
|
67
|
+
};
|
|
68
|
+
const error = useError();
|
|
69
|
+
try {
|
|
70
|
+
if (process.server) {
|
|
71
|
+
await router.push(initialURL);
|
|
72
|
+
}
|
|
73
|
+
await router.isReady();
|
|
74
|
+
} catch (error2) {
|
|
75
|
+
callWithDtwo(dtwoApp, showError, [error2]);
|
|
76
|
+
}
|
|
77
|
+
const initialLayout = useState("_layout");
|
|
78
|
+
router.beforeEach(async (to, from) => {
|
|
79
|
+
to.meta = reactive(to.meta);
|
|
80
|
+
if (dtwoApp.isHydrating) {
|
|
81
|
+
to.meta.layout = initialLayout.value ?? to.meta.layout;
|
|
82
|
+
}
|
|
83
|
+
dtwoApp._processingMiddleware = true;
|
|
84
|
+
const middlewareEntries = /* @__PURE__ */ new Set([...globalMiddleware, ...dtwoApp._middleware.global]);
|
|
85
|
+
for (const component of to.matched) {
|
|
86
|
+
const componentMiddleware = component.meta.middleware;
|
|
87
|
+
if (!componentMiddleware) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (Array.isArray(componentMiddleware)) {
|
|
91
|
+
for (const entry of componentMiddleware) {
|
|
92
|
+
middlewareEntries.add(entry);
|
|
93
|
+
}
|
|
94
|
+
} else {
|
|
95
|
+
middlewareEntries.add(componentMiddleware);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
for (const entry of middlewareEntries) {
|
|
99
|
+
const middleware = typeof entry === "string" ? dtwoApp._middleware.named[entry] || await namedMiddleware[entry]?.().then((r) => r.default || r) : entry;
|
|
100
|
+
if (!middleware) {
|
|
101
|
+
if (process.dev) {
|
|
102
|
+
throw new Error(`Unknown route middleware: '${entry}'. Valid middleware: ${Object.keys(namedMiddleware).map((mw) => `'${mw}'`).join(", ")}.`);
|
|
103
|
+
}
|
|
104
|
+
throw new Error(`Unknown route middleware: '${entry}'.`);
|
|
105
|
+
}
|
|
106
|
+
const result = await callWithDtwo(dtwoApp, middleware, [to, from]);
|
|
107
|
+
if (process.server || !dtwoApp.payload.serverRendered && dtwoApp.isHydrating) {
|
|
108
|
+
if (result === false || result instanceof Error) {
|
|
109
|
+
const error2 = result || createError({
|
|
110
|
+
statusCode: 404,
|
|
111
|
+
statusMessage: `Page Not Found: ${initialURL}`
|
|
112
|
+
});
|
|
113
|
+
await callWithDtwo(dtwoApp, showError, [error2]);
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (result || result === false) {
|
|
118
|
+
return result;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
router.afterEach(async (to) => {
|
|
123
|
+
delete dtwoApp._processingMiddleware;
|
|
124
|
+
if (process.client && !dtwoApp.isHydrating && error.value) {
|
|
125
|
+
await callWithDtwo(dtwoApp, clearError);
|
|
126
|
+
}
|
|
127
|
+
if (to.matched.length === 0) {
|
|
128
|
+
callWithDtwo(dtwoApp, showError, [createError({
|
|
129
|
+
statusCode: 404,
|
|
130
|
+
fatal: false,
|
|
131
|
+
statusMessage: `Page not found: ${to.fullPath}`
|
|
132
|
+
})]);
|
|
133
|
+
} else if (process.server) {
|
|
134
|
+
const currentURL = to.fullPath || "/";
|
|
135
|
+
if (!isEqual(currentURL, initialURL)) {
|
|
136
|
+
await callWithDtwo(dtwoApp, navigateTo, [currentURL]);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
dtwoApp.hooks.hookOnce("app:created", async () => {
|
|
141
|
+
try {
|
|
142
|
+
await router.replace({
|
|
143
|
+
...router.resolve(initialURL),
|
|
144
|
+
name: void 0,
|
|
145
|
+
force: true
|
|
146
|
+
});
|
|
147
|
+
} catch (error2) {
|
|
148
|
+
callWithDtwo(dtwoApp, showError, [error2]);
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
return { provide: { router } };
|
|
152
|
+
});
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { nextTick } from "kdu";
|
|
2
|
+
import { useDtwoApp } from "#app";
|
|
3
|
+
import { appPageTransition as defaultPageTransition } from "#build/dtwo.config.mjs";
|
|
4
|
+
export default {
|
|
5
|
+
scrollBehavior(to, from, savedPosition) {
|
|
6
|
+
const dtwoApp = useDtwoApp();
|
|
7
|
+
let position = savedPosition || void 0;
|
|
8
|
+
if (!position && from && to && to.meta.scrollToTop !== false && _isDifferentRoute(from, to)) {
|
|
9
|
+
position = { left: 0, top: 0 };
|
|
10
|
+
}
|
|
11
|
+
if (to.path === from.path) {
|
|
12
|
+
if (from.hash && !to.hash) {
|
|
13
|
+
return { left: 0, top: 0 };
|
|
14
|
+
}
|
|
15
|
+
if (to.hash) {
|
|
16
|
+
return { el: to.hash, top: _getHashElementScrollMarginTop(to.hash) };
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
const hasTransition = (route) => !!(route.meta.pageTransition ?? defaultPageTransition);
|
|
20
|
+
const hookToWait = hasTransition(from) && hasTransition(to) ? "page:transition:finish" : "page:finish";
|
|
21
|
+
return new Promise((resolve) => {
|
|
22
|
+
dtwoApp.hooks.hookOnce(hookToWait, async () => {
|
|
23
|
+
await nextTick();
|
|
24
|
+
if (to.hash) {
|
|
25
|
+
position = { el: to.hash, top: _getHashElementScrollMarginTop(to.hash) };
|
|
26
|
+
}
|
|
27
|
+
resolve(position);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
function _getHashElementScrollMarginTop(selector) {
|
|
33
|
+
try {
|
|
34
|
+
const elem = document.querySelector(selector);
|
|
35
|
+
if (elem) {
|
|
36
|
+
return parseFloat(getComputedStyle(elem).scrollMarginTop);
|
|
37
|
+
}
|
|
38
|
+
} catch {
|
|
39
|
+
}
|
|
40
|
+
return 0;
|
|
41
|
+
}
|
|
42
|
+
function _isDifferentRoute(a, b) {
|
|
43
|
+
const samePageComponent = a.matched[0] === b.matched[0];
|
|
44
|
+
if (!samePageComponent) {
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
if (samePageComponent && JSON.stringify(a.params) !== JSON.stringify(b.params)) {
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { RouterView, RouteLocationNormalizedLoaded } from 'kdu-router';
|
|
2
|
+
type InstanceOf<T> = T extends new (...args: any[]) => infer R ? R : never;
|
|
3
|
+
type RouterViewSlot = Exclude<InstanceOf<typeof RouterView>['$slots']['default'], undefined>;
|
|
4
|
+
export type RouterViewSlotProps = Parameters<RouterViewSlot>[0];
|
|
5
|
+
export declare const generateRouteKey: (override: string | ((route: RouteLocationNormalizedLoaded) => string), routeProps: RouterViewSlotProps) => string;
|
|
6
|
+
export declare const wrapInKeepAlive: (props: any, children: any) => {
|
|
7
|
+
default: () => any;
|
|
8
|
+
};
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { KeepAlive, h } from "kdu";
|
|
2
|
+
const interpolatePath = (route, match) => {
|
|
3
|
+
return match.path.replace(/(:\w+)\([^)]+\)/g, "$1").replace(/(:\w+)[?+*]/g, "$1").replace(/:\w+/g, (r) => route.params[r.slice(1)]?.toString() || "");
|
|
4
|
+
};
|
|
5
|
+
export const generateRouteKey = (override, routeProps) => {
|
|
6
|
+
const matchedRoute = routeProps.route.matched.find((m) => m.components?.default === routeProps.Component.type);
|
|
7
|
+
const source = override ?? matchedRoute?.meta.key ?? (matchedRoute && interpolatePath(routeProps.route, matchedRoute));
|
|
8
|
+
return typeof source === "function" ? source(routeProps.route) : source;
|
|
9
|
+
};
|
|
10
|
+
export const wrapInKeepAlive = (props, children) => {
|
|
11
|
+
return { default: () => process.client && props ? h(KeepAlive, props === true ? {} : props, children) : children };
|
|
12
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { createError, defineDtwoRouteMiddleware } from "#app";
|
|
2
|
+
export default defineDtwoRouteMiddleware(async (to) => {
|
|
3
|
+
if (!to.meta?.validate) {
|
|
4
|
+
return;
|
|
5
|
+
}
|
|
6
|
+
const result = await Promise.resolve(to.meta.validate(to));
|
|
7
|
+
if (typeof result === "boolean") {
|
|
8
|
+
return result;
|
|
9
|
+
}
|
|
10
|
+
return createError(result);
|
|
11
|
+
});
|
package/package.json
CHANGED
|
@@ -1,54 +1,91 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dtwo",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "A minimalistic framework for server-rendered Kdu.js applications (inspired by Next.js)",
|
|
5
|
-
"keywords": [
|
|
6
|
-
"dtwo",
|
|
7
|
-
"dtwo.js",
|
|
8
|
-
"dtwojs",
|
|
9
|
-
"ssr",
|
|
10
|
-
"kdu",
|
|
11
|
-
"kdu isomorphic",
|
|
12
|
-
"kdu server side",
|
|
13
|
-
"kdu ssr",
|
|
14
|
-
"kdu universal",
|
|
15
|
-
"kdu versatile",
|
|
16
|
-
"kdu.js",
|
|
17
|
-
"kdujs"
|
|
18
|
-
],
|
|
3
|
+
"version": "3.0.0-rc.14",
|
|
19
4
|
"repository": "d2js/framework",
|
|
20
5
|
"license": "MIT",
|
|
21
|
-
"
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.mjs",
|
|
8
|
+
"types": "./types.d.ts",
|
|
22
9
|
"bin": {
|
|
23
|
-
"
|
|
10
|
+
"dcli": "./bin/dtwo.mjs",
|
|
11
|
+
"dtwo": "./bin/dtwo.mjs"
|
|
12
|
+
},
|
|
13
|
+
"exports": {
|
|
14
|
+
".": "./dist/index.mjs",
|
|
15
|
+
"./config": {
|
|
16
|
+
"import": "./config.mjs",
|
|
17
|
+
"require": "./config.cjs",
|
|
18
|
+
"types": "./config.d.ts"
|
|
19
|
+
},
|
|
20
|
+
"./app": "./dist/app/index.mjs",
|
|
21
|
+
"./package.json": "./package.json"
|
|
22
|
+
},
|
|
23
|
+
"imports": {
|
|
24
|
+
"#app": "./dist/app/index.mjs",
|
|
25
|
+
"#head": "./dist/head/runtime/index.mjs",
|
|
26
|
+
"#pages": "./dist/pages/runtime/index.mjs"
|
|
24
27
|
},
|
|
25
28
|
"files": [
|
|
29
|
+
"app.d.ts",
|
|
26
30
|
"bin",
|
|
31
|
+
"types.d.ts",
|
|
27
32
|
"dist",
|
|
28
|
-
"
|
|
33
|
+
"config.*"
|
|
29
34
|
],
|
|
30
|
-
"scripts": {
|
|
31
|
-
"postinstall": "opencollective || exit 0"
|
|
32
|
-
},
|
|
33
35
|
"dependencies": {
|
|
34
|
-
"@
|
|
35
|
-
"@dtwo/
|
|
36
|
-
"@dtwo/
|
|
37
|
-
"@dtwo/
|
|
38
|
-
"@dtwo/
|
|
39
|
-
"@dtwo/
|
|
40
|
-
"@dtwo/
|
|
41
|
-
"@dtwo/
|
|
42
|
-
"@
|
|
43
|
-
"@
|
|
44
|
-
"@
|
|
45
|
-
"@
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
36
|
+
"@d2js/devalue": "^2.0.2",
|
|
37
|
+
"@dtwo/cli": "3.0.0-rc.14",
|
|
38
|
+
"@dtwo/kit": "3.0.0-rc.14",
|
|
39
|
+
"@dtwo/schema": "3.0.0-rc.14",
|
|
40
|
+
"@dtwo/telemetry": "^2.1.7",
|
|
41
|
+
"@dtwo/ui-templates": "^0.4.0",
|
|
42
|
+
"@dtwo/unplugin": "^1.0.0",
|
|
43
|
+
"@dtwo/wite-builder": "3.0.0-rc.14",
|
|
44
|
+
"@kdhead/ssr": "^0.6.9",
|
|
45
|
+
"@kdujs/reactivity": "^3.2.45",
|
|
46
|
+
"@kdujs/shared": "^3.2.45",
|
|
47
|
+
"@kduse/head": "^1.0.13",
|
|
48
|
+
"chokidar": "^3.5.3",
|
|
49
|
+
"cookie-es": "^0.5.0",
|
|
50
|
+
"defu": "^6.1.1",
|
|
51
|
+
"destr": "^1.2.1",
|
|
52
|
+
"escape-string-regexp": "^5.0.0",
|
|
53
|
+
"estree-walker": "^3.0.1",
|
|
54
|
+
"fs-extra": "^10.1.0",
|
|
55
|
+
"globby": "^13.1.2",
|
|
56
|
+
"h3": "^1.0.1",
|
|
57
|
+
"hash-sum": "^2.0.0",
|
|
58
|
+
"holepack": "^1.0.0",
|
|
59
|
+
"hookable": "^5.4.2",
|
|
60
|
+
"kdhead": "^0.6.9",
|
|
61
|
+
"kdimport": "^1.0.0",
|
|
62
|
+
"kdu": "^3.2.45",
|
|
63
|
+
"kdu-bundle-renderer": "^1.0.0",
|
|
64
|
+
"kdu-devtools-stub": "^0.1.0",
|
|
65
|
+
"kdu-router": "^4.1.6",
|
|
66
|
+
"knitwork": "^1.0.0",
|
|
67
|
+
"magic-string": "^0.26.7",
|
|
68
|
+
"mlly": "^1.0.0",
|
|
69
|
+
"ohash": "^1.0.0",
|
|
70
|
+
"ofetch": "^1.0.0",
|
|
71
|
+
"pathe": "^1.0.0",
|
|
72
|
+
"perfect-debounce": "^0.1.3",
|
|
73
|
+
"scule": "^1.0.0",
|
|
74
|
+
"strip-literal": "^1.0.0",
|
|
75
|
+
"ufo": "^1.0.0",
|
|
76
|
+
"ultrahtml": "^1.0.0",
|
|
77
|
+
"unctx": "^2.1.0",
|
|
78
|
+
"unenv": "^1.0.0",
|
|
79
|
+
"untyped": "^1.0.0"
|
|
80
|
+
},
|
|
81
|
+
"devDependencies": {
|
|
82
|
+
"@types/fs-extra": "^9.0.13",
|
|
83
|
+
"@types/hash-sum": "^1.0.0",
|
|
84
|
+
"kdu-meta": "next",
|
|
85
|
+
"unbuild": "latest"
|
|
86
|
+
},
|
|
87
|
+
"engines": {
|
|
88
|
+
"node": "^14.16.0 || ^16.10.0 || ^17.0.0 || ^18.0.0 || ^19.0.0"
|
|
49
89
|
},
|
|
50
|
-
"
|
|
51
|
-
|
|
52
|
-
"logoUrl": "https://opencollective.com/kdu-platforms/logo.txt?reverse=true&variant=variant2"
|
|
53
|
-
}
|
|
54
|
-
}
|
|
90
|
+
"scripts": {}
|
|
91
|
+
}
|
package/types.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/// <reference types="holepack" />
|
|
2
|
+
export * from './dist/index'
|
|
3
|
+
|
|
4
|
+
declare global {
|
|
5
|
+
const defineDtwoConfig: typeof import('dtwo/config')['defineDtwoConfig']
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
declare module 'holepack' {
|
|
9
|
+
interface HoleRouteConfig {
|
|
10
|
+
ssr?: boolean
|
|
11
|
+
}
|
|
12
|
+
interface HoleRouteRules {
|
|
13
|
+
ssr?: boolean
|
|
14
|
+
}
|
|
15
|
+
}
|
package/bin/dtwo.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
global.__DTWO_PATHS__ = (global.__DTWO_PATHS__ || []).concat(__dirname)
|
|
4
|
-
|
|
5
|
-
const suffix = require('../package.json').name.includes('-edge') ? '-edge' : ''
|
|
6
|
-
require('@dtwo/cli' + suffix).run()
|
|
7
|
-
.catch((error) => {
|
|
8
|
-
require('consola').fatal(error)
|
|
9
|
-
process.exit(2)
|
|
10
|
-
})
|
package/dist/dtwo.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* dtwo v2.17.0 (c) 2023-2023
|
|
3
|
-
* Released under the MIT License
|
|
4
|
-
* Repository: https://github.com/d2js/framework
|
|
5
|
-
* Website: https://dtwojs-v2.web.app
|
|
6
|
-
*/
|
|
7
|
-
'use strict';
|
|
8
|
-
|
|
9
|
-
const core = require('@dtwo/core');
|
|
10
|
-
const builder = require('@dtwo/builder');
|
|
11
|
-
const generator = require('@dtwo/generator');
|
|
12
|
-
const cli = require('@dtwo/cli');
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
Object.defineProperty(exports, 'getWebpackConfig', {
|
|
17
|
-
enumerable: true,
|
|
18
|
-
get: function () { return cli.getWebpackConfig; }
|
|
19
|
-
});
|
|
20
|
-
Object.keys(core).forEach(function (k) {
|
|
21
|
-
if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
|
|
22
|
-
enumerable: true,
|
|
23
|
-
get: function () { return core[k]; }
|
|
24
|
-
});
|
|
25
|
-
});
|
|
26
|
-
Object.keys(builder).forEach(function (k) {
|
|
27
|
-
if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
|
|
28
|
-
enumerable: true,
|
|
29
|
-
get: function () { return builder[k]; }
|
|
30
|
-
});
|
|
31
|
-
});
|
|
32
|
-
Object.keys(generator).forEach(function (k) {
|
|
33
|
-
if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
|
|
34
|
-
enumerable: true,
|
|
35
|
-
get: function () { return generator[k]; }
|
|
36
|
-
});
|
|
37
|
-
});
|