Skip to content

Commit

Permalink
fix: synchronising logic to vite-plugin-vue-layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
markthree committed Dec 29, 2023
1 parent 6c5ff16 commit 3e88b20
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 78 deletions.
69 changes: 35 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,40 +30,40 @@ npm i vite-plugin-vue-meta-layouts -D

```ts
// vite.config.js
import { defineConfig } from "vite";
import Vue from "@vitejs/plugin-vue";
import MetaLayouts from "vite-plugin-vue-meta-layouts";
import { defineConfig } from "vite"
import Vue from "@vitejs/plugin-vue"
import MetaLayouts from "vite-plugin-vue-meta-layouts"

export default defineConfig({
plugins: [Vue(), MetaLayouts()],
});
})
```

#### 使用

```ts
import { setupLayouts } from "virtual:meta-layouts";
import { createRouter, createWebHistory } from "vue-router";
import { setupLayouts } from "virtual:meta-layouts"
import { createRouter, createWebHistory } from "vue-router"

const routes = setupLayouts([
{
// ... 页面路由配置
},
]);
])

const router = createRouter({
routes,
history: createWebHistory(),
});
})
```

1. 创建 `layouts/default.vue` 默认布局,此时页面都会被应用该布局

```html
<template>
default
<router-view />
<!-- 视图出口 -->
default
<router-view />
<!-- 视图出口 -->
</template>
```

Expand All @@ -76,7 +76,7 @@ const router = createRouter({
const home = {
path: "/",
component: () => import("./pages/home.vue"),
};
}

// 应用 layouts/other.vue 布局
const about = {
Expand All @@ -85,9 +85,9 @@ const about = {
meta: {
layout: "other", // 通过元信息来管理布局
},
};
}

const routes = setupLayouts([home, about]);
const routes = setupLayouts([home, about])
```

<br />
Expand All @@ -106,31 +106,31 @@ npm i vite-plugin-pages -D

```ts
// vite.config.js
import { defineConfig } from "vite";
import Vue from "@vitejs/plugin-vue";
import Pages from "vite-plugin-pages"; // 引入文件路由插件
import MetaLayouts from "vite-plugin-vue-meta-layouts";
import { defineConfig } from "vite"
import Vue from "@vitejs/plugin-vue"
import Pages from "vite-plugin-pages" // 引入文件路由插件
import MetaLayouts from "vite-plugin-vue-meta-layouts"

export default defineConfig({
plugins: [
Vue(),
Pages(), // 配置文件路由插件
MetaLayouts(),
],
});
})
```

##### 使用

```ts
import fileRoutes from "~pages"; // 引入文件路由表
import { setupLayouts } from "virtual:meta-layouts";
import { createRouter, createWebHistory } from "vue-router";
import fileRoutes from "~pages" // 引入文件路由表
import { setupLayouts } from "virtual:meta-layouts"
import { createRouter, createWebHistory } from "vue-router"

const router = createRouter({
routes: setupLayouts(fileRoutes), // 注册文件路由表
history: createWebHistory(),
});
})
```

此时可以通过页面中的自定义块 `route``meta` 来做布局配置
Expand All @@ -156,14 +156,14 @@ npm i unplugin-vue-router -D
##### 使用

```ts
import { routes } from "vue-router/auto/routes"; // 引入文件路由表
import { setupLayouts } from "virtual:meta-layouts";
import { createRouter, createWebHistory } from "vue-router";
import { routes } from "vue-router/auto/routes" // 引入文件路由表
import { setupLayouts } from "virtual:meta-layouts"
import { createRouter, createWebHistory } from "vue-router"

const router = createRouter({
routes: setupLayouts(routes), // 注册文件路由表
history: createWebHistory(),
});
})
```

<br />
Expand All @@ -173,9 +173,9 @@ const router = createRouter({

```ts
// vite.config.js
import { defineConfig } from "vite";
import Vue from "@vitejs/plugin-vue";
import MetaLayouts from "vite-plugin-vue-meta-layouts";
import { defineConfig } from "vite"
import Vue from "@vitejs/plugin-vue"
import MetaLayouts from "vite-plugin-vue-meta-layouts"

export default defineConfig({
plugins: [
Expand All @@ -184,9 +184,10 @@ export default defineConfig({
target: "src/layouts", // 布局目录,默认 src/layouts
defaultLayout: "default", // 默认布局,默认为 default
importMode: "sync", // 加载模式,支持 sync 和 async。默认为自动处理,SSG 时为 sync,非 SSG 时为 async
skipTopLevelRouteLayout: true, // 打开修复 https://github.com/JohnCampionJr/vite-plugin-vue-layouts/issues/134,默认为 false 关闭
}),
],
});
})
```

<br />
Expand Down Expand Up @@ -222,12 +223,12 @@ export default defineConfig({
👇

```ts
import { createGetRoutes } from "virtual:meta-layouts";
import { createGetRoutes } from "virtual:meta-layouts"

const getRoutes = createGetRoutes(router);
const getRoutes = createGetRoutes(router)

// 获取路由表但是不包含布局路由
console.log(getRoutes());
console.log(getRoutes())
```

<br />
Expand Down
67 changes: 34 additions & 33 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,39 +31,39 @@ npm i vite-plugin-vue-meta-layouts -D

```ts
// vite.config.js
import { defineConfig } from "vite";
import Vue from "@vitejs/plugin-vue";
import MetaLayouts from "vite-plugin-vue-meta-layouts";
import { defineConfig } from "vite"
import Vue from "@vitejs/plugin-vue"
import MetaLayouts from "vite-plugin-vue-meta-layouts"

