From 8cd4e4d6c81b6ed813f08f07cb7a4e31b7a90212 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=90=AC=E6=B5=B7?= <445436867@qq.com>
Date: Thu, 6 Apr 2023 16:28:32 +0800
Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B7=BB=E5=8A=A0[...slug].vue=E6=96=B9?=
=?UTF-8?q?=E5=BC=8F=E6=9B=BF=E6=8D=A2*.vue=E5=AE=9E=E7=8E=B0=E6=A8=A1?=
=?UTF-8?q?=E7=B3=8A=E5=8C=B9=E9=85=8D=20(#182)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
* fix: 添加[...slug].vue方式替换*.vue实现模糊匹配
* fix: 兼容大写
---
docs/guide/route.md | 2 +-
.../src/plugins/misc/route/index.js | 15 ++++++++++-----
packages/fes-template/src/pages/@id/add.vue | 0
packages/fes-template/src/pages/[...].vue | 9 +++++++++
4 files changed, 20 insertions(+), 6 deletions(-)
delete mode 100644 packages/fes-template/src/pages/@id/add.vue
create mode 100644 packages/fes-template/src/pages/[...].vue
diff --git a/docs/guide/route.md b/docs/guide/route.md
index 900eab26..83cb528a 100644
--- a/docs/guide/route.md
+++ b/docs/guide/route.md
@@ -34,7 +34,7 @@ export default {
```
pages
├── index.vue # 根路由页面 路径为 /
-├── *.vue # 模糊匹配 路径为 *
+├── [...slug].vue # 模糊匹配 路径为 /:slug(.*)
├── a.vue # 路径 /a
├── b # 文件夹b
│ ├── index.vue # 路径 /b
diff --git a/packages/fes-preset-built-in/src/plugins/misc/route/index.js b/packages/fes-preset-built-in/src/plugins/misc/route/index.js
index 95568f2e..454c3e80 100644
--- a/packages/fes-preset-built-in/src/plugins/misc/route/index.js
+++ b/packages/fes-preset-built-in/src/plugins/misc/route/index.js
@@ -13,7 +13,7 @@ const logger = new Logger('fes:router');
// pages
// ├── index.vue # 根路由页面 路径 /
-// ├── *.vue # 模糊匹配 路径 *
+// ├── [...slug].vue # 模糊匹配 路径 /:slug(.*)
// ├── a.vue # 路径 /a
// ├── b
// │ ├── index.vue # 路径 /b
@@ -49,7 +49,8 @@ const getRouteName = function (parentRoutePath, fileName) {
.slice(1)
.replace(/\//g, '_')
.replace(/@/g, '_')
- .replace(/\*/g, 'FUZZYMATCH');
+ .replace(/\*/g, 'FUZZYMATCH')
+ .replace(/\[...([a-zA-Z]*)\]/, 'FUZZYMATCH-$1');
};
const getRoutePath = function (parentRoutePath, fileName, isFile = true) {
@@ -61,10 +62,14 @@ const getRoutePath = function (parentRoutePath, fileName, isFile = true) {
if (fileName.startsWith('@')) {
fileName = fileName.replace(/@/, ':');
}
- // /*.vue -> :pathMatch(.*)
+ // /*.vue -> /:pathMatch(.*)
if (fileName.includes('*')) {
fileName = fileName.replace('*', ':pathMatch(.*)');
}
+ // /[...slug].vue -> /:slug(.*)
+ if (/\[...[a-zA-Z]*\]/.test(fileName)) {
+ fileName = fileName.replace(/\[...([a-zA-Z]*)\]/, ':$1(.*)').replace(':(.*)', ':pathMatch(.*)');
+ }
return winPath(join(parentRoutePath, fileName));
};
@@ -195,9 +200,9 @@ const rank = function (routes) {
let count = 0;
arr.forEach((sonPath) => {
count += 4;
- if (sonPath.indexOf(':') !== -1 && sonPath.indexOf(':pathMatch(.*)') === -1) {
+ if (sonPath.indexOf(':') !== -1 && sonPath.indexOf('(.*)') === -1) {
count += 2;
- } else if (sonPath.indexOf(':pathMatch(.*)') !== -1) {
+ } else if (sonPath.indexOf('(.*)') !== -1) {
count -= 1;
} else if (sonPath === '') {
count += 1;
diff --git a/packages/fes-template/src/pages/@id/add.vue b/packages/fes-template/src/pages/@id/add.vue
deleted file mode 100644
index e69de29b..00000000
diff --git a/packages/fes-template/src/pages/[...].vue b/packages/fes-template/src/pages/[...].vue
new file mode 100644
index 00000000..9cb5b4e2
--- /dev/null
+++ b/packages/fes-template/src/pages/[...].vue
@@ -0,0 +1,9 @@
+
+ *
+
+