vite-plugin-generoutes 0.1.0 → 0.1.2
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/dist/index.d.ts +1 -0
- package/dist/index.js +22 -16
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -6,8 +6,14 @@ import { parse } from "@vue/compiler-sfc";
|
|
|
6
6
|
import { debounce, slash } from "@antfu/utils";
|
|
7
7
|
import chokidar from "chokidar";
|
|
8
8
|
import prettier from "prettier";
|
|
9
|
+
function toPascalCase(str) {
|
|
10
|
+
return str.toLowerCase().replace(/(?:^|-)([a-z])/g, (_, p1) => {
|
|
11
|
+
return p1.toUpperCase();
|
|
12
|
+
});
|
|
13
|
+
}
|
|
9
14
|
var defaultOptions = {
|
|
10
|
-
routesFolder: "src/router/generoutes/"
|
|
15
|
+
routesFolder: "src/router/generoutes/",
|
|
16
|
+
pagesFolder: "src/pages"
|
|
11
17
|
};
|
|
12
18
|
function VitePluginGeneroutes(options = defaultOptions) {
|
|
13
19
|
const defineOptionsCache = /* @__PURE__ */ new Map();
|
|
@@ -26,44 +32,44 @@ function VitePluginGeneroutes(options = defaultOptions) {
|
|
|
26
32
|
}
|
|
27
33
|
const debounceWriter = debounce(500, writerRoutesFile);
|
|
28
34
|
function generateMenusAndRoutes() {
|
|
29
|
-
const pages = globSync(
|
|
35
|
+
const pages = globSync(`${options.pagesFolder}/**/index.vue`).concat(globSync(`src/pages/**/\\[...all\\].vue`));
|
|
30
36
|
const routes = pages.map((filePath) => {
|
|
31
37
|
filePath = slash(filePath);
|
|
32
38
|
const defineOptions = parseDefineOptions(filePath);
|
|
33
39
|
defineOptionsCache.set(filePath, JSON.stringify(defineOptions));
|
|
34
40
|
const meta = defineOptions?.meta || {};
|
|
41
|
+
if (meta.enabled === false)
|
|
42
|
+
return null;
|
|
35
43
|
const fileName = path.basename(filePath);
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
meta.parentId = pageFolders.slice(0, -1).join("_") || null;
|
|
44
|
+
const pageName = path.parse(fileName).name;
|
|
45
|
+
const pathSegments = filePath.replace(`${options.pagesFolder}/`, "").replace(fileName, "").split("/").filter((item) => !!item && !/^\(.*\)$/.test(item));
|
|
46
|
+
let name = defineOptions?.name || pathSegments.map((item) => toPascalCase(item)).join("_") || "Index";
|
|
40
47
|
const component = `##/${filePath}##`;
|
|
41
|
-
let routePath = `/${
|
|
42
|
-
if (
|
|
43
|
-
|
|
48
|
+
let routePath = `/${pathSegments.map((item) => item.replace(/\[(.*?)\]/g, (_, p1) => p1.split(",").map((i) => `:${i}`).join("/"))).join("/")}`;
|
|
49
|
+
if (pageName === "[...all]") {
|
|
50
|
+
name = `${name}_[...all]`;
|
|
51
|
+
routePath = routePath === "/" ? "/:pathMatch(.*)*" : `${routePath}/:pathMatch(.*)*`;
|
|
44
52
|
}
|
|
45
|
-
meta.isBase = !meta.code;
|
|
46
53
|
if (!("title" in meta))
|
|
47
54
|
meta.title = name;
|
|
48
55
|
if (!("show" in meta))
|
|
49
56
|
meta.show = true;
|
|
50
|
-
|
|
51
|
-
meta.keepAlive = true;
|
|
57
|
+
meta.keepAlive = !!defineOptions?.name && meta.keepAlive !== false;
|
|
52
58
|
return {
|
|
53
59
|
name,
|
|
54
60
|
path: routePath,
|
|
55
61
|
component,
|
|
56
62
|
meta
|
|
57
63
|
};
|
|
58
|
-
});
|
|
64
|
+
}).filter(Boolean);
|
|
59
65
|
return {
|
|
60
66
|
routes
|
|
61
67
|
};
|
|
62
68
|
}
|
|
63
69
|
function createWatcher() {
|
|
64
|
-
const watcher = chokidar.watch("src/pages/**/
|
|
70
|
+
const watcher = chokidar.watch(["src/pages/**/*.vue", `src/pages/**/[...all].vue`], { ignoreInitial: true });
|
|
65
71
|
return watcher.on("all", async (event, path2) => {
|
|
66
|
-
if (path2.endsWith("index.vue") && (event === "add" || event === "unlink")) {
|
|
72
|
+
if ((path2.endsWith("index.vue") || path2.endsWith("[...all].vue")) && (event === "add" || event === "unlink")) {
|
|
67
73
|
debounceWriter();
|
|
68
74
|
await watcher.close();
|
|
69
75
|
createWatcher();
|
|
@@ -80,7 +86,7 @@ function VitePluginGeneroutes(options = defaultOptions) {
|
|
|
80
86
|
config.command !== "build" && createWatcher();
|
|
81
87
|
},
|
|
82
88
|
async handleHotUpdate({ file, read }) {
|
|
83
|
-
if (file.includes("src/pages") && file.endsWith("index.vue")) {
|
|
89
|
+
if (file.includes("src/pages") && (file.endsWith("index.vue") || file.endsWith("[...all].vue"))) {
|
|
84
90
|
const prevDefineOptions = defineOptionsCache.get(slash(path.relative(rootDir, file)));
|
|
85
91
|
if (!prevDefineOptions) {
|
|
86
92
|
debounceWriter();
|