export default defineConfig({
plugins: [Vue(), MetaLayouts()],
});
})
```

#### usage

```ts
import { setupLayouts } from "virtual:meta-layouts";
import { createRouter, createWebHistory } from "vue-router";
import { setupLayouts } from "virtual:meta-layouts"
import { createRouter, createWebHistory } from "vue-router"

const routes = setupLayouts([
{
// ... Page routes
},
]);
])

const router = createRouter({
routes,
history: createWebHistory(),
});
})
```

1. `layouts/default.vue` 👉 The default layout, which is now applied to the page

```html
<template>
default
<router-view />
default
<router-view />
</template>
```

Expand All @@ -76,7 +76,7 @@ For example `layouts/other.vue`
const home = {
path: "/",
component: () => import("./pages/home.vue"),
};
}

// apply layouts/other.vue layout
const about = {
Expand All @@ -85,9 +85,9 @@ const about = {
meta: {
layout: "other", // Manage layouts through meta information
},
};
}

const routes = setupLayouts([home, about]);
const routes = setupLayouts([home, about])
```

<br />
Expand All @@ -106,31 +106,31 @@ npm i vite-plugin-pages -D

```ts
// vite.config.js
import { defineConfig } from "vite";
import Vue from "@vitejs/plugin-vue";
import Pages from "vite-plugin-pages"; // Introducing the file routing plugin
import MetaLayouts from "vite-plugin-vue-meta-layouts";
import { defineConfig } from "vite"
import Vue from "@vitejs/plugin-vue"
import Pages from "vite-plugin-pages" // Introducing the file routing plugin
import MetaLayouts from "vite-plugin-vue-meta-layouts"

export default defineConfig({
plugins: [
Vue(),
Pages(), // Configure the configuration file routing plug-in
MetaLayouts(),
],
});
})
```

##### 使用

```ts
import fileRoutes from "~pages"; // file routes
import { setupLayouts } from "virtual:meta-layouts";
import { createRouter, createWebHistory } from "vue-router";
import fileRoutes from "~pages" // file routes
import { setupLayouts } from "virtual:meta-layouts"
import { createRouter, createWebHistory } from "vue-router"

const router = createRouter({
routes: setupLayouts(fileRoutes), // Register the file routes
history: createWebHistory(),
});
})
```

At this time, the layout can be configured by `meta` of the custom block `route`
Expand All @@ -157,14 +157,14 @@ npm i unplugin-vue-router -D
##### usage

```ts
import { routes } from "vue-router/auto/routes"; // file routes
import { setupLayouts } from "virtual:meta-layouts";
import { createRouter, createWebHistory } from "vue-router";
import { routes } from "vue-router/auto/routes" // file routes
import { setupLayouts } from "virtual:meta-layouts"
import { createRouter, createWebHistory } from "vue-router"

const router = createRouter({
routes: setupLayouts(routes), // Register the file routes
history: createWebHistory(),
});
})
```

<br />
Expand All @@ -174,9 +174,9 @@ const router = createRouter({

```ts
// vite.config.js
import { defineConfig } from "vite";
import Vue from "@vitejs/plugin-vue";
import MetaLayouts from "vite-plugin-vue-meta-layouts";
import { defineConfig } from "vite"
import Vue from "@vitejs/plugin-vue"
import MetaLayouts from "vite-plugin-vue-meta-layouts"

export default defineConfig({
plugins: [
Expand All @@ -185,9 +185,10 @@ export default defineConfig({
target: "src/layouts", // Layout directory, default src/layouts
defaultLayout: "default", // Default layout, which defaults to default
importMode: "sync", // Load mode, support sync and async. The default is automatic processing, sync for SSGs, and async for non-SSGs
skipTopLevelRouteLayout: true, // Turn on fixing https://github.com/JohnCampionJr/vite-plugin-vue-layouts/issues/134, default is false Close
}),
],
});
})
```

<br />
Expand Down Expand Up @@ -225,12 +226,12 @@ layer, it may cause confusion in obtaining the routing table, and auxiliary
functions can be used at this time 👇

```ts
import { createGetRoutes } from "virtual:meta-layouts";
import { createGetRoutes } from "virtual:meta-layouts"

const getRoutes = createGetRoutes(router);
const getRoutes = createGetRoutes(router)

// Gets the route table, but does not contain layout routes
console.log(getRoutes());
console.log(getRoutes())
```

<br />
Expand Down
4 changes: 3 additions & 1 deletion examples/unplugin-vue-router/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export default defineConfig({
plugins: [
Vue(),
Inspect(),
MetaLayouts(),
MetaLayouts({
skipTopLevelRouteLayout: true,
}),
VueRouter({
dts: "types/typed-router.d.ts",
}),
Expand Down
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@ export interface Options {
/**
* default auto resolve
*/
importMode: "sync" | "async";
importMode?: "sync" | "async";
/**
* If opened, fix →
* https://github.com/JohnCampionJr/vite-plugin-vue-layouts/issues/134
* @default false
*/
skipTopLevelRouteLayout?: boolean;
}

export default function MetaLayouts(options: Partial<Options> = {}): Plugin {
const {
target = "src/layouts",
defaultLayout = "default",
importMode = process.env.VITE_SSG ? "sync" : "async",
skipTopLevelRouteLayout = false,
} = options;

const { virtualModuleId, resolvedVirtualModuleId } = createVirtualModuleID(
Expand All @@ -42,6 +49,7 @@ export default function MetaLayouts(options: Partial<Options> = {}): Plugin {
target,
importMode,
defaultLayout,
skipTopLevelRouteLayout,
});
}
},
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function normalizePath(path: string) {
export async function isVite2() {
const info = await getPackageInfo("vite");
if (info) {
return /^.?2/.test(info.version);
return /^.?2/.test(info.version!);
}
return false;
}
Loading

0 comments on commit 3e88b20

Please sign in to comment.