valaxy 1.1.0 → 1.2.1

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,284 +0,0 @@
1
- import { t as normalizeLayoutName } from "./layoutName-DVkP7Qpo.mjs";
2
- import * as Vue from "vue";
3
- import { computed, defineAsyncComponent, defineComponent, h, inject, shallowRef } from "vue";
4
- import { RouterView, START_LOCATION, matchedRouteKey, routerKey, useRoute, useRouter } from "vue-router";
5
- //#region src/runtime/layoutMeta.ts
6
- function readLayoutMeta(meta) {
7
- const layout = meta?.layout;
8
- if (layout !== null && typeof layout === "object") return {
9
- name: layout.name,
10
- props: layout.props ?? meta?.layoutProps
11
- };
12
- return {
13
- name: layout,
14
- props: meta?.layoutProps
15
- };
16
- }
17
- function declaresLayout(meta) {
18
- const layout = meta?.layout;
19
- if (layout !== null && typeof layout === "object") return layout.name !== false;
20
- return !!layout;
21
- }
22
- //#endregion
23
- //#region src/runtime/setupLayouts.ts
24
- function createGetRoutes(router, withLayout = false) {
25
- const routes = router.getRoutes();
26
- if (withLayout) return routes;
27
- return () => routes.filter((route) => !route.meta.isLayout);
28
- }
29
- function hasChildWithLayout(route) {
30
- if (!route.children || route.children.length === 0) return false;
31
- return route.children.some((child) => {
32
- if (declaresLayout(child.meta)) return true;
33
- if (child.meta?.isLayout) return true;
34
- return hasChildWithLayout(child);
35
- });
36
- }
37
- function createSetupLayouts(wrapper, options) {
38
- const { inheritDefaultLayout } = options;
39
- function wrap(route, keepRootPath) {
40
- return {
41
- path: route.path,
42
- component: wrapper,
43
- children: keepRootPath && route.path === "/" ? [route] : [{
44
- ...route,
45
- path: ""
46
- }],
47
- meta: { isLayout: true }
48
- };
49
- }
50
- function deepSetupLayout(routes, top = true) {
51
- return routes.map((route) => {
52
- const childHasLayout = top && !inheritDefaultLayout && (route.children?.length ?? 0) > 0 ? hasChildWithLayout(route) : false;
53
- if (route.children && route.children.length > 0) route.children = deepSetupLayout(route.children, false);
54
- if (top) {
55
- if (!route.component && route.children?.find((r) => (r.path === "" || r.path === "/") && r.meta?.isLayout)) return route;
56
- if (readLayoutMeta(route.meta).name !== false) {
57
- if (inheritDefaultLayout || !childHasLayout) return wrap(route, true);
58
- }
59
- }
60
- if (declaresLayout(route.meta)) return wrap(route, false);
61
- return route;
62
- });
63
- }
64
- return (routes) => deepSetupLayout(routes);
65
- }
66
- //#endregion
67
- //#region src/runtime/wrapper.ts
68
- const LAZY = Symbol.for("vite-plugin-vue-layouts-next:lazy");
69
- /**
70
- * Both virtual-module generators wrap their async imports with this, so a lazy entry
71
- * is always identified explicitly rather than guessed from its shape - unlike Vue
72
- * Router's route components, a bare functional component (e.g. an arrow-function
73
- * default export from a `.tsx` layout) is a perfectly valid, non-lazy layout here.
74
- */
75
- function lazyLayout(loader) {
76
- return Object.assign(loader, { [LAZY]: true });
77
- }
78
- function isLazyLayout(entry) {
79
- return typeof entry === "function" && entry[LAZY] === true;
80
- }
81
- const PREFIX = "[vite-plugin-vue-layouts-next]";
82
- const hasInjectionContext = Vue.hasInjectionContext ?? (() => false);
83
- /** In-place override set by `setPageLayout`; cleared on navigation to another path. */
84
- const override = shallowRef(null);
85
- const guardedRouters = /* @__PURE__ */ new WeakSet();
86
- /** `defaultLayout` of the last created wrapper; shared with `useLayout()`. */
87
- let resolvedDefaultLayout = "default";
88
- /**
89
- * Change the layout of the current page without navigating, optionally passing props
90
- * to the layout component. The override lasts until the router navigates to a different `path`.
91
- */
92
- function setPageLayout(name, props) {
93
- override.value = name == null ? null : {
94
- name,
95
- props
96
- };
97
- }
98
- /**
99
- * Reactive name of the layout resolved for the current route.
100
- * `false` when no wrapper is rendered around it (a static `layout: false` route, or
101
- * one deliberately left unwrapped because `inheritDefaultLayout` is `false`).
102
- */
103
- function useLayout() {
104
- const route = useRoute();
105
- return computed(() => {
106
- const own = innermostLayoutRecord(route);
107
- if (!own) return false;
108
- return resolveLayoutFor(route, own, override.value).name;
109
- });
110
- }
111
- function innermostLayoutRecord(route) {
112
- const { matched } = route;
113
- for (let i = matched.length - 1; i >= 0; i--) if (matched[i].meta.isLayout) return matched[i];
114
- }
115
- /**
116
- * Layout (name and props) the wrapper whose generated record is `own` renders for `route`.
117
- *
118
- * The static layout comes from the wrapped record's own meta (`||` fallback to the
119
- * default, like the original algorithm), so nested trees keep one layout per level.
120
- * Dynamic inputs - the `setPageLayout` override and a guard assignment to the merged
121
- * `route.meta.layout` / `route.meta.layoutProps` - apply only to the innermost wrapper.
122
- * A dynamic layout brings its own props and never inherits the page's static ones.
123
- */
124
- function resolveLayoutFor(route, own, overrideValue) {
125
- const { matched } = route;
126
- const idx = matched.indexOf(own);
127
- const page = readLayoutMeta(matched[idx + 1]?.meta);
128
- const staticLayout = {
129
- name: page.name || resolvedDefaultLayout,
130
- props: page.props
131
- };
132
- if (!!matched.slice(idx + 2).some((r) => r.meta.isLayout)) return staticLayout;
133
- if (overrideValue) return overrideValue;
134
- const staticMerged = matched.reduce((m, r) => r.meta.layout ?? m, void 0);
135
- const staticMergedProps = matched.reduce((m, r) => r.meta.layoutProps ?? m, void 0);
136
- const guardProps = route.meta.layoutProps !== staticMergedProps ? route.meta.layoutProps : void 0;
137
- if (route.meta.layout != null && route.meta.layout !== staticMerged) {
138
- const guard = readLayoutMeta({
139
- layout: route.meta.layout,
140
- layoutProps: guardProps
141
- });
142
- return {
143
- name: guard.name ?? resolvedDefaultLayout,
144
- props: guard.props
145
- };
146
- }
147
- return {
148
- name: staticLayout.name,
149
- props: guardProps ?? staticLayout.props
150
- };
151
- }
152
- function unwrapModule(mod) {
153
- return mod && typeof mod === "object" && "default" in mod ? mod.default : mod;
154
- }
155
- /** Options-API navigation guards that never run on a layout component (see `checkRouteGuards`). */
156
- const ROUTE_GUARD_NAMES = [
157
- "beforeRouteEnter",
158
- "beforeRouteUpdate",
159
- "beforeRouteLeave"
160
- ];
161
- /** `fallbackLayout` is rendered when the requested layout is not in `layouts`; if it is missing too, the page renders without a layout. */
162
- function createLayoutWrapper(layouts, defaultLayout, fallbackLayout = defaultLayout) {
163
- resolvedDefaultLayout = defaultLayout;
164
- /** Lazy layouts already loaded (by the `beforeResolve` preload or an async render). */
165
- const resolved = /* @__PURE__ */ new Map();
166
- /** In-flight loads, so a layout's factory runs at most once. */
167
- const pending = /* @__PURE__ */ new Map();
168
- /** `defineAsyncComponent` per name, for renders that happen before the preload. */
169
- const asyncCache = /* @__PURE__ */ new Map();
170
- const warned = /* @__PURE__ */ new Set();
171
- let fallbackWarned = false;
172
- const guardWarned = /* @__PURE__ */ new Set();
173
- /**
174
- * Layouts are rendered by `LayoutWrapper`, not matched as route components, so Vue
175
- * Router never calls their Options-API route guards; the Composition-API
176
- * `onBeforeRouteUpdate`/`onBeforeRouteLeave` still work since they subscribe directly
177
- * to the router. Skipped for a `defineAsyncComponent` wrapper that hasn't resolved
178
- * yet - the caller passes the real component once loaded.
179
- */
180
- function checkRouteGuards(name, comp) {
181
- if (guardWarned.has(name) || !comp || typeof comp !== "object" && typeof comp !== "function") return;
182
- const target = "default" in comp ? comp.default : comp;
183
- if (!target) return;
184
- const opts = target.__vccOpts ?? target;
185
- const guards = ROUTE_GUARD_NAMES.filter((g) => opts?.[g]);
186
- if (guards.length > 0) {
187
- guardWarned.add(name);
188
- console.warn(`${PREFIX} Layout "${name}" declares ${guards.join(", ")}; layouts are no longer route components, so these guards will not run. Use onBeforeRouteUpdate/onBeforeRouteLeave or a router guard instead.`);
189
- }
190
- }
191
- function load(name, loader) {
192
- let promise = pending.get(name);
193
- if (!promise) {
194
- promise = Promise.resolve(loader()).then((mod) => {
195
- const component = unwrapModule(mod);
196
- resolved.set(name, component);
197
- return component;
198
- }, (error) => {
199
- pending.delete(name);
200
- throw error;
201
- });
202
- pending.set(name, promise);
203
- }
204
- return promise;
205
- }
206
- function resolveComponent(name) {
207
- const loaded = resolved.get(name);
208
- if (loaded) {
209
- checkRouteGuards(name, loaded);
210
- return loaded;
211
- }
212
- const entry = layouts[name];
213
- if (!entry) return void 0;
214
- if (isLazyLayout(entry)) {
215
- let created = asyncCache.get(name);
216
- if (!created) {
217
- const loader = entry;
218
- created = defineAsyncComponent(() => load(name, loader));
219
- asyncCache.set(name, created);
220
- }
221
- return created;
222
- }
223
- checkRouteGuards(name, entry);
224
- return entry;
225
- }
226
- function resolveLayout(name) {
227
- const found = resolveComponent(name);
228
- if (found) return found;
229
- if (!warned.has(name)) {
230
- warned.add(name);
231
- console.warn(`${PREFIX} Layout "${name}" not found, falling back to "${fallbackLayout}"`);
232
- }
233
- const fallback = resolveComponent(fallbackLayout);
234
- if (!fallback && !fallbackWarned) {
235
- fallbackWarned = true;
236
- console.warn(`${PREFIX} Fallback layout "${fallbackLayout}" not found, rendering without a layout`);
237
- }
238
- return fallback;
239
- }
240
- const preload = async (to, from) => {
241
- const keepOverride = from === START_LOCATION || to.path === from.path;
242
- for (const rec of to.matched) {
243
- if (!rec.meta.isLayout) continue;
244
- const { name } = resolveLayoutFor(to, rec, keepOverride ? override.value : null);
245
- if (name === false) continue;
246
- const entry = layouts[name] ?? layouts[fallbackLayout];
247
- const key = layouts[name] ? name : fallbackLayout;
248
- if (isLazyLayout(entry) && !resolved.has(key)) await load(key, entry);
249
- }
250
- };
251
- function installGuards(router) {
252
- if (guardedRouters.has(router)) return;
253
- guardedRouters.add(router);
254
- router.beforeResolve(preload);
255
- router.afterEach((to, from, failure) => {
256
- if (failure) return;
257
- if (from !== START_LOCATION && to.path !== from.path) override.value = null;
258
- });
259
- }
260
- return defineComponent({
261
- name: "LayoutWrapper",
262
- beforeRouteEnter(to, from) {
263
- if (hasInjectionContext()) {
264
- const router = inject(routerKey, null);
265
- if (router) installGuards(router);
266
- }
267
- return preload(to, from, () => {});
268
- },
269
- setup() {
270
- const route = useRoute();
271
- const own = inject(matchedRouteKey);
272
- installGuards(useRouter());
273
- const layout = computed(() => resolveLayoutFor(route, own.value, override.value));
274
- return () => {
275
- const { name, props } = layout.value;
276
- if (name === false) return h(RouterView);
277
- const LayoutComponent = resolveLayout(name);
278
- return LayoutComponent ? h(LayoutComponent, props ? { ...props } : null, { default: () => h(RouterView) }) : h(RouterView);
279
- };
280
- }
281
- });
282
- }
283
- //#endregion
284
- export { createGetRoutes, createLayoutWrapper, createSetupLayouts, lazyLayout, normalizeLayoutName, setPageLayout, useLayout };