vite-plugin-generoutes 0.1.1 → 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 CHANGED
@@ -10,6 +10,7 @@ import { Plugin } from 'vite';
10
10
 
11
11
  interface Options {
12
12
  routesFolder?: string;
13
+ pagesFolder?: string;
13
14
  }
14
15
  declare function VitePluginGeneroutes(options?: Options): Plugin<any>;
15
16
 
package/dist/index.js CHANGED
@@ -12,7 +12,8 @@ function toPascalCase(str) {
12
12
  });
13
13
  }
14
14
  var defaultOptions = {
15
- routesFolder: "src/router/generoutes/"
15
+ routesFolder: "src/router/generoutes/",
16
+ pagesFolder: "src/pages"
16
17
  };
17
18
  function VitePluginGeneroutes(options = defaultOptions) {
18
19
  const defineOptionsCache = /* @__PURE__ */ new Map();
@@ -31,7 +32,7 @@ function VitePluginGeneroutes(options = defaultOptions) {
31
32
  }
32
33
  const debounceWriter = debounce(500, writerRoutesFile);
33
34
  function generateMenusAndRoutes() {
34
- const pages = globSync("src/pages/**/index.vue");
35
+ const pages = globSync(`${options.pagesFolder}/**/index.vue`).concat(globSync(`src/pages/**/\\[...all\\].vue`));
35
36
  const routes = pages.map((filePath) => {
36
37
  filePath = slash(filePath);
37
38
  const defineOptions = parseDefineOptions(filePath);
@@ -40,20 +41,20 @@ function VitePluginGeneroutes(options = defaultOptions) {
40
41
  if (meta.enabled === false)
41
42
  return null;
42
43
  const fileName = path.basename(filePath);
43
- const pageFolders = filePath.replace("src/pages/", "").replace(fileName, "").split("/").filter((item) => !!item && !/^\(.*\)$/.test(item));
44
- const name = defineOptions?.name || pageFolders.map((item) => toPascalCase(item)).join("_") || "Index";
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";
45
47
  const component = `##/${filePath}##`;
46
- let routePath = `/${pageFolders.map((item) => item.replace(/\[(.*?)\]/g, (_, p1) => p1.split(",").map((i) => `:${i}`).join("/"))).join("/")}`;
47
- if (meta.isNotFound) {
48
- routePath = "/:pathMatch(.*)*";
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(.*)*`;
49
52
  }
50
- meta.isBase = !meta.code;
51
53
  if (!("title" in meta))
52
54
  meta.title = name;
53
55
  if (!("show" in meta))
54
56
  meta.show = true;
55
- if (!("keepAlive" in meta))
56
- meta.keepAlive = true;
57
+ meta.keepAlive = !!defineOptions?.name && meta.keepAlive !== false;
57
58
  return {
58
59
  name,
59
60
  path: routePath,
@@ -66,9 +67,9 @@ function VitePluginGeneroutes(options = defaultOptions) {
66
67
  };
67
68
  }
68
69
  function createWatcher() {
69
- const watcher = chokidar.watch("src/pages/**/index.vue", { ignoreInitial: true });
70
+ const watcher = chokidar.watch(["src/pages/**/*.vue", `src/pages/**/[...all].vue`], { ignoreInitial: true });
70
71
  return watcher.on("all", async (event, path2) => {
71
- if (path2.endsWith("index.vue") && (event === "add" || event === "unlink")) {
72
+ if ((path2.endsWith("index.vue") || path2.endsWith("[...all].vue")) && (event === "add" || event === "unlink")) {
72
73
  debounceWriter();
73
74
  await watcher.close();
74
75
  createWatcher();
@@ -85,7 +86,7 @@ function VitePluginGeneroutes(options = defaultOptions) {
85
86
  config.command !== "build" && createWatcher();
86
87
  },
87
88
  async handleHotUpdate({ file, read }) {
88
- if (file.includes("src/pages") && file.endsWith("index.vue")) {
89
+ if (file.includes("src/pages") && (file.endsWith("index.vue") || file.endsWith("[...all].vue"))) {
89
90
  const prevDefineOptions = defineOptionsCache.get(slash(path.relative(rootDir, file)));
90
91
  if (!prevDefineOptions) {
91
92
  debounceWriter();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vite-plugin-generoutes",
3
3
  "type": "module",
4
- "version": "0.1.1",
4
+ "version": "0.1.2",
5
5
  "packageManager": "pnpm@9.1.1",
6
6
  "description": "_description_",
7
7
  "author": "Ronnie Zhang <zclzone@outlook.com>",