Skip to content

Commit

Permalink
fix(projects): fix route const update
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Sep 8, 2023
1 parent facaa69 commit 2cc84f8
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 24 deletions.
1 change: 1 addition & 0 deletions examples/template-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"private": true,
"scripts": {
"dev": "vite",
"debug": "vite --debug",
"build": "run-s type-check build-only",
"preview": "vite preview",
"build-only": "vite build",
Expand Down
2 changes: 1 addition & 1 deletion examples/template-vue/src/layouts/base-layout/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import { RouterView } from 'vue-router';
defineOptions({
name: 'DefaultLayout'
name: 'BaseLayout'
});
</script>

Expand Down
15 changes: 15 additions & 0 deletions examples/template-vue/src/layouts/blank-layout/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<template>
<div>
<RouterView />
</div>
</template>

<script setup lang="ts">
import { RouterView } from 'vue-router';
defineOptions({
name: 'BlankLayout'
});
</script>

<style scoped></style>
5 changes: 4 additions & 1 deletion examples/template-vue/src/router/elegant/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
import type { RouteComponent } from "vue-router";
import type { LastLevelRouteKey, RouteLayout } from "@elegant-router/types";

import BaseLayout from "@/layouts/base-layout/index.vue";
import BlankLayout from "@/layouts/blank-layout/index.vue";

export const layouts: Record<RouteLayout, RouteComponent | (() => Promise<RouteComponent>)> = {
base: () => import("@/layouts/base-layout/index.vue"),
base: BaseLayout,
blank: BlankLayout,
};

export const views: Record<LastLevelRouteKey, RouteComponent | (() => Promise<RouteComponent>)> = {
Expand Down
2 changes: 1 addition & 1 deletion examples/template-vue/src/router/elegant/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { ElegantRoute } from '@elegant-router/types';
export const autoRoutes: ElegantRoute[] = [
{
path: '/403',
component: 'layout.base',
component: 'layout.blank',
children: [
{
name: '403',
Expand Down
2 changes: 1 addition & 1 deletion examples/template-vue/src/typings/elegant-router.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ declare module "@elegant-router/types" {
/**
* route layout
*/
export type RouteLayout = "base";
export type RouteLayout = "base" | "blank";

/**
* route map
Expand Down
13 changes: 12 additions & 1 deletion examples/template-vue/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@ import Inspect from 'vite-plugin-inspect';
import ElegantVueRouter from '@elegant-router/vue/vite';

export default defineConfig({
plugins: [vue(), vueJsx(), ElegantVueRouter(), Inspect()],
plugins: [
vue(),
vueJsx(),
ElegantVueRouter({
layouts: {
base: 'src/layouts/base-layout/index.vue',
blank: 'src/layouts/blank-layout/index.vue'
},
layoutLazyImport: () => false
}),
Inspect()
],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
Expand Down
32 changes: 17 additions & 15 deletions packages/vue/src/context/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,6 @@ export const autoRoutes: ElegantRoute[] = [];
return code;
}

function isValidLayout(layout: string, layouts: Record<string, string>) {
const layoutName = layout.replace(LAYOUT_PREFIX, '');

return Boolean(layouts[layoutName]);
}

export function getUpdatedRouteConst(oldConst: AutoRoute[], newConst: AutoRoute[], options: ElegantVueRouterOption) {
const updated = newConst.map(item => {
const hasName = Boolean(item?.name);
Expand All @@ -88,20 +82,28 @@ export function getUpdatedRouteConst(oldConst: AutoRoute[], newConst: AutoRoute[
}

if (findItem.component) {
/**
* invalid layout
* @description maybe the layouts are updated
*/
const isInValidLayout =
findItem.component.includes(LAYOUT_PREFIX) && !isValidLayout(findItem.component, options.layouts);

if (!isInValidLayout) {
const isView = findItem.component.startsWith(VIEW_PREFIX);
const isLayout = findItem.component.startsWith(LAYOUT_PREFIX);
const layoutName = findItem.component.replace(LAYOUT_PREFIX, '');
const hasLayout = Boolean(options.layouts[layoutName]);

if (isView || (isLayout && !hasLayout)) {
findItem.component = item.component;
}
}

if (findItem.redirect) {
findItem.redirect = item.redirect;
const { children = [] } = findItem;

const childrenNames = children.map(i => i.name);

const { name: redirectName } = findItem.redirect as { name: string };

const hasRedirect = redirectName && childrenNames.includes(redirectName);

if (!hasRedirect) {
findItem.redirect = item.redirect;
}
}

const children = getUpdatedRouteConst(findItem.children || [], item.children || [], options);
Expand Down
5 changes: 3 additions & 2 deletions packages/vue/src/context/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export function createPluginOptions(erOptions: ElegantRouterOption, options?: Pa
'not-found': '/:pathMatch(.*)*'
};
const DEFAULT_LAYOUTS: Record<string, string> = {
base: 'src/layouts/base-layout/index.vue'
base: 'src/layouts/base-layout/index.vue',
blank: 'src/layouts/blank-layout/index.vue'
};

const opts: ElegantVueRouterOption = {
Expand All @@ -25,7 +26,7 @@ export function createPluginOptions(erOptions: ElegantRouterOption, options?: Pa
customRoutesMap: CUSTOM_ROUTES_MAP,
layouts: DEFAULT_LAYOUTS,
defaultLayout: Object.keys(DEFAULT_LAYOUTS)[0],
layoutLazyImport: _name => true,
layoutLazyImport: _name => false,
transformDir: 'src/router/elegant/transform.ts',
onRouteMetaGen: name => ({
title: name
Expand Down
11 changes: 9 additions & 2 deletions packages/vue/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export interface ElegantVueRouterOption extends ElegantRouterOption {
* @default
* ```ts
* const layouts: Record<string, string> = {
* base: 'src/layouts/base-layout/index.vue'
* base: 'src/layouts/base-layout/index.vue',
* blank: 'src/layouts/blank-layout/index.vue'
* }
* ```
*/
Expand All @@ -62,7 +63,7 @@ export interface ElegantVueRouterOption extends ElegantRouterOption {
/**
* whether the route is lazy import
* @param layoutName the layout name
* @default _name => true
* @default _name => false
*/
layoutLazyImport(layoutName: string): boolean;
/**
Expand All @@ -73,6 +74,12 @@ export interface ElegantVueRouterOption extends ElegantRouterOption {
/**
* the route meta generator
* @param routeName the route name
* @default
* ```ts
* const onRouteMetaGen = (routeName: string) => ({
* title: routeName
* })
* ```
*/
onRouteMetaGen(routeName: string): Record<string, unknown>;
}
Expand Down

0 comments on commit 2cc84f8

Please sign in to comment.