vite-plugin-generoutes 0.0.1

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/README.md ADDED
@@ -0,0 +1 @@
1
+ # vite-plugin-generoutes
@@ -0,0 +1,13 @@
1
+ import { Plugin } from 'vite';
2
+
3
+ /**********************************
4
+ * @Author: Ronnie Zhang
5
+ * @LastEditor: Ronnie Zhang
6
+ * @LastEditTime: 2023/12/04 22:48:11
7
+ * @Email: zclzone@outlook.com
8
+ * Copyright © 2023 Ronnie Zhang(大脸怪) | https://isme.top
9
+ **********************************/
10
+
11
+ declare function VitePluginGeneroutes(): Plugin<any>;
12
+
13
+ export { VitePluginGeneroutes };
package/dist/index.js ADDED
@@ -0,0 +1,113 @@
1
+ // src/index.ts
2
+ import fs from "node:fs";
3
+ import path from "node:path";
4
+ import { exec } from "node:child_process";
5
+ import { globSync } from "glob";
6
+ import { parse } from "@vue/compiler-sfc";
7
+ import { debounce, slash } from "@antfu/utils";
8
+ import chokidar from "chokidar";
9
+ var defineOptionsCache = /* @__PURE__ */ new Map();
10
+ function VitePluginGeneroutes() {
11
+ let rootDir;
12
+ let routesFolder;
13
+ const writerRoutesFile = debounce(500, () => {
14
+ const { routes } = generateMenusAndRoutes();
15
+ let routesStr = `
16
+ // ! \u6B64\u6587\u4EF6\u6709plugin\u81EA\u52A8\u751F\u6210\uFF0C\u8BF7\u52FF\u4FEE\u6539\uFF0C\u8BF7\u52FF\u4FEE\u6539\uFF0C\u8BF7\u52FF\u4FEE\u6539!!!
17
+
18
+ export const routes = ${JSON.stringify(routes, null, 2)}
19
+ `;
20
+ routesStr = routesStr.replace(/"##(.*)##"/g, (_, p1) => `() => import('${p1}')`);
21
+ fs.writeFileSync(routesFolder, routesStr);
22
+ try {
23
+ exec(`npx eslint --fix ${routesFolder}`);
24
+ } catch (error) {
25
+ }
26
+ });
27
+ return {
28
+ name: "vite-plugin-generoutes",
29
+ async configResolved(config) {
30
+ rootDir = config.root;
31
+ routesFolder = slash(path.resolve(rootDir, "node_modules/vite-plugin-generoutes/dist/routes.js"));
32
+ chokidar.watch("src/pages/**/index.vue", { ignoreInitial: true }).on("all", (event, path2) => {
33
+ if (event === "change")
34
+ return;
35
+ if (path2.endsWith("index.vue") && (event === "add" || event === "unlink")) {
36
+ writerRoutesFile();
37
+ }
38
+ });
39
+ },
40
+ buildStart() {
41
+ writerRoutesFile();
42
+ },
43
+ async handleHotUpdate({ file, read }) {
44
+ if (file.includes("src/pages") && file.endsWith("index.vue")) {
45
+ const prevDefineOptions = defineOptionsCache.get(slash(path.relative(rootDir, file)));
46
+ if (!prevDefineOptions) {
47
+ writerRoutesFile();
48
+ } else {
49
+ const defineOptions = JSON.stringify(parseDefineOptions(file, await read()));
50
+ if (defineOptions && prevDefineOptions !== defineOptions) {
51
+ writerRoutesFile();
52
+ }
53
+ }
54
+ }
55
+ }
56
+ };
57
+ }
58
+ function parseDefineOptions(filePath, content) {
59
+ content = content ?? fs.readFileSync(filePath, "utf-8");
60
+ const { descriptor } = parse(content);
61
+ const setupScript = descriptor.scriptSetup?.content;
62
+ if (setupScript) {
63
+ const defineOptionsMatch = setupScript.match(/defineOptions\(([^)]+)\)/);
64
+ if (defineOptionsMatch) {
65
+ try {
66
+ return new Function(`return ${defineOptionsMatch[1]}`)();
67
+ } catch (e) {
68
+ console.error(`Failed to parse defineOptions in ${filePath}:`, e);
69
+ }
70
+ }
71
+ }
72
+ return null;
73
+ }
74
+ function generateMenusAndRoutes() {
75
+ const pages = globSync("src/pages/**/index.vue");
76
+ const routes = pages.map((filePath) => {
77
+ filePath = slash(filePath);
78
+ const defineOptions = parseDefineOptions(filePath);
79
+ defineOptionsCache.set(filePath, JSON.stringify(defineOptions));
80
+ const meta = defineOptions?.meta || {};
81
+ const pagePath = `/${filePath}`;
82
+ const fileName = path.basename(filePath);
83
+ const pageFolders = pagePath.replace("/src/pages/", "").replace(fileName, "").split("/").filter((item) => !!item && !/^\(.*\)$/.test(item));
84
+ meta.id = pageFolders.join("_") || "Index";
85
+ const name = defineOptions?.name || meta.id;
86
+ meta.parentId = pageFolders.slice(0, -1).join("_") || null;
87
+ const component = `##${pagePath}##`;
88
+ const folderPath = `/${pageFolders.map((item) => item.replace(/\[(.*?)\]/g, (_, p1) => p1.split(",").map((i) => `:${i}`).join("/"))).join("/")}`;
89
+ let routePath = folderPath;
90
+ if (meta.isNotFound) {
91
+ routePath = "/:pathMatch(.*)*";
92
+ }
93
+ meta.isBase = !meta.code;
94
+ if (!("title" in meta))
95
+ meta.title = name;
96
+ if (!("show" in meta))
97
+ meta.show = true;
98
+ if (!("keepAlive" in meta))
99
+ meta.keepAlive = true;
100
+ return {
101
+ name,
102
+ path: routePath,
103
+ component,
104
+ meta
105
+ };
106
+ });
107
+ return {
108
+ routes
109
+ };
110
+ }
111
+ export {
112
+ VitePluginGeneroutes
113
+ };
@@ -0,0 +1,3 @@
1
+ const routes = [];
2
+
3
+ export { routes };
package/dist/routes.js ADDED
@@ -0,0 +1,5 @@
1
+ // src/routes.js
2
+ var routes = [];
3
+ export {
4
+ routes
5
+ };
package/package.json ADDED
@@ -0,0 +1,82 @@
1
+ {
2
+ "name": "vite-plugin-generoutes",
3
+ "type": "module",
4
+ "version": "0.0.1",
5
+ "packageManager": "pnpm@9.1.1",
6
+ "description": "_description_",
7
+ "author": "Ronnie Zhang <zclzone@outlook.com>",
8
+ "license": "MIT",
9
+ "homepage": "https://github.com/zclzone/vite-plugin-generoutes",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/zclzone/vite-plugin-generoutes.git"
13
+ },
14
+ "bugs": "https://github.com/zclzone/vite-plugin-generoutes/issues",
15
+ "keywords": [],
16
+ "sideEffects": false,
17
+ "exports": {
18
+ ".": {
19
+ "types": "./dist/index.d.ts",
20
+ "import": "./dist/index.js"
21
+ },
22
+ "./routes": {
23
+ "types": "./dist/routes/index.d.ts",
24
+ "import": "./dist/routes.js"
25
+ }
26
+ },
27
+ "main": "./dist/index.js",
28
+ "module": "./dist/index.js",
29
+ "types": "./dist/index.d.ts",
30
+ "typesVersions": {
31
+ "*": {
32
+ "*": [
33
+ "./dist/*",
34
+ "./dist/index.d.ts"
35
+ ]
36
+ }
37
+ },
38
+ "files": [
39
+ "dist"
40
+ ],
41
+ "scripts": {
42
+ "start": "tsup --watch",
43
+ "build": "tsup",
44
+ "lint": "eslint .",
45
+ "prepublishOnly": "nr build",
46
+ "release": "bumpp && npm publish",
47
+ "test": "vitest",
48
+ "typecheck": "tsc --noEmit",
49
+ "prepare": "simple-git-hooks"
50
+ },
51
+ "peerDependencies": {
52
+ "vite": "^5.2.11"
53
+ },
54
+ "dependencies": {
55
+ "@antfu/utils": "^0.7.8",
56
+ "@vue/compiler-sfc": "^3.4.27",
57
+ "chokidar": "^3.6.0",
58
+ "glob": "^10.4.1"
59
+ },
60
+ "devDependencies": {
61
+ "@antfu/eslint-config": "^2.18.1",
62
+ "@antfu/ni": "^0.21.12",
63
+ "@types/node": "^20.12.12",
64
+ "bumpp": "^9.4.1",
65
+ "eslint": "^9.3.0",
66
+ "esno": "^4.7.0",
67
+ "lint-staged": "^15.2.2",
68
+ "pnpm": "^9.1.1",
69
+ "rimraf": "^5.0.7",
70
+ "simple-git-hooks": "^2.11.1",
71
+ "tsup": "^8.1.0",
72
+ "typescript": "^5.4.5",
73
+ "unbuild": "^2.0.0",
74
+ "vitest": "^1.6.0"
75
+ },
76
+ "simple-git-hooks": {
77
+ "pre-commit": "pnpm lint-staged"
78
+ },
79
+ "lint-staged": {
80
+ "*": "eslint --fix"
81
+ }
82
+ }