vite-plugin-generoutes 0.0.13 → 0.0.15
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 +5 -2
- package/dist/index.js +55 -51
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,9 @@ import { Plugin } from 'vite';
|
|
|
8
8
|
* Copyright © 2023 Ronnie Zhang(大脸怪) | https://isme.top
|
|
9
9
|
**********************************/
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
interface Options {
|
|
12
|
+
isBuild?: boolean;
|
|
13
|
+
}
|
|
14
|
+
declare function VitePluginGeneroutes(options?: Options): Plugin<any>;
|
|
12
15
|
|
|
13
|
-
export { VitePluginGeneroutes };
|
|
16
|
+
export { VitePluginGeneroutes as default };
|
package/dist/index.js
CHANGED
|
@@ -6,18 +6,17 @@ import { globSync } from "glob";
|
|
|
6
6
|
import { parse } from "@vue/compiler-sfc";
|
|
7
7
|
import { debounce, slash } from "@antfu/utils";
|
|
8
8
|
import chokidar from "chokidar";
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
function VitePluginGeneroutes(options = {}) {
|
|
10
|
+
const defineOptionsCache = /* @__PURE__ */ new Map();
|
|
11
11
|
let rootDir;
|
|
12
12
|
let routesFolder = "src/router/generoutes/";
|
|
13
13
|
const writerRoutesFile = debounce(500, async () => {
|
|
14
14
|
const { routes } = generateMenusAndRoutes();
|
|
15
|
-
|
|
16
|
-
// ! \u6B64\u6587\u4EF6\
|
|
17
|
-
|
|
15
|
+
const routesStr = `
|
|
16
|
+
// ! \u6B64\u6587\u4EF6\u7531 vite-plugin-generoutes \u81EA\u52A8\u751F\u6210\uFF0C\u8BF7\u52FF\u4FEE\u6539\uFF0C\u8BF7\u52FF\u4FEE\u6539\uFF0C\u8BF7\u52FF\u4FEE\u6539!!!
|
|
17
|
+
|
|
18
18
|
export const routes = ${JSON.stringify(routes, null, 2)}
|
|
19
|
-
|
|
20
|
-
routesStr = routesStr.replace(/"##(.*)##"/g, (_, p1) => `() => import('${p1}')`);
|
|
19
|
+
`.replace(/"##(.*)##"/g, (_, p1) => `() => import('${p1}')`);
|
|
21
20
|
const filePath = `${routesFolder}/index.js`;
|
|
22
21
|
fs.writeFileSync(filePath, routesStr);
|
|
23
22
|
try {
|
|
@@ -25,22 +24,62 @@ function VitePluginGeneroutes() {
|
|
|
25
24
|
} catch (error) {
|
|
26
25
|
}
|
|
27
26
|
});
|
|
27
|
+
function generateMenusAndRoutes() {
|
|
28
|
+
const pages = globSync("src/pages/**/index.vue");
|
|
29
|
+
const routes = pages.map((filePath) => {
|
|
30
|
+
filePath = slash(filePath);
|
|
31
|
+
const defineOptions = parseDefineOptions(filePath);
|
|
32
|
+
defineOptionsCache.set(filePath, JSON.stringify(defineOptions));
|
|
33
|
+
const meta = defineOptions?.meta || {};
|
|
34
|
+
const fileName = path.basename(filePath);
|
|
35
|
+
const pageFolders = filePath.replace("src/pages/", "").replace(fileName, "").split("/").filter((item) => !!item && !/^\(.*\)$/.test(item));
|
|
36
|
+
meta.id = pageFolders.join("_") || "Index";
|
|
37
|
+
const name = defineOptions?.name || meta.id;
|
|
38
|
+
meta.parentId = pageFolders.slice(0, -1).join("_") || null;
|
|
39
|
+
const component = `##/${filePath}##`;
|
|
40
|
+
const folderPath = `/${pageFolders.map((item) => item.replace(/\[(.*?)\]/g, (_, p1) => p1.split(",").map((i) => `:${i}`).join("/"))).join("/")}`;
|
|
41
|
+
let routePath = folderPath;
|
|
42
|
+
if (meta.isNotFound) {
|
|
43
|
+
routePath = "/:pathMatch(.*)*";
|
|
44
|
+
}
|
|
45
|
+
meta.isBase = !meta.code;
|
|
46
|
+
if (!("title" in meta))
|
|
47
|
+
meta.title = name;
|
|
48
|
+
if (!("show" in meta))
|
|
49
|
+
meta.show = true;
|
|
50
|
+
if (!("keepAlive" in meta))
|
|
51
|
+
meta.keepAlive = true;
|
|
52
|
+
return {
|
|
53
|
+
name,
|
|
54
|
+
path: routePath,
|
|
55
|
+
component,
|
|
56
|
+
meta
|
|
57
|
+
};
|
|
58
|
+
});
|
|
59
|
+
return {
|
|
60
|
+
routes
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function createWatcher() {
|
|
64
|
+
const watcher = chokidar.watch("src/pages/**/index.vue", { ignoreInitial: true });
|
|
65
|
+
return watcher.on("all", async (event, path2) => {
|
|
66
|
+
if (path2.endsWith("index.vue") && (event === "add" || event === "unlink")) {
|
|
67
|
+
writerRoutesFile();
|
|
68
|
+
await watcher.close();
|
|
69
|
+
createWatcher();
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|
|
28
73
|
return {
|
|
29
74
|
name: "vite-plugin-generoutes",
|
|
30
75
|
async configResolved(config) {
|
|
31
76
|
rootDir = config.root;
|
|
32
77
|
routesFolder = slash(path.resolve(rootDir, routesFolder));
|
|
33
78
|
await fs.ensureDir(routesFolder);
|
|
34
|
-
chokidar.watch("src/pages/**/index.vue", { ignoreInitial: true }).on("all", (event, path2) => {
|
|
35
|
-
if (event === "change")
|
|
36
|
-
return;
|
|
37
|
-
if (path2.endsWith("index.vue") && (event === "add" || event === "unlink")) {
|
|
38
|
-
writerRoutesFile();
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
79
|
},
|
|
42
80
|
buildStart() {
|
|
43
81
|
writerRoutesFile();
|
|
82
|
+
!options.isBuild && createWatcher();
|
|
44
83
|
},
|
|
45
84
|
async handleHotUpdate({ file, read }) {
|
|
46
85
|
if (file.includes("src/pages") && file.endsWith("index.vue")) {
|
|
@@ -73,42 +112,7 @@ function parseDefineOptions(filePath, content) {
|
|
|
73
112
|
}
|
|
74
113
|
return null;
|
|
75
114
|
}
|
|
76
|
-
|
|
77
|
-
const pages = globSync("src/pages/**/index.vue");
|
|
78
|
-
const routes = pages.map((filePath) => {
|
|
79
|
-
filePath = slash(filePath);
|
|
80
|
-
const defineOptions = parseDefineOptions(filePath);
|
|
81
|
-
defineOptionsCache.set(filePath, JSON.stringify(defineOptions));
|
|
82
|
-
const meta = defineOptions?.meta || {};
|
|
83
|
-
const fileName = path.basename(filePath);
|
|
84
|
-
const pageFolders = filePath.replace("src/pages/", "").replace(fileName, "").split("/").filter((item) => !!item && !/^\(.*\)$/.test(item));
|
|
85
|
-
meta.id = pageFolders.join("_") || "Index";
|
|
86
|
-
const name = defineOptions?.name || meta.id;
|
|
87
|
-
meta.parentId = pageFolders.slice(0, -1).join("_") || null;
|
|
88
|
-
const component = `##/${filePath}##`;
|
|
89
|
-
const folderPath = `/${pageFolders.map((item) => item.replace(/\[(.*?)\]/g, (_, p1) => p1.split(",").map((i) => `:${i}`).join("/"))).join("/")}`;
|
|
90
|
-
let routePath = folderPath;
|
|
91
|
-
if (meta.isNotFound) {
|
|
92
|
-
routePath = "/:pathMatch(.*)*";
|
|
93
|
-
}
|
|
94
|
-
meta.isBase = !meta.code;
|
|
95
|
-
if (!("title" in meta))
|
|
96
|
-
meta.title = name;
|
|
97
|
-
if (!("show" in meta))
|
|
98
|
-
meta.show = true;
|
|
99
|
-
if (!("keepAlive" in meta))
|
|
100
|
-
meta.keepAlive = true;
|
|
101
|
-
return {
|
|
102
|
-
name,
|
|
103
|
-
path: routePath,
|
|
104
|
-
component,
|
|
105
|
-
meta
|
|
106
|
-
};
|
|
107
|
-
});
|
|
108
|
-
return {
|
|
109
|
-
routes
|
|
110
|
-
};
|
|
111
|
-
}
|
|
115
|
+
var src_default = VitePluginGeneroutes;
|
|
112
116
|
export {
|
|
113
|
-
|
|
117
|
+
src_default as default
|
|
114
118
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-generoutes",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.15",
|
|
5
5
|
"packageManager": "pnpm@9.1.1",
|
|
6
6
|
"description": "_description_",
|
|
7
7
|
"author": "Ronnie Zhang <zclzone@outlook.com>",
|
|
@@ -77,4 +77,4 @@
|
|
|
77
77
|
"lint-staged": {
|
|
78
78
|
"*": "eslint --fix"
|
|
79
79
|
}
|
|
80
|
-
}
|
|
80
|
+
}
|