vite-plugin-generoutes 0.2.1 → 0.2.3

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
@@ -15,6 +15,12 @@ interface Options {
15
15
  * default: `src/pages`
16
16
  */
17
17
  pagesFolder: string;
18
+ /**
19
+ * 忽略文件夹
20
+ *
21
+ * default: `['components']`
22
+ */
23
+ ignoreFolders: string[];
18
24
  /**
19
25
  * 路由文件路径
20
26
  *
package/dist/index.js CHANGED
@@ -35,10 +35,33 @@ function convertToTree(routes) {
35
35
  });
36
36
  return result;
37
37
  }
38
+ function findDuplicateRoutes(routes) {
39
+ const nameSet = /* @__PURE__ */ new Set();
40
+ const pathSet = /* @__PURE__ */ new Set();
41
+ const duplicateNames = [];
42
+ const duplicatePaths = [];
43
+ for (const route of routes) {
44
+ if (nameSet.has(route.name)) {
45
+ duplicateNames.push(route.name);
46
+ } else {
47
+ nameSet.add(route.name);
48
+ }
49
+ if (pathSet.has(route.path)) {
50
+ duplicatePaths.push(route.path);
51
+ } else {
52
+ pathSet.add(route.path);
53
+ }
54
+ }
55
+ return {
56
+ duplicateNames,
57
+ duplicatePaths
58
+ };
59
+ }
38
60
 
39
61
  // src/index.ts
40
62
  var defaultOptions = {
41
63
  pagesFolder: "src/pages",
64
+ ignoreFolders: ["components"],
42
65
  nested: false
43
66
  };
44
67
  function VitePluginGeneroutes(options = {}) {
@@ -48,9 +71,10 @@ function VitePluginGeneroutes(options = {}) {
48
71
  const pagesFolder = options.pagesFolder || defaultOptions.pagesFolder;
49
72
  const routesPath = options.routesPath || path.join(pagesFolder, "generoutes.js");
50
73
  const nested = options.nested || defaultOptions.nested;
74
+ const ignoreFolders = options.ignoreFolders || defaultOptions.ignoreFolders;
51
75
  const defineOptionsCache = /* @__PURE__ */ new Map();
52
76
  function generateMenusAndRoutes() {
53
- const pages = globSync(`${pagesFolder}/**/index.vue`).concat(globSync(`${pagesFolder}/**/\\[...all\\].vue`));
77
+ const pages = globSync(`${pagesFolder}/**/*.vue`, { ignore: ignoreFolders.map((folder) => `${pagesFolder}/**/${folder}/**`) });
54
78
  const routes = pages.map((filePath) => {
55
79
  filePath = slash(filePath);
56
80
  const defineOptions = parseDefineOptions(filePath) || {};
@@ -58,15 +82,10 @@ function VitePluginGeneroutes(options = {}) {
58
82
  const meta = defineOptions?.meta || {};
59
83
  if (meta.enabled === false)
60
84
  return null;
61
- const fileName = path.basename(filePath);
62
- const pathSegments = path.dirname(filePath).replace(`${pagesFolder}`, "").split("/").filter((item) => !!item && !/^\(.*\)$/.test(item));
63
- let name = defineOptions?.name || pathSegments.map((item) => toPascalCase(item)).join("_") || "Index";
85
+ const pathSegments = filePath.replace(`${pagesFolder}`, "").replace(".vue", "").replace("index", "").split("/").filter((item) => !!item && !/^\(.*\)$/.test(item));
86
+ const name = defineOptions?.name || pathSegments.map((item) => toPascalCase(item)).join("_") || "Index";
64
87
  const component = `##/${filePath}##`;
65
- let routePath = `/${pathSegments.map((item) => item.replace(/\[(.*?)\]/g, (_, p1) => p1.split(",").map((i) => `:${i}`).join("/"))).join("/")}`;
66
- if (fileName === "[...all].vue") {
67
- name = `${name}_[...all]`;
68
- routePath = routePath === "/" ? "/:pathMatch(.*)*" : `${routePath}/:pathMatch(.*)*`;
69
- }
88
+ const routePath = `/${pathSegments.map((item) => item.replace(/\[(.*?)\]/g, (_, p1) => p1 === "...all" ? ":pathMatch(.*)*" : p1.split(",").map((i) => `:${i}`).join("/"))).join("/")}`;
70
89
  if (!("title" in meta))
71
90
  meta.title = name;
72
91
  if (!("show" in meta))
@@ -80,6 +99,11 @@ function VitePluginGeneroutes(options = {}) {
80
99
  parent: defineOptions?.parent
81
100
  };
82
101
  }).filter(Boolean);
102
+ const { duplicateNames, duplicatePaths } = findDuplicateRoutes(routes);
103
+ if (duplicateNames.length)
104
+ console.warn(`Warning: Duplicate names found in routes: ${duplicateNames.join(", ")}`);
105
+ if (duplicatePaths.length)
106
+ console.warn(`Warning: Duplicate paths found in routes: ${duplicatePaths.join(", ")}`);
83
107
  return {
84
108
  routes: nested ? convertToTree(routes) : routes
85
109
  };
@@ -100,9 +124,11 @@ function VitePluginGeneroutes(options = {}) {
100
124
  }
101
125
  const debounceWriter = debounce(500, writerRoutesFile);
102
126
  function createWatcher() {
103
- const watcher = chokidar.watch([`${pagesFolder}/**/index.vue`, `${pagesFolder}/**/[...all].vue`], { ignoreInitial: true });
127
+ const watcher = chokidar.watch(`${pagesFolder}/**/*.vue`, { ignoreInitial: true });
104
128
  return watcher.on("all", async (event, path2) => {
105
- if ((path2.endsWith("index.vue") || path2.endsWith("[...all].vue")) && (event === "add" || event === "unlink")) {
129
+ if (ignoreFolders.some((folder) => slash(path2).includes(`/${folder}/`)))
130
+ return;
131
+ if (path2.endsWith(".vue") && (event === "add" || event === "unlink")) {
106
132
  debounceWriter();
107
133
  await watcher.close();
108
134
  createWatcher();
@@ -117,15 +143,11 @@ function VitePluginGeneroutes(options = {}) {
117
143
  config.command !== "build" && createWatcher();
118
144
  },
119
145
  async handleHotUpdate({ file, read }) {
120
- if (file.includes(pagesFolder) && (file.endsWith("index.vue") || file.endsWith("[...all].vue"))) {
146
+ if (file.includes(pagesFolder) && !ignoreFolders.some((folder) => file.includes(`/${folder}/`)) && file.endsWith(".vue")) {
121
147
  const prevDefineOptions = defineOptionsCache.get(slash(path.relative(rootDir, file)));
122
- if (!prevDefineOptions) {
148
+ const defineOptions = JSON.stringify(parseDefineOptions(file, await read()));
149
+ if (prevDefineOptions !== defineOptions) {
123
150
  debounceWriter();
124
- } else {
125
- const defineOptions = JSON.stringify(parseDefineOptions(file, await read()));
126
- if (prevDefineOptions !== defineOptions) {
127
- debounceWriter();
128
- }
129
151
  }
130
152
  }
131
153
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vite-plugin-generoutes",
3
3
  "type": "module",
4
- "version": "0.2.1",
4
+ "version": "0.2.3",
5
5
  "packageManager": "pnpm@9.1.1",
6
6
  "description": "_description_",
7
7
  "author": "Ronnie Zhang <zclzone@outlook.com>",