vite-plugin-generoutes 0.0.13 → 0.0.14

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 CHANGED
@@ -10,4 +10,4 @@ import { Plugin } from 'vite';
10
10
 
11
11
  declare function VitePluginGeneroutes(): Plugin<any>;
12
12
 
13
- export { VitePluginGeneroutes };
13
+ export { VitePluginGeneroutes as default };
package/dist/index.js CHANGED
@@ -6,18 +6,15 @@ 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
- var defineOptionsCache = /* @__PURE__ */ new Map();
10
9
  function VitePluginGeneroutes() {
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
- 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
-
15
+ const routesStr = `// ! \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!!!
18
16
  export const routes = ${JSON.stringify(routes, null, 2)}
19
- `;
20
- routesStr = routesStr.replace(/"##(.*)##"/g, (_, p1) => `() => import('${p1}')`);
17
+ `.replace(/"##(.*)##"/g, (_, p1) => `() => import('${p1}')`);
21
18
  const filePath = `${routesFolder}/index.js`;
22
19
  fs.writeFileSync(filePath, routesStr);
23
20
  try {
@@ -25,6 +22,42 @@ function VitePluginGeneroutes() {
25
22
  } catch (error) {
26
23
  }
27
24
  });
25
+ function generateMenusAndRoutes() {
26
+ const pages = globSync("src/pages/**/index.vue");
27
+ const routes = pages.map((filePath) => {
28
+ filePath = slash(filePath);
29
+ const defineOptions = parseDefineOptions(filePath);
30
+ defineOptionsCache.set(filePath, JSON.stringify(defineOptions));
31
+ const meta = defineOptions?.meta || {};
32
+ const fileName = path.basename(filePath);
33
+ const pageFolders = filePath.replace("src/pages/", "").replace(fileName, "").split("/").filter((item) => !!item && !/^\(.*\)$/.test(item));
34
+ meta.id = pageFolders.join("_") || "Index";
35
+ const name = defineOptions?.name || meta.id;
36
+ meta.parentId = pageFolders.slice(0, -1).join("_") || null;
37
+ const component = `##/${filePath}##`;
38
+ const folderPath = `/${pageFolders.map((item) => item.replace(/\[(.*?)\]/g, (_, p1) => p1.split(",").map((i) => `:${i}`).join("/"))).join("/")}`;
39
+ let routePath = folderPath;
40
+ if (meta.isNotFound) {
41
+ routePath = "/:pathMatch(.*)*";
42
+ }
43
+ meta.isBase = !meta.code;
44
+ if (!("title" in meta))
45
+ meta.title = name;
46
+ if (!("show" in meta))
47
+ meta.show = true;
48
+ if (!("keepAlive" in meta))
49
+ meta.keepAlive = true;
50
+ return {
51
+ name,
52
+ path: routePath,
53
+ component,
54
+ meta
55
+ };
56
+ });
57
+ return {
58
+ routes
59
+ };
60
+ }
28
61
  return {
29
62
  name: "vite-plugin-generoutes",
30
63
  async configResolved(config) {
@@ -32,8 +65,6 @@ function VitePluginGeneroutes() {
32
65
  routesFolder = slash(path.resolve(rootDir, routesFolder));
33
66
  await fs.ensureDir(routesFolder);
34
67
  chokidar.watch("src/pages/**/index.vue", { ignoreInitial: true }).on("all", (event, path2) => {
35
- if (event === "change")
36
- return;
37
68
  if (path2.endsWith("index.vue") && (event === "add" || event === "unlink")) {
38
69
  writerRoutesFile();
39
70
  }
@@ -73,42 +104,7 @@ function parseDefineOptions(filePath, content) {
73
104
  }
74
105
  return null;
75
106
  }
76
- function generateMenusAndRoutes() {
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
- }
107
+ var src_default = VitePluginGeneroutes;
112
108
  export {
113
- VitePluginGeneroutes
109
+ src_default as default
114
110
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vite-plugin-generoutes",
3
3
  "type": "module",
4
- "version": "0.0.13",
4
+ "version": "0.0.14",
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
+ }